一、前言上一篇文章中说到android原生插件的创建过程,大致看一下插件的目录结构:
在开发插件之前我们需要引入一些相关的依赖,依赖会从以下内容体现,依赖相关文件请参考:官方原生插件开发案例
注意uni-app插件目前仅支持Module扩展和Component扩展,以下开发的插件是Component扩展
二、开发步骤2.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<TextView> { @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<String, Object> params = new HashMap<>(); Map<String, Object> 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 插件打包
打包文件在 /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'
解决方案:删除项目根目录下 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:
解决方案:在项目根目录的 build.gradle 文件中添加以下内容:
allprojects { repositories { google() jcenter() } }
|