Android Plug-in Development Chapter 5 [360 Droid Plugin]

introduction


So far in the last article, we have fully introduced the process of Android plug-in development. A brief review is the method of loading the plug-in apk through the system's ClassLoader, and interacting with the plug-in through reflection. It's easy to say, but it's a pitfall step by step, so starting from this article, we will learn about the open source plug-in libraries that are currently popular on the Internet.

This article introduces DroidPlugin , which is a plug-in mechanism implemented by the 360 ​​mobile assistant team on the Android system. It can run APK files without installation or modification. This mechanism has certain benefits for improving the architecture of large-scale APPs and realizing multi-team collaborative development.

Its features are:
1. Support Androd 2.3 and above systems
2. The plug-in APK does not need to be modified at all, and can be installed and run independently or as a plug-in. To run an APK in plugin mode, you don't need to recompile it, and you don't need to know its source code.
3. The four major components of the plug-in do not need to be registered in the Host program at all, and support the four major components of Service, Activity, BroadcastReceiver, and ContentProvider
. up.
5. API low intrusion: very few APIs. The HOST program only needs one line of code to integrate Droid Plugin
6. Super isolation: complete code-level isolation between plug-ins and between plug-ins and Host: you cannot call each other's code. Communication can only use Android system-level communication methods.
7. Support all system APIs
8. Complete resource isolation: Complete resource isolation is realized between plug-ins and between the host, and there will be no resource misuse.
9. Implemented process management, the empty process of the plug-in will be recycled in time, and the memory usage is low.
10. The static broadcast of the plug-in will be treated as dynamic. If the plug-in is not running (that is, no plug-in process is running), its static broadcast will never be triggered.

Limitations and bugs:
1. There is an obvious wait in the interface when loading. (Refer to the "Mobile Phone Cleanup" function in the 360 ​​market management interface)
2. Notifications with custom resources cannot be sent in plugins, for example: a. Notifications with custom RemoteLayout b. Notifications whose icons are specified by R.drawable.XXX (The plug-in system will automatically convert it into a Bitmap)
3. Some components such as Service, Activity, BroadcastReceiver, and ContentProvider with special Intent Filter cannot be registered in the plug-in for the Android system and other installed APPs to call.
4. Lack of Hooks for the Native layer, poor support for some apks with native code, and may not be able to run. For example, some games cannot be run as plug-ins.

Demo creation


It is very simple to integrate the Droid Plugin project in Host:
1. We only need to apply Droid Plugin as a lib project to the main project, and then: 2. Use the plugin com.morgoo.droidplugin.PluginApplication
in AndroidManifest.xml :

<application android:name="com.morgoo.droidplugin.PluginApplication" 
             android:label="@string/app_name"
             android:icon="@drawable/ic_launcher" 
  1. If you use a custom Application , then you need to add the following code in the custom Application class  onCreate and attachBaseContext methods:
@Override
public void onCreate() {
    super.onCreate();
    //这里必须在super.onCreate方法之后,顺序不能变
    PluginHelper.getInstance().applicationOnCreate(getBaseContext());
}
@Override
protected void attachBaseContext(Context base) {
    PluginHelper.getInstance().applicationAttachBaseContext(base);
    super.attachBaseContext(base);
}
  1. Change the authorities corresponding to all providers in Libraries\DroidPlugin\AndroidManifest.xml in the plugin to your own, the default is com.morgoo.droidplugin_stub_P00 , as follows:
<provider
    android:name="com.morgoo.droidplugin.stub.ContentProviderStub$StubP00"
    android:authorities="com.morgoo.droidplugin_stub_P00"
    android:exported="false"
    android:label="@string/stub_name_povider" />

You can modify it to your own package name, such as:  com.example.droidplugin_stub_P00 to prevent conflicts with other users of this plugin:

<provider
    android:name="com.morgoo.droidplugin.stub.ContentProviderStub$StubP00"
    android:authorities="com.example.droidplugin_stub_P00"
    android:exported="false"
    android:label="@string/stub_name_povider" />

And modify PluginManager.STUB_AUTHORITY_NAME in PluginManager.java to your value:

PluginManager.STUB_AUTHORITY_NAME="com.example.droidplugin_stub"

So far, the modification of the host host is completed. If students who use Android Studio do not understand how to " apply it as a lib project to the main project ", please leave a message at the end of the article, and I will start a separate article to explain it. In addition, we found that DroidPlugin declares many permissions in Manifest. In fact, we can delete unnecessary permissions in the plugin.

explain


After the Host integration is complete, we can load the plugin through the following line of code:

PluginManager.getInstance().installPackage(String filepath, int flags)

Note: Install the plugin into the plugin system, filepath is the plugin apk path, flags can be set to 0, if you want to update the plugin, set it to PackageManagerCompat.INSTALL_REPLACE_EXISTING For the return value and its meaning, please refer to the relevant fields in the PackageManagerCompat class.

The code to uninstall the plugin is as follows:

PluginManager.getInstance().deletePackage(String packageName,int flags);

Note: To uninstall a certain plugin from the plugin system, just pass the package name of the plugin as packageName, and 0 for flags.

The Activity, Service, etc. that start the plug-in are the same as you start an app installed in the system, just use the relevant API provided by the system. The same is true for inter-component communication. Such as the implicit call of Activity:

Intent intent = new Intent();
intent.setAction("h3c.pluginb.Main");
startActivity(intent);

Summarize


Finally, to put it simply, using 360's DroidPlugin can easily transform your existing projects into plug-ins. But note that the function of the plug-in must be relatively independent and Lite , otherwise data interaction and loading waiting will drive you crazy.

In the next article, we will learn about the Small of the wequick team

Guess you like

Origin blog.csdn.net/h3c4lenovo/article/details/50751643