Android class Loader

Source: Internet
Author: User

Guo Jia
Email: [Email protected]
Blog: http://blog.csdn.net/allenwells
Github:https://github.com/allenwell

Before we introduce the class loading mechanism of Android, we need to look at the Java class loading mechanism first.

"Java Security Technology Exploration Road Series: J2SE Security Architecture" V: Class Loader

A Dalvik virtual machine with a Java virtual machine

Dalvik virtual machines Like other Java virtual machines, you first need to load the corresponding classes into memory as you run the program. In Java-standard virtual machines, class loading can be read from a class file, or it can be a binary stream of other forms. Therefore, we often take advantage of this to manually load the class when the program is running, thus achieving the purpose of code dynamic load execution.

However, Dalvik virtual machines are not standard Java virtual machines, so they have the same place and are different in the class loading mechanism. We must treat each other differently. For example, when using standard Java virtual machines, we often customize the ClassLoader that inherits from ClassLoader. The DefineClass method is then used to load the class from a binary stream. However, this is not feasible in Android,

Two Android class loaders

Android class loader mainly has two pathclassloader and Dexclassloader, where Pathclassloader is the default class loader , let's say the difference between the two and contact.

    • Pathclassloader: Support for loading Dex or an already installed apk (because there is a cached Dex).
    • Dexclassloader: Supports loading apk, Dex and Jar.

Both Dexclassloader and Pathclassloader belong to class loaders that conform to the parental delegation model (because they do not overload the LoadClass method). That is, before they load a class, they go back to check themselves and whether the class loader has already loaded the class. If it has already been loaded, it will be returned directly instead of being loaded repeatedly.

Whether Pathclassloader or Dexclassloader are inherited from Baseclassloader, let's look at the implementation of Baseclassloader first.

2.1 Baseclassloader

The source code for the "android-21" Basedexclassloader is as follows:

 PackageDalvik.system;ImportJava.io.File;ImportJava.net.URL;ImportJava.util.Enumeration; Public  class basedexclassloaderextends ClassLoader{     Public Basedexclassloader(String Dexpath, File optimizeddirectory, String LibraryPath, ClassLoader parent) {Throw NewRuntimeException ("stub!"); }protectedClass<?>Findclass(String name)throwsclassnotfoundexception {Throw NewRuntimeException ("stub!"); }protectedUrlFindResource(String name) {Throw NewRuntimeException ("stub!"); }protectedEnumeration<url>findresources(String name) {Throw NewRuntimeException ("stub!"); } PublicStringfindlibrary(String name) {Throw NewRuntimeException ("stub!"); }protected synchronizedPackageGetpackage(String name) {Throw NewRuntimeException ("stub!"); } PublicStringtoString()  {Throw NewRuntimeException ("stub!"); }}
2.2 Pathclassloader

The source code for the "android-21" Pathclassloader is as follows:

 PackageDalvik.system;ImportJava.io.File; Public  class pathclassloaderextends basedexclassloader{     Public Pathclassloader(String Dexpath, ClassLoader parent) {Super((String)NULL, (File)NULL, (String)NULL, (ClassLoader)NULL);Throw NewRuntimeException ("stub!"); } Public Pathclassloader(String Dexpath, String LibraryPath, ClassLoader parent) {Super((String)NULL, (File)NULL, (String)NULL, (ClassLoader)NULL);Throw NewRuntimeException ("stub!"); }}

As you can see from the source code, Pathclassloader has two constructors, and the parameters in the function are as follows:

    • String Dexpath: Loads the path of the APK, Dex, and Jar.
    • String LibraryPath: The Lib library that is needed to load Dex, LibraryPath generally includes/vendor/lib and/system/lib.
    • ClassLoader Parent:dexclassloader the specified parent class loader

Pathclassloader

2.3 Dexclassloader

The source code for the "android-21" Dexclassloader is as follows:

package  Dalvik.system;import  java.io.File; public   class  dexclassloader  extends  basedexclassloader  { public  dexclassloader  (String Dexpath, String optimizeddirectory, String LibraryPath, ClassLoader parent) {super  (String) null , (File) null , (String) null , (ClassLoader) null ); throw  new   RuntimeException ( "stub!" ); }}

As you can see from the source code, Dexclassloader has only one constructor, and the parameters in the function are as follows:

    • String Dexpath: Loads the path of the APK, Dex, and Jar.
    • String optimizeddirectory: Is the output path of DEX.
    • String LibraryPath: The Lib library that is needed to load Dex, LibraryPath generally includes/vendor/lib and/system/lib.
    • ClassLoader Parent:dexclassloader the specified parent class loader

You can see that Dexclassloader's constructor has a string optimizeddirectory parameter more than Pathclassloader, because Pathclassloader is the apk that loads the/data/app, This part of the APK will decompress the release Dex to the specified directory.

2.4 Dexfile

Dexclassloader and Pathclassloader actually implement class loading by Dexfile this class. By the way, the Dalvik virtual machine recognizes the Dex file instead of the class file. Therefore, the file that we are loading for the class can only be a Dex file or an. apk or. jar file that contains a Dex file.

It may be thought that since dexfile can load classes directly, why should we use ClassLoader subclasses? Dexfile when loading a class, call the Member method LoadClass or Loadclassbinaryname. Where Loadclassbinaryname requires the "." In the class name that contains the package name. Convert to "/". Let's take a look at the LoadClass code to make it clear.

The "android-21" Dexfile source code is as follows:

 PackageDalvik.system;ImportJava.io.File;ImportJava.io.FileNotFoundException;ImportJava.io.IOException;ImportJava.util.Enumeration; Public Final  class dexfile{   Public Dexfile(File file)throwsIOException {Throw NewRuntimeException ("stub!"); } Public Dexfile(String fileName)throwsIOException {Throw NewRuntimeException ("stub!"); } Public StaticDexfileLoaddex(String sourcepathname, String outputpathname,intFlagsthrowsIOException {Throw NewRuntimeException ("stub!"); } PublicStringGetName()  {Throw NewRuntimeException ("stub!"); } PublicStringtoString()  {Throw NewRuntimeException ("stub!"); } Public void Close()throwsIOException {Throw NewRuntimeException ("stub!"); } PublicClassLoadClass(String name, ClassLoader loader) {Throw NewRuntimeException ("stub!"); } PublicEnumeration<string>Entries()  {Throw NewRuntimeException ("stub!"); }protected void Finalize()throwsThrowable {Throw NewRuntimeException ("stub!"); } Public Static native Boolean isdexoptneeded(String paramstring)throwsFileNotFoundException, IOException;}
Three Android class loading mechanism 3.1 class loader structure 3.1.1 System class Loader

Example

Context.class.getClassLoader();

The result of the above code shows that the loader of the system class is Bootclassloader.

ClassLoader.getSystemClassLoader().getParent();

The above code indicates whether the system loader's parent class loader or

3.2.2 Application Loader

Example

getClassLoader();

The result of the above code shows that the application loader is Pathclassloader

getClassLoader().getParent();

The above code results show that the application's home in the Boot Hungry parent class loader is bootclassloader.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android class Loader

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.