Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of ClassLoader Class loading

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of ClassLoader class loading, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Java class loader

1. BootClassLoader: used to load Android Framework layer class files.

2. PathClassLoader: used for Android application class loader. You can load classes.dex in specified dex,jar, zip, zpk

3. DexClassLoader: load the specified dex and the classes.dex in jar, zip, and apk

Source code parsing

LoadClass is provided in 1.ClassLoader to load the specified class

/ / ClassLoader.javapublic Class loadClass (String name) throws ClassNotFoundException {/ / the overloaded method return loadClass (name, false) with two parameters is called here;} protected Class loadClass (String name, boolean resolve) throws ClassNotFoundException {/ / first check whether the class has been loaded with Class c = findLoadedClass (name) If (c = = null) {try {/ / parental entrustment mechanism, first let the father go to if (parent! = null) {c = parent.loadClass (name, false);} else {/ / if parent is null, load c = findBootstrapClassOrNull (name) with BootClassLoader }} catch (ClassNotFoundException e) {/ / ClassNotFoundException thrown if class not found / / from the non-null parent class loader} if (c = = null) {/ / if you can't find it, find it yourself. This method overrides c = findClass (name) in the subclass BaseDexClassLoader class;}} return c;}

FindClass is overridden in the 2.BaseDexClassLoader class and is actually used to execute

/ / BaseDexClassLoader.java// looks for class @ Override protected Class findClass (String name) throws ClassNotFoundException {. / / here it is found through the pathList variable, while pathList is the Class c = pathList.findClass (name, suppressedExceptions) initialized in the constructor of BaseDexClassLoader. Return c;} private final DexPathList pathList;public BaseDexClassLoader (String dexPath, File optimizedDirectory, String librarySearchPath, ClassLoader parent, boolean isTrusted) {super (parent); / / initialize the pathList variable this.pathList = new DexPathList (this, dexPath, librarySearchPath, null, isTrusted) in the constructor;}

3.BaseDexClassLoader is implemented by calling findClass in DexPathList, so let's analyze how DexPathList is implemented.

/ / DexPathList.java// is an array of Element. A DexFile,DexFile in an element represents a Dex file. The native function in it is used to load the Dex. Private Element [] dexElements; public Class findClass (String name, List suppressed) {for (Element element: dexElements) {/ call Element's findClass here, Class clazz = element.findClass (name, definingContext, suppressed); if (clazz! = null) {return clazz }} return null;} / / the inner class static class Element {private final File path; / / an DexFile that is DexPathList represents a Dex file private final DexFile dexFile; / / has multiple constructors, but all of them just pass the value and let Element hold a DexFile public Element (DexFile dexFile) {this.dexFile = dexFile; this.path = null. } public Class findClass (String name, ClassLoader definingContext, List suppressed) {/ / load class return dexFile! = null? through DexFile DexFile.loadClassBinaryName (name, definingContext, suppressed): null;}} DexPathList (ClassLoader definingContext, String dexPath, String librarySearchPath, File optimizedDirectory, boolean isTrusted) {/ / initialize dexElements with the makeDexElements method this.dexElements = makeDexElements (splitDexPath (dexPath), optimizedDirectory, suppressedExceptions, definingContext, isTrusted) } / / Hot fixes of Tencent system, such as Wechat tinker and Qzone qfix, reflect this method, package the repaired class into dex, convert the file into Element by reflecting this method, and put the newly generated element in front of dexElements, so that the next time the system looks for a class, it will first look for class from the repaired dex, and then stop looking for it, thus repairing the class. This method is private static Element [] makeDexElements (List files, File optimizedDirectory, List suppressedExceptions, ClassLoader loader, boolean isTrusted) {Element [] elements = new Element [files.size ()] ... For (File file: files) {if (name.endsWith (DEX_SUFFIX)) {/ Raw dex file (not inside a zip/jar) ending with .dex. / / load the dex file dex = loadDexFile (file, optimizedDirectory, loader, elements); if (dex! = null) {elements [elementsPos++] = new Element (dex, null);}. Return elements;} private static DexFile loadDexFile (File file, File optimizedDirectory, ClassLoader loader, Element [] elements) throws IOException {if (optimizedDirectory = = null) {return new DexFile (file, loader, elements);} else {String optimizedPath = optimizedPathFor (file, optimizedDirectory); return DexFile.loadDex (file.getPath (), optimizedPath, 0, loader, elements);}}

4. Here we use new DexFile or loadDex methods to create DexFile, both of which are similar, so let's take new DexFile as an example

/ / DexFile.javaprivate DexFile (String sourceName, String outputName, int flags, ClassLoader loader, DexPathList.Element [] elements) throws IOException {. / / call openDexFile here to implement mCookie = openDexFile (sourceName, outputName, flags, loader, elements) ...} private static Object openDexFile (String sourceName, String outputName, int flags, ClassLoader loader, DexPathList.Element [] elements) throws IOException {/ / here return openDexFileNative (new File (sourceName). GetAbsolutePath (), (outputName = = null) is implemented by calling openDexFileNative? Null: new File (outputName). GetAbsolutePath (), flags, loader, elements);} / / openDexFileNative is a native method, which is implemented by private static native Object openDexFileNative (String sourceName, String outputName, int flags, ClassLoader loader, DexPathList.Element [] elements). Thank you for reading this article carefully. I hope the article "sample Analysis of ClassLoader Class loading" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report