1、
- JAVA環境 jdk1.8
- Android Studio 下載地址:Android Studio官網 OR Android Studio中文社區
- App離線SDK下載:請下載2.9.8+版本的android平台SDK 版本和HbuilderX版本相同
2、
- 解壓sdk后 導入uniSDK中的UniPlugin-Hello-AS項目
- 自動下載gradle 和 ndk (project模式下 根目錄 build.gradle 可能需要修改maven庫地址,百度一下)
3、Android模式下,右鍵app new module 選擇 Android Library -> Module name 輸入個人前綴(mrt_firstModule) -> Package name -> edit ->done -> 選擇java-> finish
4、Android模式下, Gradle Scripts -> 文件的所有內容復制到自建的 mrt_firstModule 中 //implementation 'xxx:xxx:1.0.0' //添加依賴包
5、右鍵 mrt_firstModule -> new -> Java Class -> testModule (Class)
6、
{
"nativePlugins": [
{
"plugins": [
{
"type": "module",
"name": "TestModule",
"class": "io.dcloud.uniplugin.TestModule"
}
]
},
{
"plugins": [
{
"type": "component",
"name": "myText",
"class": "io.dcloud.uniplugin.TestText"
}
]
},
{
"hooksClass": "",
"plugins": [
{
"type": "module",
"name": "DCloud-RichAlert",
"class": "uni.dcloud.io.uniplugin_richalert.RichAlertModule"
}
]
},
{
"plugins": [
{
"type": "module",
"name": "mrt_firstModule",
"class": "com.example.mrt_firstmodule.testModule" //粘貼
}]
}
]
}
implementation project(':mrt_firstModule')
回到 testModule 中
package com.example.mrt_firstmodule;
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 testModule extends UniModule {
/*
JS中調用方法
module.add({
},callback)
*/
@UniJSMethod
public void add(JSONObject json, UniJSCallback callback){
final int a = json.getIntValue("a");
final int b = json.getIntValue("b");
callback.invoke(new JSONObject(){{
put("code",0);
put("result",a+b);
}});
}
}
7、
添加插件調試 debug='true' syncDebug='true'
<hbuilder debug='true' syncDebug='true'>
<apps>
<app appid="__UNI__B2A5BFD" appver=""/>
</apps>
</hbuilder>
8、新建離線文件(hbuilderX)
<template>
<view class="content">
<button type="default" @tap="testadd">ADD FUN</button>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
testadd() {
const uniModule = uni.requireNativePlugin('mrt_firstModule');
uniModule.add({
a: 1,
b: 2
}, res => {
uni.showToast({
title:JSON.stringify(res)
})
})
}
}
}
</script>
生成離線文件后復制到Android Studio中 的 assets中
登錄 https://dev.dcloud.net.cn/ 選擇剛剛創建的uniapp項目,配置 Android 包名 和 Android 證書簽名SHA1
Android Studio 4.2 中 Tasks 隱藏了,需要在
連接手機 運行app查看 調試一下
連接成功
9、雙擊打包
打包完成后 mrt_firstModule\build\outputs\aar 復制 mrt_firstModule-release.aar 到HbuilderX中的項目中 mrt_firstModule命名一定不能錯
雲打包。下載安裝即可
https://gitee.com/yyy090320_admin/uni-plugins