您现在的位置是:首页>文章详情 文章详情 uniapp开发android原生插件(二):引入依赖、开发插件 yangxshn 2021-09-29 6400 ### 一、前言 上一篇文章中说到android原生插件的创建过程,大致看一下插件的目录结构: ![插件目录结构](https://static.web.u7007.com/web/resource/1_4d2feac9464b490e89d251b42c6e93e7.png "插件目录结构") 在开发插件之前我们需要引入一些相关的依赖,依赖会从以下内容体现,依赖相关文件请参考:[官方原生插件开发案例][1] 注意uni-app插件目前仅支持Module扩展和Component扩展,以下开发的插件是Component扩展 ### 二、开发步骤 #### 2.1 引入相关依赖 - 将[官方原生插件开发案例][1]下的uniapp-v8-release.aar文件,复制到 /Plugin-Demo/app/libs 目录下 - 修改myapplication目录下的build.gradle文件,文件内容如下(需要修改的地方有相应的标注): ``` plugins { id 'com.android.library' } android { compileSdk 30 defaultConfig { minSdk 21 targetSdk 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } // 此处需要修改 repositories { flatDir { dirs 'libs' } } // 此处需要修改 dependencies { compileOnly fileTree(dir: 'libs', include: ['*.jar']) compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar']) 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" implementation 'com.alibaba:fastjson:1.1.46.android' implementation 'com.facebook.fresco:fresco:1.13.0' } ``` #### 2.2 开发第一个插件 ``` package com.ali.myapplication; import android.content.Context; import android.graphics.Color; import android.widget.TextView; import java.util.HashMap; import java.util.Map; import io.dcloud.feature.uniapp.UniSDKInstance; import io.dcloud.feature.uniapp.annotation.UniJSMethod; import io.dcloud.feature.uniapp.ui.action.AbsComponentData; import io.dcloud.feature.uniapp.ui.component.AbsVContainer; import io.dcloud.feature.uniapp.ui.component.UniComponent; import io.dcloud.feature.uniapp.ui.component.UniComponentProp; public class TestText extends UniComponent { @Override protected TextView initComponentHostView(Context context) { TextView textView = new TextView(context); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; } // 属性 @UniComponentProp(name = "tel") public void setTel(String telNumber) { getHostView().setText("tel: " + telNumber); Map params = new HashMap<>(); Map number = new HashMap<>(); number.put("tel", telNumber); //目前uni限制 参数需要放入到"detail"中 否则会被清理 params.put("detail", number); fireEvent("onTel", params); } // 方法 @UniJSMethod public void clearTel() { getHostView().setText(""); } @Override public void onActivityResume() { super.onActivityResume(); } @Override public void onActivityPause() { super.onActivityPause(); } @Override public void onActivityDestroy() { super.onActivityDestroy(); } public TestText(UniSDKInstance instance, AbsVContainer parent, int type, AbsComponentData componentData) { super(instance, parent, type, componentData); } public TestText(UniSDKInstance instance, AbsVContainer parent, AbsComponentData componentData) { super(instance, parent, componentData); } } ``` #### 2.3 插件打包 ![插件打包](https://static.web.u7007.com/web/resource/1_d12c63ef9af84a3d9b3923cf2829be4b.png "插件打包") 打包文件在 /Plugin-Demo/myapplication/build/outputs/aar 目录下:myapplication-release.aar ### 三、常见问题 #### 3.1 错误一 ``` Caused by: org.gradle.api.InvalidUserCodeException: Build was configured to prefer settings repositories over project repositories but repository 'flatDir' was added by build file 'myapplication\build.gradle' ``` ![Build was configured to prefer settings repositories over project repositories but repository 'flatDir'](https://static.web.u7007.com/web/resource/1_9750462a451e428aa97b862117826a22.png "Build was configured to prefer settings repositories over project repositories but repository 'flatDir'") **解决方案:删除项目根目录下 settings.gradle 文件中的 dependencyResolutionManagement** #### 3.2 问题二 ``` Execution failed for task ':myapplication:generateReleaseRFile'. > Could not resolve all files for configuration ':myapplication:releaseCompileClasspath'. > Could not find androidx.recyclerview:recyclerview:1.0.0. Searched in the following locations: - file:/G:/myproject/study/Plugin-Demo/myapplication/libs/recyclerview-1.0.0.jar - file:/G:/myproject/study/Plugin-Demo/myapplication/libs/recyclerview.jar Required by: ``` ![问题二](https://static.web.u7007.com/web/resource/1_61ef6711ea3a433a9a5894fa196fde6e.png "问题二") **解决方案:在项目根目录的 build.gradle 文件中添加以下内容:** ``` allprojects { repositories { google() jcenter() } } ``` [1]: https://nativesupport.dcloud.net.cn/AppDocs/download/android "官方原生插件开发案例"