Android Dynamic loading technology (plug-in technology)

Source: Internet
Author: User

Tag: is the BSP direct try its return method out agent

NO1:

Benefits of plug-in technology:

1) reduce the memory and CPU consumption of the application

2) Hot -swappable, i.e. updating some modules without releasing a new version

No2:

The plug-in solution must address three fundamental issues: resource access ,activity Lifecycle Management , and ClassLoader management

No3:

The host refers to the normal apk , which is usually referred to as a processed Dex or APK. Plug-in framework most of the use of APK as a plug-in, many need to use agent activity, plug-in activity to start most of the use of a proxy activity to achieve.

No4:

Activity is done mainly through the Contextimpl , the activity has a mbase member variable, its type is Contextimpl. In the context there are two abstract methods getassets and getresources, which are used to obtain resources, and are actually implemented in Contextimpl.

NO5:

Resource access

Load the resources in the APK

protected voidloadresources () {Try{Assetmanager Assetmanager= Assetmanager.class. newinstance (); Method Addassetpath= Assetmanager.getclass (). GetMethod ("Addassetpath", String.class);        Addassetpath.invoke (Assetmanager,mdexpath); Massetmanager=Assetmanager; }Catch(Exception e) {e.printstacktrace (); } Resources superres=Super. Getresources (); Mresources=NewResources (Massetmanager,superres.getdisplaymetrics (), superres.getconfiguration ()); Mtheme=Mresources.newtheme (); Mtheme.setto (Super. Gettheme ());}

By reflection , call the Addassetpath method in Assetmanager and load the resource in an APK into the resources object. Then create a new resources object with Assetmanager.

 Public Final int Addassetpath (String path) {    synchronized(this) {        int res = addassetpathnative (path);        Makestringblocks (mstringblocks);         return res;    }}

Then implement Getassets and getresources in agent activity .

@Override  Public Assetmanager getassets () {    returnnull?  Super. Getassets (): Massetmanager;} @Override public Resources getresources () {    returnnull?  Super. Getresources (): mresources;}

NO6:

Management of activity life cycle

Reflection mode

@Overrideprotected voidOnresume () {Super. Onresume (); Method Onresume= Mactivitylifecirclemethods.get ("Onresume"); if(onresume!=NULL){        Try{Onresume.invoke (mremoteactivity,Newobject[]{})}Catch(Exception e) {e.printstacktrace (); } }} @Overrideprotected voidOnPause () {Method onPause= Mactivitylifecirclemethods.get ("OnPause"); if(onpause!=NULL){        Try{Onpause.invoke (mremoteactivity,Newobject[]{})}Catch(Exception e) {e.printstacktrace (); }    }    Super. OnPause ();}

Interface mode

 Public Interface dlplugin{    publicvoid  onStart ();      Public void Onrestart ();      Public void Onresume ();      Public void onPause ();      Public void onStop ();      Public void OnDestroy ();    ...}

Called in Agent Actvitiy

... @Override protected void OnStart () {    mremoteactivity.onstart ();     Super . OnStart ();} @Overrideprotectedvoid  Onrestart () {    mremoteactivity.onrestart ();     Super . Onrestart ();} @Overrideprotectedvoid  Onresume () {    mremoteactivity.onresume ();     Super . Onresume ();}

Mremoteactivity is the realization of dlplugin

No7:

Management of plugin ClassLoader

 Public classDlclassloaderextendsdexclassloader{Private Static FinalString TAG = "Dlclassloader"; Private Static FinalHashmap<string,dlclassloader> mpluginclassloaders =NewHashmap<string,dlclassloader>(); protectedDlclassloader (String dexpath,string optimizeddirectory,string librarypath,classloader parent) {Super(dexpath,optimizeddirectory,librarypath,parent); }         Public Staticdlclassloader getclassloader (String dexpath,context context,classloader parentloader) {Dlclassloader dLassLoad ER=Mpluginclassloaders.get (Dexpath); if(Dlassloader! =NULL){            returnDlclassloader; } File Dexoutputdir= Context.getdir ("dex", context.mode_private); FinalString Dexoutputpath =Dexoutputdir.getabsolutepath (); Dlclassloader=NewDlclassloader (Dexpath,dexoutputpath,NULL, Parentloader);                Mpluginclassloaders.put (Dexpath,dlclassloader); returnDlclassloader; }}

By storing the ClassLoader of different plug-ins in a single HashMap , you can ensure that classes in different plugins do not interfere with each other.

Android Dynamic loading technology (plug-in technology)

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.