android studio版本和环境配置
android studio2021.1.1
gradle-6.5 -> classpath 'com.android.tools.build:gradle:4.1.1'
参考uniapp使用安卓原生插件(包含插件带第三方jar)_执拗的小豆芽的博客-优快云博客_uniapp 原生插件
导入下载的项目UniPlugin-Hello-AS
下载Android 离线SDK
https://nativesupport.dcloud.net.cn/AppDocs/download/android
package com.example.mylibrary;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import java.io.IOException;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
import io.dcloud.feature.uniapp.common.UniModule;
public class ToastPlus extends UniModule {
JSCallback jsCallback;
String path = "";
@JSMethod(uiThread = true)
public void encoder(String data, JSCallback jsCallback) {
//调用第三方sdk处理字符串
this.jsCallback = jsCallback;
BASE64Encoder encoder = new BASE64Encoder();
String encode = encoder.encode(data.getBytes());
jsCallback.invoke(encode);
}
@JSMethod(uiThread = true)
public void decoder(String data, JSCallback jsCallback) throws IOException {
//调用第三方sdk处理字符串
this.jsCallback = jsCallback;
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(data);
jsCallback.invoke(new String(bytes));
}
}
dependencies {
implementation files('libs\\BASE64Decoder.jar')
compileOnly fileTree(dir: 'libs', include: ['*.jar'])
compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar'])
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
在uni-app中使用刚刚生成的aar包
package.json配置
{
"name": "toastPlusTest",
"id": "toastPlusTest",
"version": "1.0.0",
"description": "原生Toast",
"_dp_type":"nativeplugin",
"_dp_nativeplugin":{
"android": {
"plugins": [
{
"type": "module",
"name":"toastPlusTest",
"class": "com.example.mylibrary.ToastPlus"
}
],
"integrateType": "aar"
}
}
}
选择刚刚配置的插件
新建test.vue 测试一下
<template>
<view class="content">
<view class="button3"><button @click="encoder(encoderStr)">编码字符串</button></view>
<view class="button4"><button @click="decoder()">解码字符串</button></view>
</view>
</template>
<script>
const ToastPlusModule = uni.requireNativePlugin('toastPlusTest');
export default {
data() {
return {
decoderStr: '',
encoderStr: '请点击控制台工具栏上的'
};
},
onLoad() {},
methods: {
encoder(data) {
console.log('23132213');
ToastPlusModule.encoder(data, res => {
console.log('1111111');
this.decoderStr = res;
uni.showToast({
title: '编码数据--->' + res,
icon: 'none',
duration: 2000
});
});
},
decoder() {
ToastPlusModule.decoder(this.decoderStr, res => {
uni.showToast({
title: '解码数据-->' + res,
icon: 'none',
duration: 2000
});
});
}
}
};
</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;
}
.button3 {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 100rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.button4 {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 100rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
</style>
打包自定义基座,测试