uniapp插件开发例子
官方插件开发流程 https://nativesupport.dcloud.net.cn/NativePlugin/course/package.html#
官方sdk下载 https://nativesupport.dcloud.net.cn/AppDocs/download/android.html#
1、用android stdio 打开 Android-SDK@3.8.4.81844_20230531UniPlugin-Hello-AS 切换到“project”
2、右键选择项目->New->Module
3、打开build.gradle 其中compileSdkVersion 需要选择适合的
apply plugin: 'com.android.library' android { compileSdkVersion 33 defaultConfig { minSdkVersion 16 targetSdkVersion 28 testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } repositories { flatDir { dirs 'libs' } } dependencies { compileOnly fileTree(dir: 'libs', include: ['*.jar']) compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar']) compileOnly 'androidx.recyclerview:recyclerview:1.0.0' compileOnly 'androidx.legacy:legacy-support-v4:1.0.0' compileOnly 'androidx.appcompat:appcompat:1.0.0' implementation 'com.alibaba:fastjson:1.2.83' implementation 'com.facebook.fresco:fresco:1.13.0' /*implementation 'com.android.support:appcompat-v7:28.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'*/ }
4、打开proguard-rules.pro
# Add project specific ProGuard rules here. # You can control the set of applied configuration files using the # proguardFiles setting in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile -keep public class * extends io.dcloud.feature.uniapp.common.UniModule{*;}
5、刷新gradle配置文件
6、新建java类 并编写代码 这里用了前台服务
package com.example.testposition; import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.util.Log; import com.alibaba.fastjson.JSONObject; import io.dcloud.feature.uniapp.annotation.UniJSMethod; import io.dcloud.feature.uniapp.bridge.UniJSCallback; import io.dcloud.feature.uniapp.common.UniModule; public class TestPosition extends UniModule { @androidx.annotation.RequiresApi(api = android.os.Build.VERSION_CODES.S) @UniJSMethod(uiThread = true) public void openPositionService(String text, final UniJSCallback callback) { Context context = mWXSDKInstance.getContext(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //Android 8.0以上调用 PositionService.setAct((Activity) context); context.startForegroundService(new Intent(context, PositionService.class)); } callback.invokeAndKeepAlive("打开定位成功"); } @androidx.annotation.RequiresApi(api = android.os.Build.VERSION_CODES.S) @UniJSMethod(uiThread = true) public void closePositionService(String text, final UniJSCallback callback) { Context context = mWXSDKInstance.getContext(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //Android 8.0以上调用 PositionService.setAct((Activity) context); context.stopService(new Intent(context, PositionService.class)); } callback.invokeAndKeepAlive("关闭定位成功"); } }
7、打开AndroidManifest.xml
8、编写uniapp页面代码
{{title}} {{msg}}
9、制作证书
testalias 证书名
test.keystore 证书保存的文件名
keytool -genkey -alias testalias -keyalg RSA -keysize 2048 -validity 36500 -keystore test.keystore
查看证书信息,用来生成dcloud_appkey时有用
keytool -list -v -keystore test.keystore Enter keystore password: //输入密码,回车
10、制作自定义基座
证书用刚刚的证书
11、登录开发者后台
https://dev.dcloud.net.cn/pages/common/login?uniIdRedirectUrl=%252Fpages%252Fapp%252Flist
找到刚刚的应用
用刚刚获取的签名信息填进去
生成的打包key是这样的
12、配置真机测试环境
需要修改的文件
build.gradle文件修改
AndroidManifest.xml文件修改
dcloud_uniplugins.json 文件修改
需要什么module就加,如果其中一个module编译不通过,整个应用的插件都不能使用
13、生成本地打包资源
14、把生成的打包资源放入apps文件夹中
15、连接手机,打开usb调试 ,运行
16、生成aar供uniapp打包程序用
编译生成module的aar文件:选择Gradle--->插件module--->Tasks--->other--->assembleRelease(运行生成module的aar文件)
如果没有assembleRelease 把钩去掉