The article directories
Android plugin series of articles
- “Pile-in” plug-in
- The component classes in the plug-in package are different from those in the application
- “Agent” components and “plug-in” components
- “Pile type” plug-in framework implementation ideas
One, “inserted pile” plug-in
Compile and package the “plug-in” module into an APK file. This file contains the classes.dex file, which is packaged in the APK compression package.
As long as you get the APK file, you can decompress it, get the classes.dex file, you can implement a DexClassLoader to load the dex file, and then call the encapsulated bytecode class object;
“Pile-in” pluginization: In a “host” module application, the “plug-in” module APK is inserted into the program to invoke functionality in the apK installation package; In the “host” module, the corresponding agent component interface of the plug-in component should be reserved in advance;
Similar to the computer motherboard, pre-fabricated slots can be inserted into the memory, graphics card and other hardware devices, motherboard running call these devices;
2. The component classes in the plug-in package are different from those in the application
Components in the application:
When the application is running, the plug-in package APK is loaded after it is run. After the dex of the apK file is loaded into the memory, the component classes such as Activity and Service are different from those of the current application. These new component classes have no context.
The initialization of the Activity, Service and other components in the application does not need developer intervention, are completed by the system, these components are registered in the manifest file, the system according to the manifest initialization of related components;
Components in the plug-in package:
The classes in the apK plug-in package are loaded into memory by the DexClassLoader. Only the bytecode data is loaded, the component initialization, and the context is given, which must be completed manually by the developer, regardless of the application system.
The Activity in the loaded plug-in is not an Activity component, but a bytecode class with Activity methods, members, and inheritance relationships.
The plug-in Activity class has no lifecycle and is not managed by The ActivityManagerService.
The life cycle of these component classes, which are loaded through plug-in packages, needs to be managed by the developer;
“Agent” components and “plug-in” components
Agent Activity and PluginActivity:
Using the PluginActivity class loaded in as a normal class, construct a ProxyActivity named ProxyActivity. ProxyActivity holds the PluginActivity class object loaded from the apK plug-in package;
ProxyActivity is an empty Activity that does not have any real business logic and acts as a lifecycle interface for the forwarding agent; But ProxyActivity has a full lifecycle callback mechanism. When entering the interface, it calls back onCreate, onStart, onResume lifecycle methods, and when exiting the interface, it calls back onPause, onStop, and onPause. OnDestroy lifecycle method;
In the lifecycle method of ProxyActivity, call the corresponding lifecycle method of PluginActivity;
ProxyActivity has a context when it runs, and PluginActivity uses that context to invoke the context of ProxyActivity;
Four, “pile type” plug-in framework implementation ideas
“Piling” plug-in framework implementation idea: If you want to load the Activity class in the APK plug-in package compiled by the “plug-in” module, you need to perform the following process;
(1) Load Class objects: Use the DexClassLoader to load Class bytecode objects corresponding to the Activity.
(2) Manage the life cycle: process the life cycle of the Activity class loaded in, create ProxyActivity, through its life cycle callback, agent management plug-in package loaded in the Activity class that is not included in the application management;
(3) Inject context: Inject context for the Activity class loaded;
④ Load resources: use AssetManager to load resources in the plug-in package APK actively;