first, the dynamic loading classloader mechanism of temperature
if the Android ClassLoader loading mechanism is not familiar, poke the Android plug-in development dynamic loading base of the ClassLoader working mechanism http://blog.csdn.net/u011068702/ article/details/53248960
Second, Introductionwe know that in Android you can dynamically load the jar like Java, but Android uses the Tak hoi Dalvik VM and cannot directly load the Java package jar's byte code, which requires the DX tool to optimize the Dalvik bytecode.
Android in the API is given to dynamically load the: Dexclassloader and Pathclassloader (above the connection has been described in detail)
Dexclassloader: loadable jar, APK and Dex can be loaded from SD card (this blog is used this way)
Pathclassloader: can only load apk files that have been installed with Android system
Third, Exposure demo photos, not afraid, not much, very simple
Iv. Writing interface Files
Package Com.example.testclassloader;public interface Showstring {public String saychenyu ();}
Five, write the interface implementation file
Package Com.example.testclassloader;import Android.util.log;public class Showstringclass implements showstring{ public static final String TAG = "Showstringclass"; @Overridepublic string Saychenyu () {String chenyu = "Chenyu"; LOG.I (TAG, Chenyu); return chenyu;}}
six, packaged into a jar file compiled into Dex We package the Showstringclass.java file to generate the Showstringclass.jar file, and then put the file in the SDK directory under the Build-tools 23.0.1 directory, I use Ubuntu, so I will see the Dex file, If window will see the Dex.bat file in this directory, then use the command below to showstringclass.jar generation showstringclass_imle.jar dex file
DX--dex--output=showstringclass_impl.jar Showstringclass.jar
And then put the Showstringclass_impl.jar file in the phone directory to use this command.
ADB push Showstringclass_impl.jar /sdcard/
Specific operation picture as follows
Seven, then write the Mainactivity.java file
Package Com.example.testclassloader;import Java.io.file;import Android.content.context;import android.os.Bundle; Import Android.os.environment;import Android.support.v7.app.actionbaractivity;import Android.util.Log;import Android.widget.textview;import Dalvik.system.dexclassloader;public class Mainactivity extends ActionBarActivity { public static final String TAG = "Mainactivityclassloader"; public static final String Showstringclass = "Showstringclass_impl.jar"; public static final String showstringclass_path= "Com.example.testclassloader.ShowStringClass"; public static final String dex = "Dex"; Public Showstringclass mshowstringclass = null; Public TextView mTv = null; public int i = 0; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); mTv = (TextView) Findviewbyid (R.id.hello);D exclassloader (this);} /** * Use Dexclassloader mode to load class */public void Dexclassloader (Context context) {//Dex Compressed file path (can be in apk,jar,zip format) String Dexpath = Environment.getexternalstoragedirectory (). toString () + File.separator + Showstringclass; Dex unzip the released directory String dexoutputdirs = Environment.getexternalstoragedirectory (). toString (); Specify Dexoutputpath as the app's own cache directory File Dexoutputdir = context.getdir (DEX, 0); Define Dexclassloader//First parameter: is the path of the Dex compressed file//second parameter: is the directory that Dex unpacked///The third parameter: is the local library file directory that is a/C + + dependent, can be null Fourth parameter: is the class loader at the top level//dexclassloader Dexclassloader = new Dexclassloader (dexpath,dexoutputdirs,null,getclassload ER ()); Dexclassloader Dexclassloader = new Dexclassloader (Dexpath,dexoutputdir.getabsolutepath (), Null,getClassLoader ()); Class libprovierclazz = null; Use the Dexclassloader load class try {Libprovierclazz = Dexclassloader.loadclass (Showstringclass_path); Create dynamic instance Mshowstringclass = (Showstringclass) libprovierclazz.newinstance (); if (MSHowstringclass = null) {final String Chenyu = Mshowstringclass.saychenyu (); if (Chenyu! = null) {Mtv.post (new Runnable () {@Overridepublic void Run () {Mtv.settext (Chenyu);} }); }} else {log.d (TAG, "Mshowstringclass is null"); }} catch (Exception e) {e.printstacktrace (); }}/** * Print system ClassLoader */public void Showclassloader () {ClassLoader ClassLoader = getClassLoader (); if (ClassLoader! = null) {LOG.I (TAG, "[OnCreate] classLoader" + i + ":" + classloader.tostring ()); while (Classloader.getparent ()!=null) {ClassLoader = Classloader.getparent (); LOG.I (TAG, "[OnCreate] classLoader" + i + ":" + classloader.tostring ()); i++; } }}}
Eight, run the results of the demo photoprint results on Ubuntu terminal as follows
The photos on the phone are as follows
indicates that the loaded external file was loaded successfully
If you change the code above into this one.
Dexclassloader Dexclassloader = new Dexclassloader (Dexpath,dexoutputdirs,null,getclassloader ());
will report the following error
directories that need to be cached for Dex files
Specify Dexoutputpath as the app's own cache directory File dexoutputdir = Context.getdir (DEX, 0);
ix. Summary1, deepen the understanding of dynamic loading2. How to implement project loading external DEX files have a better understanding3, to Dexclassloader, Dexclassloader.load (Package.class), class.newinstance () have a better understanding
Android Plugin Development dexclassloader dynamic load Dex, jar small Demo