Dynamic loading in Android. Implementation steps for Jars _android

Source: Internet
Author: User
First of all, the first is the production of jar files, Java inside directly to the. class file into the. jar file, but Android's Dalvik VM is not the Java byte code, so it can not be directly packaged, but to use the DX tool to turn To Dalvik byte code. Of course, after the DX tool is turned, the jar package is not a. class file, but a. dex file.

The second is that Android offers a urlclassloader implementation, but it's not going to work. To dynamically load other classes, the class Loader you can use are:
Dexclassloader,pathclassloader, where dexclassloader can load apk, jar, or Dex files, for example:
Copy Code code as follows:

File Jarfile = new file ("/sdcard/test.dex");
# if (jarfile.exists ()) {
Dexclassloader cl = new Dexclassloader (jarfile.tostring (), "/sdcard/test", NULL, Classloader.getsystemclassloader ());
class<?> C = Cl.loadclass ("Xiaogang.test.Test");

However, Dexclassloader requires that a writable directory, the second parameter of the Dexclassloader constructor, be specified, in the example above/sdcard/test
The meaning of this parameter is: directory where optimized DEX files should be written
Because Dalvik dynamically optimizes the Dex file when it loads, Dexclassloader requires that the location of the optimized Dex file be specified.

Pathclassloader is more restrictive, it can only load apk files that have been installed into the Android system, that is, apk files in the/data/app directory. ClassNotFoundException will appear when files are loaded in other locations. For example:
Copy Code code as follows:

Pathclassloader cl = new Pathclassloader (jarfile.tostring (), "/data/app/", Classloader.getsystemclassloader ());

Because Pathclassloader will read the Dalvik optimized Dex file in the/data/dalvik-cache directory, the Dex file for this directory was generated by Dalvik when the APK package was installed. For example, if the name of the package is stored in the/data/app directory after the xiaogang.test,android application installation, that is,/data/app/xiaogang.test-1.apk, then/data/dalvik-cache The Data@app@xiaogang.test-1.apk@classes.dex file is generated under the directory. When Pathclassloader is invoked, it will follow this rule to find the Dex file, and if the apk file you specify is/sdcard/test.apk, it will read it according to this rule/data/dalvik-cache/ Sdcard@test.apk@classes.dex file, obviously this file does not exist, so Pathclassloader will complain.

Before Google fixes this problem, we either have to use Dexclassloader or we can only load the installed apk with Pathclassloader.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.