uniapp android 原生插件封装--入门篇

本文将介绍uniapp 插件封装官方例子的搭建,运行。和注意事项,避坑

1、文档地址:简介 | uni小程序SDK

开发者须知 | uni小程序SDK

Android平台第三方插件开发指导 - DCloud问答 这个介绍插件与原生的架构

2、下载官方示例:百度网盘 请输入提取码 : jrrb

3、下载最新的 android sdk .2023-3-1

编译项目:

这个版本默认的SDK是17,官方的例子是旧项目。会提示sdkb版本太高,需要如下设置:把sdk改为你本机的1.8

3、代码需要添加gradle 配置:uniplugin_richalert ->build.gradle

复制代码
dependencies {
    implementation 'androidx.test.ext:junit:1.1.5'
    compileOnly fileTree(dir: 'libs', include: ['*.jar'])

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'

    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'



    testImplementation 'junit:junit:4.12'

    compileOnly 'androidx.recyclerview:recyclerview:1.0.0'
    compileOnly 'androidx.legacy:legacy-support-v4:1.0.0'
    compileOnly 'androidx.appcompat:appcompat:1.0.0'
    compileOnly 'com.alibaba:fastjson:1.2.83'

    compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar'])

    compileOnly 'com.alibaba:fastjson:1.1.46.android'
}
复制代码
uni.dcloud.io.uniplugin_richalert.ExampleInstrumentedTest.java 这个代码修改:
复制代码
package uni.dcloud.io.uniplugin_richalert;

import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {

        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("uni.dcloud.io.uniplugin_richalert.test", appContext.getPackageName());
    }
}

到这步,编译可以通过了。

运行插件

接下来,如何运行插件,并进行测试:

1、在hbuilderx 导入项目:

2、申请appId

3、发行--》原生app本地打包--》生成本地资源

4、把打包的文件拷贝到 安卓开发的app/assets/apps/{appid}下。

5、配置这

6、需要配appKey 不然运行会提示没有配,这步需要导dcloud官网

手册:申请 | uni小程序SDK

申请过程需要 sha1 sha256 可以从目录下的这个文件提取:test.jks

javascript 复制代码
keytool -list -v -keystore test.jks

7、在android开发工具里配置 appkey:

包名要和申请的一样

都搞定了。运行:

相关推荐
七七小报2 小时前
uniapp-商城-40-shop 购物车 选好了 进行订单确认4 配送方式3 地址编辑
uni-app
xuanjiong6 小时前
微信小程序,基于uni-app的轮播图制作,调用文件中图片
微信小程序·小程序·uni-app
flying robot6 小时前
uniapp: 低功耗蓝牙(BLE)的使用
uni-app
程序猿John6 小时前
uniapp跳转和获取参数方式
前端·javascript·uni-app
七七小报21 小时前
uniapp-商城-38-shop 购物车 选好了 进行订单确认4 配送方式1
uni-app
七七小报1 天前
uniapp-商城-39-shop 购物车 选好了 进行订单确认4 配送方式2 地址页面
uni-app
G_GreenHand1 天前
uniapp 仿小红书轮播图效果
uni-app
性野喜悲1 天前
uniapp返回上一页接口数据更新了,页面未更新
uni-app
冰镇生鲜1 天前
小程序·安全·胶囊·容器组件
前端·vue.js·uni-app
盛夏绽放1 天前
uni-app云开发总结
uni-app·云开发