Reference Links:
1, http://blog.csdn.net/bboyfeiyu/article/details/11710497
2\ http://www.cnblogs.com/LittleRedPoint/p/3429709.html
First step: Writing interfaces
Package COM.LPL;
Public interface Iloader {
Public String SayHelloWorld ();
}
Step two: Implement the interface
Package COM.LPL; Public class Remote implements iloader{ @Override public String sayhelloworld () { return"sayhelloworld I am a jar"; } }
Step Three: Package jar
Export the specified package or Java file using the export feature of Eclipse
Fourth step: Working with the jar file using the DX command
DX--dex--output=g:\remote_dex.jar G:\remote.jar
Note: The DX command may be in platform-tools or in a subdirectory in Build-tools
Fifth step: Copy Dex to the Android project under the package directory
1, directly into the sdcard may be reported abnormal, because the Android system to SDcard read and write access restrictions
2. Need to add read and write permissions in Androidmanifest
3. The jar needs to be put into the android/data/com.*** directory
Sixth step:
File File =NewFile (Environment.getexternalstoragedirectory (). toString () +file.separator+"Android/data/com.example.test/remote_dex.jar");Class Lib=NULL; Try {//Basedexclassloader cl = new Basedexclassloader (Environment.getexternalstoragedirectory (). ToString (), F Ile, File.getabsolutepath (), getClassLoader ());Dexclassloader cl =NewDexclassloader (File.getabsolutepath (), File.getpath (),NULL, getClassLoader ()); Lib= Cl.loadclass ("Com.lpl.Remote");Iloader Loader=(Iloader) lib.newinstance (); Toast.maketext (Getapplicationcontext (),""+loader.sayhelloworld (), the). Show (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {E.printstacktrace (); }
Note here that the APK must also have an interface file, the remote jar is just the implementation of the interface file
Android load remote jar, APK