插件化分为两种项目,1.宿主app(用来安装插件),2.插件app
1.宿主app集成:
(1) project 下 build.gradle 中添加
repositories {
maven {
url "https://dl.bintray.com/qihoo360/replugin"
}
}
dependencies {
classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.4'
}导入了上述依赖库之后,进行同步项目时,遇到了如下问题:
Gradle sync failed: No signature ofmethod:com.android.build.gradle.
internal.scope.VariantScopeImpl.getMergeAssetsTask() is applicable for argument types: () values: []。
这是因为 Google 对 3.2.0 版本之后的 Gradle 构建工具做了一些修改,RePlugin 官方还没有对此用兼容性处理。
解决方法也很简单,把项目根目录的 build.gradle(注意:不是 app/build.gradle)中的 build:gradle版本改为3.1.2
classpath 'com.android.tools.build:gradle:3.1.2'(2) app 下 build.gradle 中添加
dependencies {
implementation 'com.qihoo360.replugin:replugin-host-lib:2.2.4'}
}
// 在 android{} 下方加上 (防止加载时还没取applicationId)
apply plugin: 'replugin-host-gradle'
(3)创建一个Application 如果你的工程已有 Application 类,则可以将基类切换到 RePluginApplication 即可。
public class MainApplication extends RePluginApplication { }或者你也可以用“非继承式”接入
public class MainApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
RePlugin.App.attachBaseContext(this);
....
}
@Override
public void onCreate() {
super.onCreate();
RePlugin.App.onCreate();
....
}
@Override
public void onLowMemory() {
super.onLowMemory();
/* Not need to be called if your application's minSdkVersion > = 14 */
RePlugin.App.onLowMemory();
....
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
/* Not need to be called if your application's minSdkVersion > = 14 */
RePlugin.App.onTrimMemory(level);
....
}
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
/* Not need to be called if your application's minSdkVersion > = 14 */
RePlugin.App.onConfigurationChanged(config);
....
}
}
既然声明了 Application,自然还需要在 AndroidManifest 中配置这个 Application。
<application
android:name=".MainApplication"
... />
针对“非继承式”的注意点:
所有方法必须在 UI 线程来“同步”调用。切勿放到工作线程,或者通过 post 方法来执行所有方法必须一一对应,例如 RePlugin.App.attachBaseContext() 方法只在 Application.attachBaseContext() 中调用请将 RePlugin.App 的调用方法,放在“仅次于 super.xxx()”方法的后面
2.插件app集成project 下 build.gradle 中 添加
dependencies {
classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.2.4'
}
app下 build.gradle 中添加
dependencies {
implementation 'com.qihoo360.replugin:replugin-plugin-lib:2.2.4'
}
//android {}下方添加
apply plugin: 'replugin-plugin-gradle'
集成完毕.
接下来 --> 安装插件
1.外部插件安装方法
//插件下载下来的绝对地址
PluginInfo install = RePlugin.install("/storage/sdcard0/apk_包/Module1.apk");
//如果为nill则没有安装成功
Log.i(TAG,install);
//判断是否安装了插件 plugName 别名
if (RePlugin.isPluginInstalled("com.*.module1")) {
//"com.*.module1.MainActivity" 为要跳转组件的路径
RePlugin.startActivity(this,RePlugin.createIntent("com.business.module1", "com.*.module1.MainActivity"));
}部插件安装
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.myreplugintest2"><application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
== android:exported="true"==>
<activity android:name=".MainActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
== <!-插件名称->
<meta-data
android:name="com.my.myreplugintest2.name"
android:value="plugin1"/>
<!-插件版本->
<meta-data
android:name="com.my.myreplugintest2.version.ver"
android:value="1"/>==
</application>
</manifest>
设置成暴露exported=“true”,然后设置别名
说明:meta-data下的name是包名+name,包名+version.var
然后打成apk
(内置于 APP 之中,并随 APP 一并发版,需要将插件 apk 改成 .jar 结尾放入主程序的assets/plugins目录。)
7:主程序运行
在主程序中与java文件同级,创建assets/plugins目录,把打好的apk修改名字为 plugin1.jar(plugin1是我的别名)
RePlugin.startActivity(MainActivity.this, RePlugin.createIntent(“plugin1”, “com.my.myreplugintest2.MainActivity”));
plugin1为插件别名。com.my.myreplugintest2.MainActivity是插件文件的位置。