1.1 First thing you need to know: Dynamic loading in Android, but it's not as easy as Java to load jars dynamically
Cause: The Android virtual machine (Dalvik VM) is a byte code that does not recognize the Java-typed jar and needs to be optimized for conversion to Dalvik byte code by using the DX tool. This is what we can see in our Android project's packaged APK: the introduction of the contents of the other jars is packaged into the classes.dex.
So this road is not through, please pay attention.
1.2 What APIs are currently available for dynamic loading
1.2.1 Dexclassloader
This can be loaded Jar/apk/dex, can also be loaded from the SD card, is also the focus of this article.
1.2.2 Pathclassloader
You can only load apk files that are already installed on your Android system.
1.3 Code
Package Com.example.dynamicloaddemo.jar; public class Dynamictest implements idynamic {@Override public String HelloWorld () {return "Hello World Form JAR "; } }
To export this class, note that the compile time must be Java 1.6 compilation (Java 1.7 compilation error: Zip is good, but no classes.dex inside, and no valid. odex file in the SAM e-Directory)
1.4 After compiling the file, package the above class as Dynamic.jar
1.4.1 Download the jar conversion Dex tool and copy the packaged jar to the SDK installation directory android-sdk-windows\ Platform-tools, Dos enters this directory, executes the name: dx --dex --output=test.jar Dynamic.jar
1.4.2 will test.jar Copy to/data/data/ packagename/app-libs/
There will be errors on the SDcard optimized data directory/data/ Data/com.example.dynamicloaddemo/files/test.jar is isn't owned by the current user.
650) this.width=650; " Src= "Http://s3.51cto.com/wyfs02/M00/5B/84/wKiom1UKkMrQb82RAACcD1kPgqo464.jpg" title= "Qq20150319170138.png" alt= " Wkiom1ukkmrqb82raaccd1kpgqo464.jpg "/>
Program code:
public class Mainactivity extends Activity {
Button Mtoastbutton;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mtoastbutton = (Button) Findviewbyid (R.ID.MAIN_BTN);
Before the secondary Dex file can be processed by the Dexclassloader,
It has to is first copied from asset resource to a storage location.
Final file Dexinternalstoragepath = new file (Getdir ("Dex",
context.mode_private), secondary_dex_name);
if (!dexinternalstoragepath.exists ()) {
Mprogressdialog = Progressdialog.show (This,
Getresources (). getString (R.string.diag_title),
Getresources (). getString (R.string.diag_message), true, false);
Perform the file copying in an asynctask.
Download required DEX files from the network
(New Preparedextask ()). Execute (Dexinternalstoragepath);
} else {
Mtoastbutton.setenabled (TRUE);
// }
System.out.println (Getdir ("Libs", context.mode_private));
System.out.println (Getfilesdir ());
System.out.println (Getcachedir ());
System.out.println (Getdir ("Libs", context.mode_private));
System.out.println (Getdir ("Libs", context.mode_private));
Mtoastbutton.setonclicklistener (New View.onclicklistener () {
public void OnClick (view view) {
Internal storage where the Dexclassloader writes the
Optimized Dex file to.
Final File Optimizeddexoutputpath = Getdir ("Outdex", context.mode_private);
Final file Optimizeddexoutputpath = new file (Getdir ("Libs", context.mode_private) + File.separator + "Test.jar");
Initialize the class loader with the secondary Dex file.
Dexclassloader cl = new
Dexclassloader (Dexinternalstoragepath.getabsolutepath (),
Optimizeddexoutputpath.getabsolutepath (),
Null
getClassLoader ());
/*
Dexclassloader cl = new Dexclassloader (Optimizeddexoutputpath.getabsolutepath (), Environment
. getExternalStorageDirectory (). toString (), NULL, getClassLoader ());
*/
Dexclassloader cl = new Dexclassloader (Optimizeddexoutputpath.getabsolutepath (), Getdir ("Libs", context.mode_private ). GetAbsolutePath (), NULL, getClassLoader ());
Class<?> libproviderclazz = null;
try {
Load the Library class from the class loader.
Loading classes downloaded from the network
Libproviderclazz =
Cl.loadclass ("Com.example.dex.lib.LibraryProvider");
Libproviderclazz = Cl.loadclass ("Com.example.dynamicloaddemo.jar.DynamicTest");
Cast the return object to the library interface so
The
Caller can directly invoke methods in the interface.
Alternatively, the caller can invoke methods through
Reflection
Which is more verbose and slow.
Libraryinterface lib = (libraryinterface)
Libproviderclazz.newinstance ();
Idynamic lib = (idynamic) libproviderclazz.newinstance ();
Display the toast!
Lib.showawesometoast (View.getcontext (), "Hello World!");
Toast.maketext (Mainactivity.this, Lib.helloworld (), Toast.length_short). Show ();
} catch (Exception Exception) {
Handle exception gracefully here.
Exception.printstacktrace ();
}
}
});
}
}
1.5 Run, the Hello world from JAR appears , indicating a successful dynamic load.
This article is from the LUA Learning Notes blog, so be sure to keep this source http://mythwind.blog.51cto.com/6506705/1622297
About Android dynamic load jar file