Android hotfix & plug-in

  • Definition of plug-in

Pluginization refers to the process of integrating a function into a system or application in the form of a plug-in. In layman's terms, it is to separate certain functions of the application from the original application, package and deploy them as independent plug-ins, so that users can dynamically add, update or delete these plug-ins in the original application without modifying the original application and recompile.

  • Advantages of plug-in

  1. Modular development and maintenance: By decomposing different functions in the application into independent modules, developers can focus more on the implementation and maintenance of each module, thereby improving code quality and maintainability.

  2. Dynamic update and upgrade: Some functional modules in the application can be updated and upgraded independently without affecting the release and deployment of the entire application, greatly improving the reliability and flexibility of the application.

  3. Reduce memory consumption and compilation time: Since the plug-in technology can dynamically load some functions of the application in the form of plug-ins, only the necessary plug-ins need to be loaded at runtime, which avoids unnecessary memory consumption and reduces the number of applications. The time the program was compiled.

  4. Improve user experience: Through plug-in technology, some functional modules of the application can be packaged, downloaded, installed and uninstalled separately in the form of plug-ins, which brings better user experience to users and increases user stickiness.

  5. Save development costs: Through plug-in technology, different functional modules of the application can be developed in parallel by different developers or teams, so as to improve development efficiency and save development costs.

  • Plug-in framework comparison

Currently popular Android plug-in frameworks mainly include: VirtualApk, DroidPlugin, DynamicAPK, Ranger, Small, RePlugin, etc.

The following is a comparison of several major plug-in frameworks:

  1. Virtual Apk

VirtualApk is a plug-in framework open sourced by Meituan Dianping. Its biggest advantage lies in its excellent compatibility and stability. It supports plug-ins and hosts to share processes, and can avoid performance problems caused by process communication. At the same time, VirtualApk also provides rich plug-in lifecycle management, resource isolation and other functions, which can realize sophisticated plug-in running status monitoring and exception response mechanism.

  1. DroidPlugin

DroidPlugin is developed and open sourced by 360 Mobile Guard. It is a full-featured plug-in framework with perfect integration, usage and documentation. The framework can flexibly control the process, permissions, upgrades, priorities, etc. of each plug-in, and makes the plug-in startup easier and faster through the APK loading method.

  1. DynamicAPK

DynamicAPK is a community-contributed plug-in framework dedicated to improving the dynamic characteristics and component-based development of Android applications. The framework supports a variety of plug-in loading modes, supports the extension of components such as Activity, Service, and Broadcast, and has relatively friendly documents and sample codes.

  1. Ranger

Ranger is a domestic plug-in framework developed and promoted by Xiaomi. The framework is streamlined, lightweight and fast, and the control panel is relatively simple and easy to use. It can realize functions such as asynchronous loading and unloading of plug-ins, and also provides a complete debugging mechanism such as plug-in exception information reporting and CRASH reporting.

  1. Small

Small is a plug-in framework based on microkernel architecture, produced and promoted by Alibaba. The framework can freely add and delete plug-ins in the application, and can isolate the plug-in and the host. The startup switching is frequent and the performance is excellent.

  1. RePlugin

RePlugin is another excellent plug-in framework produced by Ctrip. Similar to VirtualApk, its advantages lie in its excellent performance in compatibility, stability and operating efficiency. The Model plug-in and View plug-in of the framework use different class loaders to achieve isolation, support the separation of framework and logic, and have excellent performance in multi-threading and concurrent access.

  • Plug-in process

  1. Develop plugin code

First of all, it is necessary to develop an independent plug-in, that is, an APK package, and then develop and test it after pulling through the project. During the development process, the interaction between the plug-in and the host must be considered, and global variables, singletons, statics, etc. should be avoided as much as possible to isolate the plug-in from the host.

  1. Host access plug-in framework

By referring to the plug-in framework library, the dynamic loading of the plug-in APK package is realized within the system. The plug-in framework requires the host program to customize ClassLoader and Resource to replace the original ClassLoader and Resource of the system. Common plug-in frameworks include the above-mentioned VirtualApk, DynamicAPK, RePlugin, etc.

  1. Plug-in packaging

Since the plug-in and the host belong to two different APK packages, the plug-in needs to be packaged into an apk and signed. This involves the issue of "v1" and "v2" signatures, which correspond to the two packaging methods "debug" and "release" in Android Studio.

  1. Upload the plugin to the server

The packaged and signed plug-in can be directly uploaded to the server for download and installation, or it can be packaged into a "patch" package to store the update information on the server, and realize real-time download and update according to user needs.

  1. Download and load plugins

When the application starts, the host will download or obtain the plug-in to be loaded from the server as needed, decompress it and store it in a certain location on the device. At the same time, a caching strategy can be adopted to cache downloaded plug-ins to avoid repeated network requests and traffic consumption.

  1. run plugin

After the plug-in is loaded, it needs to perform some necessary initialization operations, such as setting a custom class loader, Context context, etc., and finally start components such as Activity or Service to run through Intent, and pass the returned result back to the host.

  • Plug-in class loading principle

  1. Shared ClassLoader implementation

This is the simplest and most common plug-in class loading method, which is often used for dynamic loading of Dex files. This method needs to create a shared ClassLoader in the host program and pass it to the host and the plug-in. When an Activity or Service is started, it will first search for the corresponding class information from the ClassLoader.

The plug-in part obtains the reference of the host ClassLoader (that is, the shared ClassLoader) by calling Activity.getClassLoader(), calls the makeDexElements method in DexPathList through reflection to obtain all the Class objects of the current Dex, and finally inserts them into "gDefault"

Disadvantages: It is prone to problems such as class naming conflicts and resource exhaustion leading to crashes.

  1. Custom ClassLoader implementation

In order to avoid problems related to shared ClassLoader, many plug-in frameworks choose to use a custom ClassLoader to load plug-ins, and load each plug-in with an independent ClassLoader to ensure the isolation between the plug-in and the host.

This method is more flexible than the previous method, and can better implement functions such as resource isolation and plug-in lifecycle control. At the same time, this method can also realize the modification and extension of the system class through the Hook system ClassLoader.

Disadvantages: Some detailed issues need to be dealt with, such as the plug-in cannot share host resources and system resources, the startup time is often relatively long, and the third-party library used in the adaptation plug-in, etc.

Guess you like

Origin blog.csdn.net/slave_of_life/article/details/130787491