How to dynamically load. jar in Android

Source: Internet
Author: User

First, the first one is the production of jar files, which are directly stored in Java. package the class file. the jar file can be used, but the Dalvik VM of Android does not recognize the byte code of Java, so it cannot be packaged directly. Instead, you can convert it to the Dalvik byte code using the dx tool. Of course, after the dx tool is switched, the jar package is not a. class file, but a. dex file.

The second is that although the URLClassLoader implementation is also provided in Android, it cannot be used. To dynamically load other classes, you can use the following Class Loader:
DexClassLoader and PathClassLoader. DexClassLoader can load apk, jar, or dex files, for example:Copy codeThe Code is 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 must specify a writable directory, that is, the second parameter of the DexClassLoader constructor. In the preceding example, It is/sdcard/test.
This parameter indicates directory where optimized DEX files shocould be written.
Because Dalvik dynamically optimizes dex files when loading them, DexClassLoader requires you to specify the location where the optimized dex files are stored.

PathClassLoader has more restrictions. It can only load the apk file installed in the Android system, that is, the apk file under the/data/app directory. ClassNotFoundException occurs when files in other locations are loaded. For example:Copy codeThe Code is as follows: PathClassLoader cl = new PathClassLoader (jarFile. toString (), "/data/app/", ClassLoader. getSystemClassLoader ());

PathClassLoader reads the dalvik-optimized dex file under the/data/Dalvik-cache directory. The dex file in this directory is generated by Dalvik when the apk package is installed. For example, if the package name is xiaogang. test, Android Application installed are stored in the/data/app directory, that is,/data/app/xiaogang.test-1.apk, then/data/dalvik-cache directory will generate data@app@xiaogang.test-1.apk @ classes. dex file. When you call PathClassLoader, it will follow this rule to find the dex file. If the specified apk file is/sdcard/test.apk, it reads/data/dalvik-cache/sdcard@test.apk @ classes according to this rule. dex file. Obviously, this file does not exist, and all PathClassLoader reports an error.

Before Google fixes this issue, we can only use DexClassLoader, or we can only use PathClassLoader to load installed apk.

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.