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

Source: Internet
Author: User

After knowing the pre-knowledge points of class loading in Android, look at the detailed implementation of Dexclassloader in Android
The detailed loading process is as follows:
The host program will load the APK "1" into the file system, such as SD card, and then run the activity in the APK with an activity called proxy.
About dynamically loading APS. Theoretically usable to Dexclassload, Pathclassloader, URLClassLoader;
Dexclassloader: Ability to load jar, Dex, apk on file system
Pathclassloader: The ability to load the APK under the/data/app folder, which also means it can only load the installed APK
URLClassLoader: The ability to load jars in Java, but because Dalvik in Android does not recognize jars directly, all of this is not available on Android. Although there is this class

Get Assetsmanager
The method of loading is through reflection, by invoking the Addassetpath method in Assetmanager. We were able to load a resource from an APK into the resources because Addassetpath is a hidden API that we can't call directly, so just by reflection, the following is its declaration, and by staring at what we can see, the path passed can be a zip file or a resource folder. And the APK is a zip. So just pass the APK path to it, the resource is loaded into Assetmanager, and then create a new resources object by Assetmanager, which is the resource in the apk we can use.


Encapsulating data:
1: ClassLoader, Assetmanager, resource as a member of the plugin. Encapsulation into beans
2: Multi-plug-in through the map maintenance, the key is the APK PackageName


Plug-in loading into the memory flow:

Loading plug-in activity:

? PluginsActivityitself (life cycle, resources, etc.) is provided by the hostproxyactivityto load. ? 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 plugin activity

Dxpluginbean encapsulated Bean

    ?encapsulates each of thePluginalsoapkof Data? Maintenance inDxpluginmangerClass ofMapin

Dxpluginmanager plug-in Management core class      ? load Plug-in,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 Time. 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 to jump main Activity


dxpluginbaseactivity, dxpluginbasefragmentactivity:

all plug-ins activity inherits these two activity

The activity implements the Idxplugin interface

Get proxyactivity references in the Onattach method

? AllActivityThe inheritance method needs to infer 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 repetitive 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



Can directly participate in the demo:

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



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.