删除build.gradle插件
删除settings.gradle 设置
刷新一下项目
加载一下依赖
test-module – build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
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.1.46.android'
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'*/
}
test-module – AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_module" />
修改完后再重写加载一下依赖
public class UniTestModule extends UniModule {
@UniJSMethod(uiThread = false)
public void testAsyncFunc(JSONObject options, UniJSCallback callback) {
if(callback != null) {
JSONObject data = new JSONObject();
data.put("code", "success");
callback.invoke(data);
}
}
}
package.json 文件
{
"name": "test-module",
"id": "test-module",
"version": "1.0.0",
"description": "内置插件",
"_dp_type": "nativeplugin",
"_dp_nativeplugin": {
"android": {
"plugins": [{
"type": "module", //module 或 component类型
"name": "test-module", //注册名称 和后续uniapp项目模块导入名字一致
"class": "com.example.test_module.UniTestModule" //原生项目实体类完整名称
}],
"integrateType": "aar",
"abis": []
}
}
}
<template>
<view>
<button @click="handleTest">测试按钮</button>
<view>
返回信息:{{result}}
</view>
</view>
</template>
<script>
const testModule = uni.requireNativePlugin('test-module')
export default {
data() {
return {
result:""
}
},
onLoad() {
},
methods: {
handleTest(){
const param = {name:"张三"}
testModule.testAsyncFunc(param,(res)=>{
this.result = JSON.stringify(res)
})
}
}
}
</script>
DCloud开发者中心:https://dev.dcloud.net.cn/
android 包名 与 ios 一致为:com.android.UniPlugin
生成步骤教程:https://blog.csdn.net/qq812457115/article/details/126011332
{
"nativePlugins": [
{
"plugins": [
{
"type": "module",
"name": "test-module",
"class": "com.example.test_module.UniTestModule"
}
]
}
]
}
重新加载一下依赖