1.新建两个module,plugins 和 host,然后在project的build.gradle添加对应的依赖,可在同一个项目也可分两个项目,此处图省事丢到同一个project
classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.4'
classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.2.4'
2.在app plugins的build.gradle添加
compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.4'
compile 'com.android.support:appcompat-v7:25.3.1'
然后设置
// 这个plugin需要放在android配置之后,因为需要读取android中的配置项
apply plugin: 'replugin-plugin-gradle'
repluginPluginConfig {
//插件名
pluginName = "test"
//宿主app的包名
hostApplicationId = "com.lei.simpletest.host"
//宿主app的启动activity
hostAppLauncherActivity = "com.lei.simpletest.host.MainActivity"
}
3.app host的build.gradle添加
compile 'com.qihoo360.replugin:replugin-host-lib:2.2.4'
compile 'com.android.support:appcompat-v7:25.3.1'
然后
// ATTENTION!!! Must be PLACED AFTER "android{}" to read the applicationId
apply plugin: 'replugin-host-gradle'
/**
* 配置项均为可选配置,默认无需添加
* 更多可选配置项参见replugin-host-gradle的RepluginConfig类
* 可更改配置项参见 自动生成RePluginHostConfig.java
*/
repluginHostConfig {
/**
* 是否使用 AppCompat 库
* 不需要个性化配置时,无需添加
*/
useAppCompat = true
/**
* 背景不透明的坑的数量
*/
countNotTranslucentStandard = 6
countNotTranslucentSingleTop = 2
countNotTranslucentSingleTask = 3
countNotTranslucentSingleInstance = 2
}
4.把plugins的app跑一个debug.apk或者签名包,改名为pluginName填写的插件名.jar,然后放到host app的main/assets/plugins中我的是test.jar
5.调用插件的主界面
RePlugin.startActivity(MainActivity.this,RePlugin.createIntent("test",
"com.lei.RePlugin.MainActivity"));
6.坑
bug1
Error:No resource found that matches the given name (at 'theme' with value '@style/Theme.AppCompat').
应该是主题的原因,据说 useAppCompat = true可以关闭,果断干掉,然而并没有卵用
bug2
FATAL EXCEPTION: main
Process: com.lei.simpletest.host, PID: 6962
java.lang.NoClassDefFoundError: android.support.v4.content.LocalBroadcastManager
at com.qihoo360.loader2.PmBase.registerReceiverAction(PmBase.java:632)
at com.qihoo360.loader2.PmBase.callAppCreate(PmBase.java:622)
at com.qihoo360.loader2.PMF.callAppCreate(PMF.java:84)
at com.qihoo360.replugin.RePlugin$App.onCreate(RePlugin.java:1002)
at com.qihoo360.replugin.RePluginApplication.onCreate(RePluginApplication.java:74)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4384)
at android.app.ActivityThread.access$1500(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
看信息是缺360的插件缺v4包,v7包默认引用这个,然后把注释的useAppCompat = true放开,直接依赖v7包 解决
compile 'com.android.support:appcompat-v7:25.3.1'
bug3 特别莫名其妙
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lei.simpletest.host, PID: 1236
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lei.simpletest.host/com.lei.simpletest.host.loader.a.ActivityN1NRNTS3}: android.content.res.Resources$NotFoundException: File res/layout/activity_main.xml from drawable resource ID #0x7f030000
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2235)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)
at android.app.ActivityThread.access$800(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/layout/activity_main.xml from drawable resource ID #0x7f030000
at android.content.res.Resources.loadDrawable(Resources.java:2097)
at android.content.res.Resources.getDrawable(Resources.java:700)
at com.android.internal.widget.ActionBarView.setIcon(ActionBarView.java:699)
at com.android.internal.policy.impl.PhoneWindow.setDefaultIcon(PhoneWindow.java:1446)
at android.app.Activity.initActionBar(Activity.java:1915)
at android.app.Activity.setContentView(Activity.java:1930)
at com.lei.RePlugin.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)
at android.app.ActivityThread.access$800(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag RelativeLayout
plugins的build.gradle添加v7包依赖解决,对着官方代码瞅出来的,我也不知道为啥
反正能使了