一、准备开发环境
1.Android Studio Arctic Fox | 2020.3.1 Patch 1
2.HBuilder X 3.2.3.20210825
3.Android-SDK@3.2.3.81011_20210826
二、开始开发
1.导入uni插件原生项目 UniPlugin-Hello-AS工程请在App离线SDK中查找
点击Android Studio菜单选项File—>New—>Import Project。
2.升级项目为androidx 点击等待即可
三、创建插件Library
1.new moudle
2.选择Android Library
3.输入 Library名称toastplus 点击finish
4.在gradle script build.gradle ( uniplugs-hello-as.app ) 下引入 toastplus
implementation project(':toastplus')
5.修改 gradle script build.gradle ( uniplugs-hello-as.toastplug ) 下追加
compileOnly'com.android.support:recyclerview-v7:28.0.0'
compileOnly'com.android.support:support-v4:28.0.0'
compileOnly'com.android.support:appcompat-v7:28.0.0'
compileOnly'com.alibaba:fastjson:1.1.46.android'
compileOnly fileTree(include: ['uniapp-v8-release.aar'],dir:'../app/libs')
6.在toastplug目录下创建ToastPlus类集成UniMoudle
package com.example.toastplus;
import android.widget.Toast;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;
public class Toastplusextends UniModule {
UniJSCallbackjsCallback;
Stringpath ="";
/**
* 简单回调示例
* 传入一个String类型 处理并返回
*/
@UniJSMethod(uiThread =true)
public void processData(String data, UniJSCallback jsCallback) {
this.jsCallback = jsCallback;
path = data +"+(处理完成)";
//返回处理结果 进行回调
jsCallback.invoke(path);
}
/**
* 调用原生Toast显示传入内容
*/
@UniJSMethod(uiThread =true)
public void showToast(String message) {
Toast.makeText(mWXSDKInstance.getContext(), message, Toast.LENGTH_SHORT).show();
}
}
7.app/dcloud_uniplugins.json文件中添加插件信息
,
{
"hooksClass": "",
"plugins": [
{
"type": "module",
"name": "toastplus",
"class": "com.example.toastplus.Toastplus"
}
]
}
三、创建uni项目
打开HBuilderX,务必保证使用最新uniapp安卓原生SDK以及HBuilderX最新版本
新建uniapp项目 并修改index代码
<template>
<view class="content">
<view class="button1">
<button @click="showToast(message)">点击显示原生Toast</button>
</view>
<view class="button2">
<button @click="processData(waitprocessdata)">回调的使用</button>
</view>
</view>
</template>
<script>
const ToastPlusModule = uni.requireNativePlugin("toastplus")
export default {
data() {
return {
message: '我是一条很长的Toast我是一条很长的Toast',
waitprocessdata: '我是待处理的数据'
}
},
onLoad() {
},
methods: {
showToast(message) {
ToastPlusModule.showToast(message)
},
processData(data) {
ToastPlusModule.processData(data, processokdata => {
ToastPlusModule.showToast(processokdata)
})
}
}
}
</script>
<style>
.button1 {
display: flex;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
flex-direction: column;
align-items: center;
justify-content: center;
}
.button2 {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 100rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
</style>
2.点击本地资源打包
[HBuilder] 11:01:10.138 项目 'test'导出成功,路径为:C:\Users\Allen\Desktop\uni\test\unpackage\resources\__UNI__ECBA764\www
3.复制 __UNI__ECBA764 文件夹到AndroidStudio
app/src/main/assets/apps/目录下,删除原有的 __UNI__E 文件夹
4.dcloud_control.xml中的appid字段
<app appid="__UNI__ECBA764" appver=""/>
5. 3.1.10版本起需要申请 Appkey(dcloud_appkey),否则无法正常运行uniapp并调试插件,申请参考
6.修改build.gradle ( uniplug-hello-as.app) ,和生成证书时填写一致
signingConfigs{
config{
keyAlias'testalias'
keyPassword'111111'
storeFile file('test.keystore')
storePassword'111111'
v1SigningEnabledtrue
v2SigningEnabledtrue
}
}
7.复制生成证书时test.keystore 文件到 .app目录下