1.新建兩個(gè)module死宣,plugins 和 host搓幌,然后在project的build.gradle添加對(duì)應(yīng)的依賴,可在同一個(gè)項(xiàng)目也可分兩個(gè)項(xiàng)目育苟,此處圖省事丟到同一個(gè)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'
然后設(shè)置
// 這個(gè)plugin需要放在android配置之后筹裕,因?yàn)樾枰x取android中的配置項(xiàng)
apply plugin: 'replugin-plugin-gradle'
repluginPluginConfig {
//插件名
pluginName = "test"
//宿主app的包名
hostApplicationId = "com.lei.simpletest.host"
//宿主app的啟動(dòng)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'
/**
* 配置項(xiàng)均為可選配置醋闭,默認(rèn)無(wú)需添加
* 更多可選配置項(xiàng)參見(jiàn)replugin-host-gradle的RepluginConfig類
* 可更改配置項(xiàng)參見(jiàn) 自動(dòng)生成RePluginHostConfig.java
*/
repluginHostConfig {
/**
* 是否使用 AppCompat 庫(kù)
* 不需要個(gè)性化配置時(shí)窄驹,無(wú)需添加
*/
useAppCompat = true
/**
* 背景不透明的坑的數(shù)量
*/
countNotTranslucentStandard = 6
countNotTranslucentSingleTop = 2
countNotTranslucentSingleTask = 3
countNotTranslucentSingleInstance = 2
}
4.把plugins的app跑一個(gè)debug.apk或者簽名包朝卒,改名為pluginName填寫的插件名.jar,然后放到host app的main/assets/plugins中我的是test.jar
5.調(diào)用插件的主界面
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').
應(yīng)該是主題的原因乐埠,據(jù)說(shuō) useAppCompat = true可以關(guān)閉抗斤,果斷干掉,然而并沒(méi)有卵用
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包默認(rèn)引用這個(gè)丈咐,然后把注釋的useAppCompat = true放開(kāi)瑞眼,直接依賴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包依賴解決,對(duì)著官方代碼瞅出來(lái)的棵逊,我也不知道為啥
反正能使了