uniapp Android 原生插件


1、

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM