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包依賴解決,對著官方代碼瞅出來的,我也不知道為啥
反正能使了