The previous article said that the dynamic loading in Android, that is, in the Android project dynamically loaded after the DX operation of the jar file and not installed APK file, today we look at how to execute the installed APK in the class method.
So, we will need two projects, one is Plugone, this is an Android project that we expose to outside methods. The other one, let's call him Useplugone.
First look at the Plugone project, we have a class in the Plugone project that exposes a method to the caller:
Package Com.example.plugone;public class Plugin1 {public int add (int a,int b) {return a + B;}}
In addition, you need to add an action in the manifest file for mainactivity in order to find the resources in this apk in the Useplugone project, the code is as follows:
<activity android:name= "com.example.plugone.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android: Name= "Android.intent.category.LAUNCHER"/> </intent-filter> <intent-filter><action Android:name= "Com.haha.android.plugin"/> </intent-filter> </activity>
Next is our Useplugone project, the main code is as follows:
Create an intent to find the specified apk Intent Intent = new Intent ("Com.haha.android.plugin", null); Get Package Manager Packagemanager PM = Getpackagemanager (); list<resolveinfo> resolveinfoes = pm.queryintentactivities (Intent, 0); Gets the information for the specified activity activityinfo actinfo = resolveinfoes.get (0). Activityinfo; Get the package name String pacagename = actinfo.packagename; Get the directory of the APK or the directory of the jar String Apkpath = ActInfo.applicationInfo.sourceDir; Dex Unzip the directory, note that this directory with the host program, Android only allows the program to read write itself//directory under the file String Dexoutputdir = Getapplicationinfo (). DataDir ; Directory of/data/app/com.example.plugone-1.apk//native code String LibPath = ACTINFO.APPLICATIONINFO.N Ativelibrarydir; /data/app-lib/com.example.plugone-1//Create class loader, load Dex into virtual machine Dexclassloader Calssloader = new Dexclassload ER (Apkpath, Dexoutputdir, LibPath, This.getclass (). getClassLoader ()); Method of calling the class within the plug-in package using reflection try {class<?> clazz = Calssloader.loadclass (pacagename+ ". Plugin1 "); Object obj = clazz.newinstance (); class[] param = new class[2]; Param[0] = Integer.type; PARAM[1] = Integer.type; method = Clazz.getmethod ("Add", param); integer ret = (integer) method.invoke (obj, 1,12); int result = Ret.intvalue (); SYSTEM.OUT.PRINTLN (result); } catch (Exception e) {e.printstacktrace (); }
SOURCE download
Below we are looking at the dynamic loading of the jar file in Android project, the same jar file needs to be processed by DX, do not know can see the previous article Android dynamic loading, in the previous article when we exported the jar file with the Java project, It was also indicated that when exporting the jar file do not choose to export the interface, because this interface is also declared in this Android project, so if the interface is exported, there will be an interface conflict, this time we have to export the Android project as a jar file, and can export the interface, This time we create an Android engineering Plugtwo, then export the jar file, and after DX processing, put it into another Useplugtwo project's asset folder for the program to run automatically. Here's a look at the code for the Plugtwo and Useplugtwo projects:
The first is plugtwo:
First we declare an interface file:
Package Com.example.plugtwo;public interface IInterface {public void call (); Public String getData (); }
Then create an entity class to implement the interface, this time we let the entity class pop toast, so we need to create a context member variable
Package Com.example.plugtwo;import Android.content.Context; Import Android.widget.Toast; public class IClass implements IInterface { private context context; Public IClass (Context context) { super (); This.context = context; } @Override public Void Call () { Toast.maketext (context, "call method", 0). Show (); @Override public String getData () { return ' hello,i am from IClass '; }
The procedure is relatively simple, I will not elaborate. Then export the program as a jar file, where we export the same asLoad.jar, then copy the Load.jar file to the folder with the SDK for the DX command, perform the dx--dex--output=testdex.jar Load.jar, Success will find a Testdex.jar file generated under this folder, which is a jar file that our Android project can call directly.
Now we create a new Android project called Useplugtwo, and will The Testdex.jar file is copied to useplugtwo Asset project, next look at Useplugtwo code in our mainactivity:
First we call the Copyfromasset method to copy the Testdex.jar file under the Asset folder to the specified directory of the SDcard, with the following code:
public void Copyfromasset () {InputStream ins = null; FileOutputStream fos = null;try {ins = Getassets (). Open ("Testdex.jar");//string getstr = "/storage/sdcard0/liuhang/"; File File = new file (dir+ "/testjar/"), if (!file.exists ()) {file.mkdirs ();} File = new file (file, "Testdex.jar"); fos = new FileOutputStream (file); int count = 0;byte[]b = new Byte[1024];while (count = Ins.read (b))! =-1) { fos.write (b,0,count);}} catch (Exception e) {e.printstacktrace ();} Finally{try {if (fos! = null) {Fos.flush (); Fos.close ()}} catch (Exception e) {e.printstacktrace ();}}}
This is then done using reflection to dynamically execute the method in the generated Testdex.jar file, as follows:
file file = new file ("/storage/sdcard0/testjar/testdex.jar"); final File Optimizeddexoutputpath = getdir ("temp", context.mode_private); String filePath = File.getabsolutepath (); string optimizedpath = OPTIMIZEDDEXOUTP Utpath.getabsolutepath (); //optimizedpath = = Data/data/com.example.useplugtwo/app_temp Dexclassloader classLoader = new Dexclassloader (File.getabsolutepath (), & nbsp Optimizeddexoutputpath.getabsolutepath (), NULL, &NBS P getClassLoader ()); try { class<?> IClass = Classloade R.loadclass ("Com.example.plugtwo.IClass"); constructor<?> istructor = Iclass.getconstructor (Context.class); //Use reflection principle to call Method Me Thod = Iclass.getmethod ("call", null); String data = (string) Method.invoke (Istructor.newinstance (this), NULL); SYSTEM.OUT.PRINTLN (data); } catch (Exception e) { //TODO Auto-gen Erated Catch block e.printstacktrace (); }
It's time to get some rest today.
Source Connection
Android dynamic loading (ii)