Android studio打包uniapp插件

[复制链接]
发表于 2025-9-30 00:40:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
一.参考资料与环境预备



  • 原生工程设置
  • 必要使用到Android studio和HbuilderX

    • 当前测试的as版本-20240301,下载地点:
    • HbuilderX版本:4.36

二.插件创建流程



  • 1.导入下载的UniPlugin-Hello-AS工程(下载地点见参考资料)

  • 2.天生jks证书

    • 2.1.通过下令行天生jsk证书,假设jks名称为myjks,则输入以下下令
    1. keytool -genkeypair -alias myjks -keyalg RSA -keystore myjks.keystore -keysize 2048 -validity 36500 -sigalg SHA256withRSA
    复制代码
      

    • 2.2.将证书格式转一下
    1. keytool -importkeystore -srckeystore myjks.keystore -srcstoretype PKCS12 -deststoretype JKS -destkeystore myjks.jks
    复制代码

  • 使用下令获取SHA1和SHA256两个值(下方第三步中会使用到)
    1. keytool -list -v -keystore myjks.jks
    复制代码
  • 3.在uniapp配景添加离线打包的设置

    • 在配景选择使用HbuilderX创建的项目,选择“离线打包Key管理”,按照下图方式举行设置

    • 生存之后选择“创建离线key”,记录天生的key为Android对应的value的值


  • 4.打包本地资源


    • 将天生的本地资源放入文件夹中(UniPlugin-Hello-AS\app\src\main\assets\apps)

  • 5.删除UniPlugin-Hello-AS自带的module,新建一个module(假设取名为mylibrary)

    • 5.1.更改mylibrary的内容,添加文件

    • 5.2.更改mylibrary的build.gradle中内容,
    1. plugins {
    2.   id 'com.android.library'
    3. }
    4. android {
    5.   compileSdkVersion 35
    6.   namespace 'com.example.mylibrary'
    7.   defaultConfig {
    8.       minSdk 24
    9.       ndk {
    10.           moduleName "NDK"
    11.           abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
    12.       }
    13.       testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    14.       consumerProguardFiles "consumer-rules.pro"
    15.   }
    16.   buildTypes {
    17.       release {
    18.           minifyEnabled false
    19.           proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    20.       }
    21.   }
    22.   compileOptions {
    23.       sourceCompatibility JavaVersion.VERSION_11
    24.       targetCompatibility JavaVersion.VERSION_11
    25.   }
    26.   sourceSets {
    27.       main {
    28.           jni.srcDirs = ['libs']
    29.           jni.srcDirs = ['src/main/jniLibs']
    30.           jniLibs.srcDir 'src/main/jniLibs'
    31.       }
    32.   }
    33. }
    34. dependencies {
    35.   implementation files('src\\libs\\AclasDriverSdk.jar')
    36.   implementation files('src\\libs\\LABLE_USB_SDK_V01.08.jar')
    37.   implementation files('src\\libs\\NeoStraElectronicSDK-3-v1.3_2302281129.jar')
    38.   implementation files('src\\libs\\zxing.jar')
    39.   compileOnly fileTree(dir: 'libs', include: ['*.jar'])
    40.   compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar'])
    41. //    compileOnly 'androidx.recyclerview:recyclerview:1.0.0'
    42. //    compileOnly 'androidx.legacy:legacy-support-v4:1.0.0'
    43.   compileOnly 'androidx.appcompat:appcompat:1.6.1'
    44.   implementation 'com.alibaba:fastjson:1.2.83'
    45.   implementation 'com.facebook.fresco:fresco:1.13.0'
    46.   implementation 'com.github.bumptech.glide:glide:4.16.0'
    47.   implementation 'androidx.appcompat:appcompat:1.6.1'
    48.   implementation 'com.google.android.material:material:1.10.0'
    49.   testImplementation 'junit:junit:4.13.2'
    50.   androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    51.   androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    52. }
    复制代码

  • 6.开始写自界说插件的逻辑

    • 新建Java文件, MyPlug.java,文件中界说的两个方法init和printInfo可以由uniapp项目举行调用,后续分析
    1. public class MyPlug extends UniModule {
    2.   //定义方法一,uniapp项目调用不需要传递参数
    3.   @UniJSMethod
    4.   public void init(UniJSCallback callback) {
    5.       JSONObject data = new JSONObject();
    6.       data.put("code", "success");
    7.       data.put("data", "操作成功");
    8.       callback.invoke(data);
    9.   }
    10.   //定义方法二,uniapp项目调用可以传递参数
    11.   @UniJSMethod
    12.   public void printInfo(int markType, String width, String height, String jsonInfo, UniJSCallback callback) throws IOException {
    13.       JSONObject data = new JSONObject();
    14.       data.put("code", "success");
    15.       data.put("data", "操作成功");
    16.       callback.invoke(data);
    17.   }
    18. }
    复制代码

  • 7.更改\UniPlugin-Hello-AS\app\src\main\AndroidManifest.xml中的内容
    1. 将<meta-data
    2.         android:name="dcloud_appkey"
    3.         android:value="开发者需登录https://dev.dcloud.net.cn/申请签名" />
    4. 更改成        
    5. <meta-data
    6.         android:name="dcloud_appkey"
    7.         android:value="78b2a45f4274af99fa17d63615458ecd" />
    复制代码
  • 8.更改\UniPlugin-Hello-AS\app\src\build.gradle中的内容
    1. applicationId "www.test555.com"        //更改1,www.test555.com为在uniapp后台配置的包名
    2. //signingConfigs中的jks替换成自己生成的
    3. signingConfigs {
    4.     config {
    5.         keyAlias 'myjks'
    6.         keyPassword '123456'
    7.         storeFile file('myjks.jks')
    8.         storePassword '123456'
    9.         v1SigningEnabled true
    10.         v2SigningEnabled true
    11.     }
    12. }
    13. // 添加uni-app插件
    14. dependencies {
    15.   //...
    16.   // 添加uni-app插件
    17.   implementation project(':mylibrary')
    18. }
    复制代码
  • 9.更改\UniPlugin-Hello-AS\app\src\main\assets\dcloud_uniplugins.json中的内容
  1. {
  2.   "nativePlugins": [
  3.     {
  4.       "hooksClass": "",
  5.       "plugins": [
  6.         {
  7.           "type": "module",
  8.           "name": "testPlug",
  9.           "class": "com.example.mylibrary.MyPlug"
  10.         }
  11.       ]
  12.     }
  13.   ]
  14. }
复制代码


  • 10.更改\UniPlugin-Hello-AS\app\src\main\assets\data\dcloud_control.xml中的内容
  1. <hbuilder>
  2. <apps>
  3.     <app appid="__UNI__F089EA2" appver=""/>
  4. </apps>
  5. </hbuilder>
复制代码


  • 11.使用Android studio 天生mylibrary的arr文件

  • 12.uniapp项目引入插件

    • 按照截图的格式创建好文件夹和对应的文件(保持名称划一)

    • package.json的设置如下
    1. {
    2.   "name": "testPlug",
    3.   "id": "testPlug",
    4.   "version": "1",
    5.   "description": "这里是描述",
    6.   "_dp_type":"nativeplugin",
    7.   "_dp_nativeplugin":{
    8.       "android": {
    9.           "plugins": [
    10.               {
    11.                   "type": "module",
    12.                   "name": "testPlug",
    13.                   "class": "com.example.mylibrary.MyPlug"
    14.               }
    15.           ],
    16.           "integrateType": "aar",
    17.           "dependencies": [],
    18.           "compileOptions": {  
    19.               "sourceCompatibility": "11",
    20.               "targetCompatibility": "11"
    21.           },
    22.           "minSdkVersion": "24"
    23.       }
    24.   }
    25. }
    复制代码
      

    • 将11中天生的arr文件放入到nativeplugins\testPlug\android文件夹下
    • uniapp项目要添加本地插件的依赖

  • 13.uniapp项目使用插件
  1. <template>
  2.         <view style="margin: 80rpx 20rpx;">
  3.                 <div style="margin-top: 15rpx;">
  4.                         <button type="primary" @click="test1">领料标签</button>
  5.                 </div>
  6.         </view>
  7. </template>
  8. <script>
  9.         // 1.引入插件
  10.         let testPlugModule = uni.requireNativePlugin("testPlug")
  11.         export default {
  12.                 data() {
  13.                         return {
  14.                                 title: 'Hello'
  15.                         }
  16.                 },
  17.                 onLoad() {
  18.                         this.inits();
  19.                 },
  20.                 methods: {
  21.                         inits() {
  22.                     //2.调用插件方法,不传递参数
  23.                                 testPlugModule.init((res) => {
  24.                                         uni.showToast({
  25.                                                 title: res.data,
  26.                                                 icon: "none"
  27.                                         })
  28.                                 })
  29.                         },
  30.                         test1() {
  31.                                 //缺少原料字段
  32.                                 var beanInfo1 = {
  33.                                         itemId: "12345678",
  34.                                         itemCode: "999",
  35.                                         itemName: "毛豆",
  36.                                         itemBarcode: "0123456789",
  37.                                         chargeBy: "王德发"
  38.                                 }
  39.                                 var jsonBean = JSON.stringify(beanInfo1);
  40.                                 console.log("查看参数信息 jsonBean = ", jsonBean)
  41.                        
  42.                                 this.info1 = jsonBean
  43.                 //3.调用插件方法,传递参数
  44.                                 testPlugModule.printInfo(1,this.width,this.height, jsonBean, (res) => {
  45.                                         uni.showToast({
  46.                                                 title: res.data,
  47.                                                 icon: "none"
  48.                                         })
  49.                                 })
  50.                         },
  51.                 }
  52.         }
  53. </script>
  54. <style>
  55.         .content {
  56.                 display: flex;
  57.                 flex-direction: column;
  58.                 align-items: center;
  59.                 justify-content: center;
  60.         }
  61.         .logo {
  62.                 height: 200rpx;
  63.                 width: 200rpx;
  64.                 margin-top: 200rpx;
  65.                 margin-left: auto;
  66.                 margin-right: auto;
  67.                 margin-bottom: 50rpx;
  68.         }
  69.         .text-area {
  70.                 display: flex;
  71.                 justify-content: center;
  72.         }
  73.         .title {
  74.                 font-size: 36rpx;
  75.                 color: #8f8f94;
  76.         }
  77. </style>
复制代码


  • 14.重新实验步调4,然后测试

三.总结



  • 按照上方流程则可以实现uniapp调用安卓原生插件。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表