Android插件化开发 第五篇 [360 Droid Plugin]
2016-08-17 13:14
471 查看
上篇文章为止我们有完整的介绍Android插件开发的流程。简单的回顾一下就是通过系统的ClassLoader加载插件apk里的方法,通过反射与插件交互。说起来容易实现起来却是一步一个坑,所以这篇文章起我们会了解一下目前网上流行的开源插件库。
本文介绍DroidPlugin,它是360手机助手团队在Android系统上实现了一种插件机制。它可以在无需安装、修改的情况下运行APK文件,此机制对改进大型APP的架构,实现多团队协作开发具有一定的好处。
其特点有:
1. 支持Androd 2.3以上系统
2. 插件APK完全不需做任何修改,可以独立安装运行、也可以做插件运行。要以插件模式运行某个APK,你无需重新编译、无需知道其源码。
3. 插件的四大组件完全不需要在Host程序中注册,支持Service、Activity、BroadcastReceiver、ContentProvider四大组件
4. 插件之间、Host程序与插件之间会互相认为对方已经”安装”在系统上了。
5. API低侵入性:极少的API。HOST程序只是需要一行代码即可集成Droid Plugin
6. 超强隔离:插件之间、插件与Host之间完全的代码级别的隔离:不能互相调用对方的代码。通讯只能使用Android系统级别的通讯方法。
7. 支持所有系统API
8. 资源完全隔离:插件之间、与Host之间实现了资源完全隔离,不会出现资源窜用的情况。
9. 实现了进程管理,插件的空进程会被及时回收,占用内存低。
10. 插件的静态广播会被当作动态处理,如果插件没有运行(即没有插件进程运行),其静态广播也永远不会被触发。
限制和缺陷:
1. 加载时界面存在明显的等待。(参见360市场管理界面中的”手机清理”功能)
2. 无法在插件中发送具有自定义资源的Notification,例如: a. 带自定义RemoteLayout的Notification b. 图标通过R.drawable.XXX指定的通知(插件系统会自动将其转化为Bitmap)
3. 无法在插件中注册一些具有特殊Intent Filter的Service、Activity、BroadcastReceiver、ContentProvider等组件以供Android系统、已经安装的其他APP调用。
4. 缺乏对Native层的Hook,对某些带native代码的apk支持不好,可能无法运行。比如一部分游戏无法当作插件运行。
在Host中集成Droid Plugin项目非常简单:
1. 我们只需要将Droid Plugin当作一个lib工程应用到主项目中,然后:
2. 在AndroidManifest.xml中使用插件的com.morgoo.droidplugin.PluginApplication:
如果你使用自定义的Application,那么你需要在自定义的Application class onCreate和attachBaseContext方法中添加如下代码:
将插件中Libraries\DroidPlugin\AndroidManifest.xml中所有的provider对应的authorities修改成自己的,默认为com.morgoo.droidplugin_stub_P00,如下:
可以修改为自己的包名,如: com.example.droidplugin_stub_P00防止跟其它本插件使用者冲突:
并且修改PluginManager.java中的PluginManager.STUB_AUTHORITY_NAME为你的值:
至此宿主Host修改完毕,使用Android Studio的同学如果不明白如何”当作一个lib工程应用到主项目中“请在文章结尾留言,我会单独开篇文章讲解。另外我们发现DroidPlugin在Manifest中声明了许多许多的权限,其实我们可以删除掉插件中不需要的权限。
Host集成完毕后,我们可以通过以下一句代码加载插件:
说明:安装插件到插件系统中,filepath为插件apk路径,flags可以设置为0,如果要更新插件,则设置为PackageManagerCompat.INSTALL_REPLACE_EXISTING返回值及其含义请参见PackageManagerCompat类中的相关字段。
卸载插件的代码如下:
说明:从插件系统中卸载某个插件,packageName传插件包名即可,flags传0。
启动插件的Activity、Service等都和你启动一个以及安装在系统中的app一样,使用系统提供的相关API即可。组件间通讯也是如此。如Activity的隐式调用:
最后简单的来说,使用360的DroidPlugin可以很方便容易的把自己现有项目改造成插件。但是注意插件的功能一定要相对独立,且Lite,否则数据交互及加载等待都会让你抓狂。
本文介绍DroidPlugin,它是360手机助手团队在Android系统上实现了一种插件机制。它可以在无需安装、修改的情况下运行APK文件,此机制对改进大型APP的架构,实现多团队协作开发具有一定的好处。
其特点有:
1. 支持Androd 2.3以上系统
2. 插件APK完全不需做任何修改,可以独立安装运行、也可以做插件运行。要以插件模式运行某个APK,你无需重新编译、无需知道其源码。
3. 插件的四大组件完全不需要在Host程序中注册,支持Service、Activity、BroadcastReceiver、ContentProvider四大组件
4. 插件之间、Host程序与插件之间会互相认为对方已经”安装”在系统上了。
5. API低侵入性:极少的API。HOST程序只是需要一行代码即可集成Droid Plugin
6. 超强隔离:插件之间、插件与Host之间完全的代码级别的隔离:不能互相调用对方的代码。通讯只能使用Android系统级别的通讯方法。
7. 支持所有系统API
8. 资源完全隔离:插件之间、与Host之间实现了资源完全隔离,不会出现资源窜用的情况。
9. 实现了进程管理,插件的空进程会被及时回收,占用内存低。
10. 插件的静态广播会被当作动态处理,如果插件没有运行(即没有插件进程运行),其静态广播也永远不会被触发。
限制和缺陷:
1. 加载时界面存在明显的等待。(参见360市场管理界面中的”手机清理”功能)
2. 无法在插件中发送具有自定义资源的Notification,例如: a. 带自定义RemoteLayout的Notification b. 图标通过R.drawable.XXX指定的通知(插件系统会自动将其转化为Bitmap)
3. 无法在插件中注册一些具有特殊Intent Filter的Service、Activity、BroadcastReceiver、ContentProvider等组件以供Android系统、已经安装的其他APP调用。
4. 缺乏对Native层的Hook,对某些带native代码的apk支持不好,可能无法运行。比如一部分游戏无法当作插件运行。
Demo创建
在Host中集成Droid Plugin项目非常简单: 1. 我们只需要将Droid Plugin当作一个lib工程应用到主项目中,然后:
2. 在AndroidManifest.xml中使用插件的com.morgoo.droidplugin.PluginApplication:
<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><application android:name=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.morgoo.droidplugin.PluginApplication"</span> android:label=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@string</span>/app_name"</span> android:icon=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@drawable</span>/ic_launcher"</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
如果你使用自定义的Application,那么你需要在自定义的Application class onCreate和attachBaseContext方法中添加如下代码:
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//这里必须在super.onCreate方法之后,顺序不能变</span> PluginHelper.getInstance().applicationOnCreate(getBaseContext()); } <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">attachBaseContext</span>(Context base) { PluginHelper.getInstance().applicationAttachBaseContext(base); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.attachBaseContext(base); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
将插件中Libraries\DroidPlugin\AndroidManifest.xml中所有的provider对应的authorities修改成自己的,默认为com.morgoo.droidplugin_stub_P00,如下:
<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><provider android:name=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.morgoo.droidplugin.stub.ContentProviderStub<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$StubP00</span>"</span> android:authorities=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.morgoo.droidplugin_stub_P00"</span> android:exported=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"false"</span> android:label=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@string</span>/stub_name_povider"</span> /></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
可以修改为自己的包名,如: com.example.droidplugin_stub_P00防止跟其它本插件使用者冲突:
<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><provider android:name=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.morgoo.droidplugin.stub.ContentProviderStub<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$StubP00</span>"</span> android:authorities=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.example.droidplugin_stub_P00"</span> android:exported=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"false"</span> android:label=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@string</span>/stub_name_povider"</span> /></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
并且修改PluginManager.java中的PluginManager.STUB_AUTHORITY_NAME为你的值:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">PluginManager<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.STUB</span>_AUTHORITY_NAME=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"com.example.droidplugin_stub"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
至此宿主Host修改完毕,使用Android Studio的同学如果不明白如何”当作一个lib工程应用到主项目中“请在文章结尾留言,我会单独开篇文章讲解。另外我们发现DroidPlugin在Manifest中声明了许多许多的权限,其实我们可以删除掉插件中不需要的权限。
讲解
Host集成完毕后,我们可以通过以下一句代码加载插件:<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">PluginManager<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getInstance</span>()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.installPackage</span>(String filepath, int flags)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
说明:安装插件到插件系统中,filepath为插件apk路径,flags可以设置为0,如果要更新插件,则设置为PackageManagerCompat.INSTALL_REPLACE_EXISTING返回值及其含义请参见PackageManagerCompat类中的相关字段。
卸载插件的代码如下:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">PluginManager<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getInstance</span>()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.deletePackage</span>(String packageName,int flags)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
说明:从插件系统中卸载某个插件,packageName传插件包名即可,flags传0。
启动插件的Activity、Service等都和你启动一个以及安装在系统中的app一样,使用系统提供的相关API即可。组件间通讯也是如此。如Activity的隐式调用:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">Intent intent = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Intent(); intent.setAction(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"h3c.pluginb.Main"</span>); startActivity(intent);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
总结
最后简单的来说,使用360的DroidPlugin可以很方便容易的把自己现有项目改造成插件。但是注意插件的功能一定要相对独立,且Lite,否则数据交互及加载等待都会让你抓狂。
相关文章推荐
- Android插件化开发 第五篇 [360 Droid Plugin]
- 360 Android 插件开发 DroidPlugin 代码分析 -随笔
- Android插件化框架研究-DroidPlugin
- 插件开发之360 DroidPlugin源码分析(一)初识
- 插件开发之360 DroidPlugin源码分析(一)初识
- 插件化开发---DroidPlugin对Servie的管理
- 插件开发之360 DroidPlugin源码分析(五)Service预注册占坑
- 360 Android 插件项目 DroidPlugin
- 插件开发之360 DroidPlugin源码分析(三)Binder代理
- 插件开发之360 DroidPlugin源码分析(二)Hook机制
- 插件开发之360 DroidPlugin源码分析(四)Activity预注册占坑
- 插件化开发---DroidPlugin对广播的管理
- 插件开发之360 DroidPlugin源码分析(二)Hook机制
- 应用插件化实践--DroidPlugin插件 在Eclipse上开发Demo
- 插件开发之360 DroidPlugin源码分析(三)Binder代理
- Android插件实例——360 DroidPlugin详解
- [Android 插件化(二)] DroidPlugin 用法
- 插件开发之360 DroidPlugin源码分析(二)Hook机制
- DroidPlugin插件化开发
- 插件开发之360 DroidPlugin源码分析(四)Activity预注册占坑