Android DynamicLoadApk open-source plug-in development project code analysis, androidstudio plug-in
The directory structure of the current Code is:
1. com. ryg. dynamicload. internal
2. com. ryg. dynamicload. service
3. com. ryg. dynamicload
4. com. ryg. dynamicload. utils
First, let's look at the following classes in the com. ryg. dynamicload. internal Package:
1. DLAttachable
DL is short for DynamicLoad. Attachable is connectable, which means dynamic loading can be connected. Essentially, this is an interface. There is a method in the interface, attach. The parameter is the proxy DLPlugin required for plug-in development, and the plug-in Development Manager DLPluginManager.
/**
* When the proxy impl ({@ see DLProxyImpl # launchTargetActivity ()}) launch
* The plugin activity, dl will call this method to attach the proxy activity
* And pluginManager to the plugin activity. the proxy activity will load
* The plugin's resource, so the proxy activity is a resource delegate
* Plugin activity.
*
* @ Param proxyActivity a instance of DLPlugin, {@ see DLBasePluginActivity}
* And {@ see DLBasePluginFragmentActivity}
* @ Param pluginManager DLPluginManager instance, manager the plugins
*/
The above section is roughly translated into English:
The current implementation class starts the Activity of the plug-in. DL will call this method to connect the agent Activity and the plug-in Manager to the plug-in Activity. The agent Activity will load the plug-in resources, therefore, the agent Activity is a resource hosting of the internal Activity of the plug-in.
2. DLIntent
DLIntent is a subclass of Intent. It contains two member variables: the package name of the plug-in to be started and the class name of the plug-in to be started.
3. DLPluginManager
First, analyze its internal member variables,
First, there are three Integer constants:
START_RESULT_SUCCESS
START_RESULT_NO_PKG
START_RESULT_NO_CLASS
START_RESULT_TYPE_ERROR
The plug-in is successfully started, the package name of the specified plug-in is not found, one of the classes of the plug-in to be started is not found, and the type of the specified class is incorrect.
Another variable is the so directory of the specified plug-in.
The methods include obtaining Dex, starting, stopping, binding, unbinding, starting Activity, loading the class with the specified class name, and copying the so file.
3. DLPluginPackage
Literally, it specifies the Apk of a plug-in.
Take a look at the Annotations:
* A plugin apk. Activities in a same apk share a same AssetManager, Resources
* And DexClassLoader.
A plug-in Apk shares the same AssetManager, Resources, and DexClassLoader among a series of Activities in the same Apk.
The member variables are as follows:
1. Complete Package name of the plug-in
2. default Activity name
3. Dex Class Loader
4. AssetManager object
5. Resource Resources object
5. PackageInfo object
4. DLProxyImpl
It literally refers to the implementation of plug-in proxy.
Note:
* This is a plugin activity proxy, the proxy will create the plugin activity
* With reflect, and then call the plugin activity's attach, onCreate method,
* This time, the plugin activity is running.
This is the agent of the plug-in Activity. The agent will use reflection to create the plug-in Activity, call the attach and onCreate methods of the plug-in Activity, and the plug-in Activity will be in the running state.
The member variables are as follows:
Private String mClass;
Private String mPackageName;
Private DLPluginPackage mPluginPackage;
Private DLPluginManager mPluginManager;
Private AssetManager mAssetManager;
Private Resources mResources;
Private Theme mTheme;
Private ActivityInfo mActivityInfo;
Private Activity mProxyActivity;
Protected DLPlugin mPluginActivity;
Public ClassLoader mPluginClassLoader;
1. The class name of the current plug-in Activity
2. Package name of the current plug-in Activity
3. The object that identifies the current plug-in Apk
4. Plug-In Manager
5. Asset Manager
6. Resource Manager
7. Subject object
8. object that describes information about the current plug-in Activity
9. Proxy Activity object
10. Plug-in Interface
11. class loaders
5. DLServiceAttachable
This class is similar to the function of DLAttachable, except for Activity and service.
6. DLServiceProxyImpl
In the constructor, the required parameter is the proxy service object.
The init initialization function is an intent object.
The internal process is described as follows:
1. The class loader is used to load the Internal Service name of the specified plug-in. In fact, the reflection is used to create an object.
2. Bind the Service object inside the plug-in to the proxy Service object
3. directly call the onCreate method of the plug-in Service
Next, let's look at the following classes in the com. ryg. dynamicload package:
1. DLBasePluginActivity
The member variables are as follows:
/**
* The proxy activity can be used as the Context to determine whether to point to this
*/
Protected Activity mProxyActivity;
/**
* It is equivalent to mProxyActivity and can be used as Context. It determines whether to point to this as needed. <br/>
* It can be used as this.
*/
Protected Activity that;
Protected DLPluginManager mPluginManager;
Protected DLPluginPackage mPluginPackage;
1. The proxy Activity is a running Activity.
2. Plug-In Manager
3. Plug-in package name
4. The default is from the internal plug-in.
The following describes the main methods in this class:
1. Assignment of proxy Activity
2. assign values to the plug-in Apk object
3. At the same time, it is found that methods such as onCreate and setContentView related to Activity can be used to determine whether internal or external startup is enabled. If internal startup is enabled, no proxy Activity is required, if it is external
Start. Any Activity-related activities must be processed by a proxy Activity.
4. Note that onAttach is an external start.
2. DLBasePluginFragmentActivity
This class is the same as DLBasePluginActivity and will not be repeated.
3. DLPlugin
This is an interface developed by the plug-in. The method in the interface is as follows:
Public void onCreate (Bundle savedInstanceState );
Public void onStart ();
Public void onRestart ();
Public void onActivityResult (int requestCode, int resultCode, Intent data );
Public void onResume ();
Public void onPause ();
Public void onStop ();
Public void onDestroy ();
Public void attach (Activity proxyActivity, DLPluginPackage pluginPackage );
Public void onSaveInstanceState (Bundle outState );
Public void onNewIntent (Intent intent );
Public void onRestoreInstanceState (Bundle savedInstanceState );
Public boolean onTouchEvent (MotionEvent event );
Public boolean onKeyUp (int keyCode, KeyEvent event );
Public void onWindowAttributesChanged (LayoutParams params );
Public void onWindowFocusChanged (boolean hasFocus );
Public void onBackPressed ();
Public boolean onCreateOptionsMenu (Menu menu );
Public boolean onOptionsItemSelected (MenuItem item );
It basically includes all the system methods in the Activity.
4. DLProxyActivity
Agent class of the plug-in Activity.
Among them, the member variables include: 1, DLPlugin 2, and DLProxyImpl. One is to point to the plug-in object and the other is to point to the Implementation object of the proxy.
Since it is the agent class of the plug-in, it is clear that all the implementation methods here can be guessed that all the methods related to the Activity will call the method of the plug-in's own Activity on the one hand, and on the other hand, in the same method, the method in the proxy Activity is called.
5. DLProxyFragmentActivity
Similar to DLProxyActivity.
6. DLProxyService
This is the Service proxy Service.
From the above variables, it mainly includes the following three points:
1. Service proxy implementation
2. Internal Service of the plug-in
3. Plug-In Manager
In terms of function implementation, it is actually the same as the proxy implementation of the Activity. Any key internal method is called by both the proxy class and the plug-in class, and the rest will not be repeated.
7. DLServicePlugin
The interface that includes the key methods of the Service is not described in detail.
Finally, let's take a look at the following classes in the com. ryg. dynamicload. util package:
1. DLConfigs
Storage and acquisition of so modification time
2. DLConstants
In this constant class, we mainly configure the following major constants:
1. Internal start or external start
2. The Dex path and the Tag of the related package name Storage
3. Activity-related types
4. CPU-related architecture
3. DLUtils
This tool class
Includes the following functions:
1. Get the package name getPackageInfo of the specified plug-in Apk file under the Sdk directory
2. Get the application icon getAppIcon of the specified plug-in Apk file in the Sdk directory
3. Get the application name of the specified plug-in Apk file in the Sdk directory getAppLabel
4. The reflection mechanism is used to obtain the types in the specified plug-in Activity.
4. SoLibManager
1. Obtain CPU-related information 2. Copy the so file of the plug-in to the directory folder of the so file in the current project directory in the thread (according to the current cpu architecture, copy the appropriate so file)
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.