RePlugin研究一:基础接入教程

简介

本文介绍RePlugin基础使用方法,宿主和插件如何集成RePlugin具体包括宿主侧调起插件侧的Activity和Service,及插件如何调宿主Activity等.

宿主集成RePlugin

宿主侧只需按照下面3步就可以了:

1.根目录build.gradle添加replugin-host-gradle依赖

build.gradle增加:

1
2
3
4
5
6
buildscript {
dependencies {
classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.4'
...
}
}

2.app/build.gradle应用replugin-host-gradle插件,增加replugin-host-lib依赖

android{}后增加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apply plugin: 'replugin-host-gradle'
/**
* 配置项均为可选配置,默认无需添加
* 更多可选配置项参见replugin-host-gradle的RepluginConfig类
* 可更改配置项参见 自动生成RePluginHostConfig.java
*/
repluginHostConfig {
/**
* 是否使用 AppCompat 库
* 不需要个性化配置时,无需添加
*/
useAppCompat = true
/**
* 背景不透明的坑的数量
* 不需要个性化配置时,无需添加
*/
countNotTranslucentStandard = 6
countNotTranslucentSingleTop = 2
countNotTranslucentSingleTask = 3
countNotTranslucentSingleInstance = 2
}
dependencies {
compile 'com.qihoo360.replugin:replugin-host-lib:2.2.4'
...
}

3.配置Application

最简单方式采用继承式:

1
2
public class MainApplication extends RePluginApplication {
}

之后在AndroidManifest.xml里使用:

1
2
3
<application
android:name=".MainApplication"
... />

当然也可以采用非继承式,即不用RePluginApplication:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);
....
}
}

以上就是宿主侧的集成流程。

插件侧集成

插件侧接入只需要两步:

1.根目录build.gradle添加replugin-plugin-gradl依赖

1
2
3
4
5
6
buildscript {
dependencies {
classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.2.4'
...
}
}

2.app/build.gradle应用replugin-plugin-gradle插件,并添加replugin-plugin-lib依赖:

1
2
3
4
5
6
apply plugin: 'replugin-plugin-gradle'
dependencies {
compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.4'
...
}

插件管理

接下来就要介绍如何把插件和宿主关联起来,可以分为内置插件和外置插件两种类型.

内置插件

定义:随着宿主发布的插件叫内置插件.
接入步骤:将插件xxx.apk改为xxx.jar放到宿主主modulesrc/main/assets/plugins下。

宿主调插件activity

代码如下:

1
2
3
4
5
6
7
public static void startPlugingMainActivity(Context context){
Intent intent = RePlugin.createIntent(PLUGIN_NAME, "com.yanzi.plugin.MainActivity");
Bundle bundle = new Bundle();
bundle.putString("MSG", "您好");
intent.putExtras(bundle);
RePlugin.startActivity(context, intent);
}

宿主调插件service

代码如下:

1
2
3
4
5
6
7
public static void startPluginService(Context context, Bundle bundle){
Intent intent = RePlugin.createIntent(PLUGIN_NAME, "com.yanzi.plugin.MyService");
if(bundle != null){
intent.putExtras(bundle);
}
PluginServiceClient.startService(context, intent);
}

插件调宿主Activity

1
2
3
4
5
6
7
8
9
10
11
private void startHostActivity() {
Intent intent = new Intent();
//第一种写法
//intent.setComponent(new ComponentName(getApplicationContext(), "com.yanzi.host.HostActivity1"));
//第二种写法(官方推荐的)
intent.setComponent(new ComponentName("com.yanzi.host", "com.yanzi.host.HostActivity1"));
startActivity(intent);
}

插件调其他插件的Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 方法1(最“单品”)
Intent intent = new Intent();
intent.setComponent(new ComponentName("demo2",
"com.qihoo360.replugin.sample.demo2.databinding.DataBindingActivity"));
context.startActivity(intent);
// 方法2(快速创建Intent)
Intent intent = RePlugin.createIntent("demo2",
"com.qihoo360.replugin.sample.demo2.databinding.DataBindingActivity");
context.startActivity(intent);
// 方法3(一行搞定)
RePlugin.startActivity(v.getContext(), new Intent(), "demo2",
"com.qihoo360.replugin.sample.demo2.databinding.DataBindingActivity");

注意:

  1. 插件调宿主的Activity不需要借助360replugin lib里的类
  2. 插件调宿主和插件调其他插件是有区别的。
  3. 详见官网wiki

常见问题

  1. 宿主调插件会遇到问题:
    1
    2
    3
    4
    06-03 09:54:55.287 20856-20856/com.yanzi.host E/ws001: Didn't find class "com.qihoo360.replugin.Entry" on path: DexPathList[[zip file "/data/user/0/com.yanzi.host/app_plugins_v3/app-debug-10-10-1.jar"],nativeLibraryDirectories=[/data/user/0/com.yanzi.host/app_plugins_v3_libs/app-debug-10-10-1, /system/lib64, /vendor/lib64, /product/lib64]]
    java.lang.ClassNotFoundException: Didn't find class "com.qihoo360.replugin.Entry" on path: DexPathList[[zip file "/data/user/0/com.yanzi.host/app_plugins_v3/app-debug-10-10-1.jar"],nativeLibraryDirectories=[/data/user/0/com.yanzi.host/app_plugins_v3_libs/app-debug-10-10-1, /system/lib64, /vendor/lib64, /product/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)

这是因为插件没有把com.qihoo360.replugin:replugin-plugin-lib:2.2.4编进去。而不是https://www.jianshu.com/p/ac0082e4b930说的改个插件的theme就好了。

  1. 插件的界面显示灰不拉几的,这是因为插件继承了AppCompatActivity,一定要在宿主侧把开关打开(app/build.gradle):
    1
    2
    3
    4
    5
    6
    7
    8
    //RePlugin 配置start
    apply plugin: 'replugin-host-gradle'
    repluginHostConfig {
    useAppCompat = true
    }
    //RePlugin 配置end

集成总结

  1. 宿主侧要改造Application
  2. 除此外,宿主和插件套路几乎一样,都是在根目录的build.gradle文件里添加各自gradle的依赖,然后在app/build.gradle文件里应用插件(apply plugin xxx)
  3. 至于app这个module是否必须依赖replugin-xxx-lib则不一定,而是在module需要使用对应库的api时才添加依赖.举个例子,宿主侧再创造MyApplication需要继承RePluginApplication,这个RePluginApplication就在com.qihoo360.replugin:replugin-host-lib:2.2.4这个包里。只需让MyApplication所在的module依赖lib库就可以了。
  4. 接3,但是插件和宿主一定要把对应的lib编进去,否则会找不到类而启动不了.
  5. 插件编译成debug和release都是可以的。
  6. 内置插件不需要校验签名!!!
  7. 宿主侧的compile 'com.qihoo360.replugin:replugin-host-lib:2.2.4'是完全可以搞成离线本地依赖的。在http://mvnrepository.com/artifact/com.qihoo360.replugin/replugin-host-lib/2.2.1下载到aar,然后将其放在app module下的libs文件夹下,build.gradle里增加:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.yanzi.host"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
flatDir {
dirs 'libs'
}
}
}
....
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
// compile 'com.qihoo360.replugin:replugin-host-lib:2.2.4'
compile(name: 'replugin-host-lib-2.2.4', ext: 'aar')
}

就可以了。

显示 Gitment 评论