Android APK Dynamic Loading technology Research (2) Android plugin and implementation

Source: Internet
Author: User

After learning about the pre-knowledge of class loading in Android, take a look at the specific implementation of Dexclassloader in Android
The specific loading process is as follows:
The host program will go to the file system such as SD card to load the APK "1", and then through an activity called Proxy to perform activity in the APK
On the dynamic loading AP, it can theoretically be used to dexclassload, Pathclassloader, URLClassLoader;
Dexclassloader: Can load jar, Dex, apk on file system
Pathclassloader: The APK can be loaded in the/data/app directory, which also means that it can only load the installed APK
URLClassLoader: The jar in Java can be loaded, but because the Dalvik in Android does not recognize the jar directly, all of the methods are not available in Android, although there is this class

Get Assetsmanager
The method of loading is through reflection, by calling the Addassetpath method in Assetmanager, we can load a resource in an apk into the resources, because Addassetpath is a hidden API that we cannot call directly, so we can only pass the reflection, Here is its declaration, through the comments we can see that the path can be a zip file or a resource directory, and the APK is a zip, so directly to the APK path to it, the resources are loaded into the Assetmanager, And then create a new resources object through Assetmanager, which is the resource in the apk we can use.


Encapsulating data:
1: ClassLoader, Assetmanager, resource as a member of the plug-in, encapsulated into beans
2: Multiple plugins are maintained via map, where key is apk packagename


Plug-in loading into memory flow:

Load plug-in activity:

? PluginsActivityitself (life cycle, resources, etc.) is provided by the hostproxyactivityto load the. ? when we sendIntentto start the plugin whenActivityWhen the substance started isproxyactivity? Encapsulated in order to encapsulate the detailsdxintent? all plugins are implementedIdxpluginInterfacePrxoyactivity took over all the plugins activity

Dxpluginbean encapsulated Bean

    ?encapsulates each of thePluginalsoapkof Data? Maintenance inDxpluginmangerClass ofMapin

Dxpluginmanager plug-in Management core class      ? Load plug-ins,Start plug-ins,plug-in maintenance
Idxplugin:

     ?put each plugin'sActivityabstract into a "plugin"? Idxpluginrealized theActivitythe Main method? Onattachmethod is a plugin-specific callback method that when the plug- inActivitywasProxyload When,putProxyis assigned a reference value to the that
dxintent:? Pluginpackgename:Jump toPluginthe package name, i.e.Manifestin thePackageName

pluginclassname: Jump plugin specified in the Activityname, can be passed NULL, the default is jump main Activity


dxpluginbaseactivity, dxpluginbasefragmentactivity:

All plugins activity inherits these two activity

The activity implements the Idxplugin interface

Get proxyactivity references in the Onattach method

? AllActivityThe inheritance method needs to determine whether the plug-in itself starts or is started in the host----"From_internal and from_external
dxproxyactivity,dxproxyfragmentactivity:. ? HostActivity? in theHostcalling plug-ins inActivityThe jump, the essence is these twoActivityThe jump between? provides the plug-in with a trueContext? to reduce the duplication of code, place the initialization of the plug-inDxplugininitializerin class    
Dxplugininitializer     ?FixThemeThe crash problem that brought    ? Launch PluginActivity

1 get plug-ins with reflection Activity the default constructor

2 by Reflection New out a plugin and turn it into a strong Idxplugin

3 Callback Onattach method Incoming Prxoy the reference

4 called onCreate method to adjust the plug-in



You can directly refer to the demo:

http://download.csdn.net/detail/yaya_soft/8858645



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android APK Dynamic Loading technology Research (2) Android plugin and implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.