安卓第三方SDK打包成uni-app插件

这篇博客介绍了如何在uni-app中使用Android Studio创建并配置原生插件,包括设置Android Studio的版本、Gradle依赖和环境。作者通过一个名为`ToastPlus`的示例模块展示了如何调用第三方SDK进行Base64编码和解码操作,并提供了uni-app的package.json配置、测试组件代码以及打包自定义基座的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

打包自定义基座,测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值