Previous article, [Android plugin Development Atlas first experience] (
http://blog.csdn.net/sbsujjbcy/article/details/47446733), a simple introduction to the entire process of using Atlas, but if you have not practiced the estimate is still confused, starting from this article, Slowly cut into the details. The topic of this article is to generate a list of plugin information.
Careful you may find that in the last article we used a project called Openatlasbundler to generate so and a JSON file, which is just the apk rename, and this JSON file contains the plugin information, For example, in the previous article two plugins, the information is as follows.
[ { "Pkgname":"Com.lizhangqu.test", "version":"1.0", "Activities":[ "com.lizhangqu.test.MainActivity" ], "Services":[], "Receivers":[], "contentproviders":[], "Dependency":[], "MD5":"882d50a3c2668360b96a3af8f72b3bd2", "size":24780, "Hasso":false }, { "Pkgname":"Com.lizhangqu.zxing", "version":"1.0", "Activities":[ "com.lizhangqu.zxing.android.CaptureActivity" ], "Services":[], "Receivers":[], "contentproviders":[], "Dependency":[], "MD5":"e68270557ae9776d218bb034916c124a", "size":312352, "Hasso":false }]
You can see some of the component information stored in the plugin, as well as capitalization and MD5. According to the author's original words.
The generation of bundlelist. This part is written in Java, bundlelistinfo a text file that stores json, all we do is improve efficiency, for example, when you install the plug-in if there is a dynamic library to extract, can not be installed when each file to detect the APK exists so does not, Open file is slow, program optimization is a little bit
We can try, if we don't pack the so file, what happens when the program runs.
We found that a log was printed saying that the plugin information list could not be found. As for the plug-in information list reading, this is not to be ignored here, the focus of this article is how to generate the list.
Open the Bundlemakebooter class, there is a main function, that is, this is just a common Java project, in fact, we can from the command line parameters passed in, but also remember the last article we commented out the first three lines of code, added three lines of code to write dead.
args=new String[2];args[0]="C:\\Users\\kltz\\Desktop\\AtlasDemo\\plugin";args[1]="C:\\Users\\kltz\\Desktop\\AtlasDemo\\plugin\\bundle-info.json";
The first parameter of the array represents the directory where the APK is to be processed, and the second parameter of the array represents the file in which the plugin information is generated. And we are in the project root directory to create a folder called plugin, and then the plug-in apk are copied to the directory for processing.
The main function starts by calling apkpreprocess.preprocess (path), passing in the first argument, the function is actually very simple, is to rename the apk to so, how to rename the method. First traverse the folder, get the apk suffix file, by parsing the package name to get the APK, the apk named Lib, followed by the package name, the package name in the. will be replaced by _, the last file suffix is so, if the directory has the so, then the original so file deleted, and then renamed the APK. It's that simple. Look at the code
public static void Preprocess (String mdir) {collection<file> apkfiles = Org. Apache. Commons. IO. FileUtils. Listfiles(New File (Mdir), new string[]{"APK"}, True);for (File file:apkfiles) {String pkgname = Packagelite. Parse(File. GetAbsolutePath()). PackageName;Pkgname ="Lib"+ Pkgname. ReplaceAll("\\.","_") +". So";File TargetFile = new file (mdir + file. Separator+ pkgname);if (targetfile. Exists()) TargetFile. Delete();System. out. println("Rename:"+ File. GetName() +" ,"+ pkgname);while (!file. Renameto(targetfile)) {System. GC();Thread. Yield();} System. out. println("apkpreprocess.preprocess () processed"+ pkgname);} }
After renaming is complete, the next step is to traverse the folder, looking for files beginning with libcom_, note that this is a convention, that is, the package name begins with COM. Then through the packagelite.parse () function to parse each so, through the packagelit.getbundleinfo () function to obtain the parse result and throw it into a JSON array, The JSON array is finally written to the file, which is the content specified by the second parameter of the args array.
Put in how to parse, here do not expand, the code is too long, in fact, in Android will parse the XML parsing. Resolved in pull mode. You can look at the Getbundleinfo () function
Public Jsonobject Getbundleinfo () throws jsonexception {Jsonobject jsonobject=new jsonobject (); Jsonobject.put("Pkgname", PackageName); Jsonobject.put("Version", versionname); Jsonarray activityarray=new Jsonarray (); for(Stringname: Activitys) {Activityarray.put(name); } jsonobject.put("Activities", Activityarray); Jsonarray servicesarray=new Jsonarray (); for(Stringname: Services) {Servicesarray.put(name); } jsonobject.put("Services", Servicesarray); Jsonarray receiversarray=new Jsonarray (); for(Stringname: receivers) {Receiversarray.put(name); } jsonobject.put("Receivers", Receiversarray); Jsonarray providersarray=new Jsonarray (); for(Stringname:p roviders) {Providersarray.put(name); } jsonobject.put("Contentproviders", Providersarray); Jsonarray dependencyarray=new Jsonarray (); for(Stringname:d ependency) {Dependencyarray.put(name); } jsonobject.put("Dependency", Dependencyarray); Jsonobject.put("MD5", apkMD5); Jsonobject.put("Size", size); Jsonobject.put("Hasso", Hasso);returnJsonobject; }
In fact, the results are converted to JSON, very simple wood has.
Note that the file name of the generated JSON file is Bundle-info.json, but you can also use a different file name by modifying the code, and then put it in the assets directory.
The code that parses the JSON file is in the parser function in the Bundleparser class in the Openatlascore project, and it is interesting to take a look first.
The next thing to do is to pack the JSON into the APK, put so into the corresponding directory, that is, the Armeabi directory, according to the author's words is to speed up the start speed, you can see this issue plugin can not be placed in the main project Libs/armeabi
Also, hurry up and try it yourself, as long as you have tried it, it will feel, oh, it is such a thing ah.
What, source code, wood source code, the code see the end of the previous article [Android plugin development of the Atlas first experience] (
http://blog.csdn.net/sbsujjbcy/article/details/47446733)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Plugin Development Atlas Build plugin Information list