[Android plug-in] "Plug-in" plug-in framework (principles and realization ideas)


Android plug-in series of articles directory

  1. "Plug-in" plug-in
  2. The difference between the component class in the plug-in package and the component class in the application
  3. "Agent" component and "plug-in" component
  4. Ideas for the realization of "plug-in" plug-in framework

1. "Plug-in" plug-in

The "plug-in" module is compiled into a package apk file contains classes.dex file, it is packaged in a compressed package apk;

As long as you get the apk file, you can decompress it, get the classes.dex file, and implement a DexClassLoader to load the dex file, and then call the bytecode class object encapsulated in it;

"Instrumentation" type of plug : the "host" module application, the "plug-in" module apk inserted into the program, can be invoked apk installation feature package; "host" to be reserved well in advance module plug-in componentcorresponds to the proxy component interfaces;

Similar to a computer motherboard, the slot is prefabricated in advance, you can insert hardware devices such as memory sticks, graphics cards, etc., and call these devices when the motherboard is running;

Second, the difference between the component class in the plug-in package and the component class in the application

Components in the application:

When the application is running, add-on pack apk is run after loading , the dex apk file is loaded into memory, after which the Activity, Service and other components like, and application components that are currently running is different, these new entrants The component class has no context;

The initialization of the Activity, Service and other components in the application does not require developer intervention . They are all completed by the system . These components are registered in the manifest file, and the system initializes the relevant components according to the list;

Components in the plug-in package:

Apk plug-in package classes, is loaded into memory by DexClassLoader, simply by loading a byte code data , the initialization component, imparting context, etc., must be completed manually by the developer , the system is a matter of application;

Loaded plug-in component Activity Activity not only have the bytecode class Activity method, a member of the inheritance;

The plug-in Activity class has no life cycle and is not within the scope of ActivityManagerService management;

The life cycle of these component classes loaded through the plug-in package needs to be managed by the developer;

3. "Agent" component and "plug-in" component

Proxy Activity (ProxyActivity) and Plugin Activity (PluginActivity ):

Loaded incoming Activity class (PluginActivity) as a normal class, a configuration agent Activity , named ProxyActivity, in ProxyActivity the holder from the loaded object class PluginActivity apk widget package;

ProxyActivity is empty Activity, without any real business logic, just as a forwarding agent interface life cycle; however ProxyActivity have a complete life cycle callback mechanism when entering the interface will callback onCreate, onStart, onResume life-cycle approach, When exiting the interface, the onPause, onStop, onDestroy life cycle methods will be called back;

In the life cycle method in ProxyActivity, call the corresponding life cycle method of PluginActivity;

ProxyActivity will have a context when it runs, and PluginActivity will call the context of ProxyActivity when it uses the context;

Fourth, the realization of the "plug-in" plug-in framework

"Plug-in" plug-in framework implementation ideas: 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;

① Load class object: Use DexClassLoader to load the Class bytecode class object corresponding to Activity;

② Manage life cycle: process the life cycle of the loaded Activity class, create ProxyActivity, and through its life cycle callback, proxy the activity classes loaded in the plug-in package that are not included in application management;

③ Inject context: inject context into the loaded Activity class;

④ Load resources: Use AssetManager to actively load the resources in the plug-in package apk;


Post a Comment

Previous Post Next Post