public class PathClassLoader extends BaseDexClassLoader {
public PathClassLoader(String dexPath, ClassLoader parent) { super(dexPath, null, null, parent); } public PathClassLoader(String dexPath, String librarySearchPath, ClassLoader parent){ super(dexPath, null, librarySearchPath, parent); }
}
You can see that the only difference between the two is: create`DexClassLoader`Need to pass a`optimizedDirectory`Parameter, and it is created as`File`Object passed to`super`,and`PathClassLoader`Directly to null. Therefore, both can be loaded**designated dex,as well as jar,zip,apk Medium classes.dex** ```java PathClassLoader pathClassLoader = new PathClassLoader("/sdcard/xx.dex", getClassLoader()); File dexOutputDir = context.getCodeCacheDir(); DexClassLoader dexClassLoader = new DexClassLoader("/sdcard/xx.dex",dexOutputDir.getAbsolutePath(), null,getClassLoader());
The directory whose optimized directory parameter is odex. In fact, when loading dex, ClassLoader in Android will first optimize dex through dexopt to generate odex files. The default path when the optimized directory is null is: * * * / data / Dalvik cache * * *. And for security reasons, this directory needs to use the app private directory, such as getCodeCacheDir()
In the API 26 source code, mark the optimized directory of DexClassLoader as deprecated and discard it. The implementation also changes to:
public DexClassLoader(String dexPath, String optimizedDirectory, String librarySearchPath, ClassLoader parent) { super(dexPath, null, librarySearchPath, parent); }
It's the same as PathClassLoader!
Parental entrustment mechanism
establish`ClassLoader`Need to receive one`ClassLoader parent`Parameters. this`parent`Load for parent class. That is, when a class loader receives a request to load a class, it first delegates the loading task to the parent class loader and recurses in turn. If the parent class loader can complete the class loading task, it returns successfully; Only when the parent class loader is unable to complete this loading task can it load itself. This is it.**Parental entrustment mechanism**!
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException{ // Check whether the class is loaded Class c = findLoadedClass(name); if (c == null) { long t0 = System.nanoTime(); try { if (parent != null) { //If the parent is not null, call the parent's loadClass to load c = parent.loadClass(name, false); } else { //If the parent is null, call BootClassLoader to load c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { } if (c == null) { // If you can't find it, find it yourself long t1 = System.nanoTime(); c = findClass(name); } } return c; }
Therefore, we created our own ClassLoader: new PathClassLoader("/sdcard/xx.dex", getClassLoader()); Not just XX The Class in dex can also obtain the Class loaded in its parent ClassLoader.
findClass
In all parents ClassLoader Unable to load Class When, it will call its own`findClass`method.`findClass`stay ClassLoader Is defined as:
protected Class<?> findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); }
Actually any ClassLoader Subclasses can be overridden`loadClass`And`findClass`. Generally, if you don't want to use parent delegation, override it`loadClass`Modify its implementation. And rewrite`findClass`It means that under the entrustment of both parents, the parent ClassLoader Can't find it Class In this case, define how to find one Class. And our`PathClassLoader`It will be loaded by itself`MainActivity`Such programs write their own classes, using parents to delegate parents ClassLoader load Framework Medium`Activity`. explain`PathClassLoader`Not rewritten`loadClass`,So we can take a look PathClassLoader Medium `findClass` How to achieve it.
public BaseDexClassLoader(String dexPath, File optimizedDirectory,String librarySearchPath, ClassLoader parent) { super(parent); this.pathList = new DexPathList(this, dexPath, librarySearchPath, optimizedDirectory); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { List<Throwable> suppressedExceptions = new ArrayList<Throwable>(); //Find the specified class Class c = pathList.findClass(name, suppressedExceptions); if (c == null) { ClassNotFoundException cnfe = new ClassNotFoundException("Didn't find class \"" + name + "\" on path: " + pathList); for (Throwable t : suppressedExceptions) { cnfe.addSuppressed(t); } throw cnfe; } return c; }
The implementation is very simple, from`pathList`Find in class. Continue viewing`DexPathList`
public DexPathList(ClassLoader definingContext, String dexPath, String librarySearchPath, File optimizedDirectory) { //......... // splitDexPath is implemented to return list < File > add(dexPath) // makeDexElements will go to list < File > Use DexFile in add (dexpath) to load the dex file and return the Element array this.dexElements = makeDexElements(splitDexPath(dexPath), optimizedDirectory, suppressedExceptions, definingContext); //......... } public Class findClass(String name, List<Throwable> suppressed) { //Get the DexFile representing Dex from element for (Element element : dexElements) { DexFile dex = element.dexFile; if (dex != null) { //Find class Class clazz = dex.loadClassBinaryName(name, definingContext, suppressed); if (clazz != null) { return clazz; } } } if (dexElementsSuppressedExceptions != null) { suppressed.addAll(Arrays.asList(dexElementsSuppressedExceptions)); } return null; }
hot fix
`PathClassLoader`There is one in Element Array, Element One exists in the class`dexFile`Member representation dex Documents, i.e.: APK There are X individual dex,be Element Array X Elements.
For class lookup, the code`for (Element element : dexElements) `We know that the array will look up from front to back.
Summary: draw a brain map of Kakfa framework Thinking Outline (xmind)
In fact, there are too many questions you can ask about Kafka. After a few days, you finally screened out 44 questions: 17 questions in the basic chapter, 15 questions in the advanced chapter and 12 questions in the advanced chapter. All of them poke pain points. I don't know how many questions you can answer if you don't look at the answers in a hurry?
If you can't recall Kafka's knowledge, you might as well look at my hand-painted knowledge summary brain map (xmind can't be uploaded, but the picture version is used in the article) to sort out the overall structure
Data collection method: click here to download for free
After combing the knowledge and finishing the interview, if you want to further study and interpret kafka and the source code, the next handwritten "kafka" will be a good choice.
-
Introduction to Kafka
-
Why Kafka
-
Installation, management and configuration of Kafka
-
Kafka's cluster
-
The first Kafka program
-
Producers of Kafka
-
Kafka consumers
-
Deep understanding of Kafka
-
Reliable data transfer
-
Integration of Spring and Kafka
-
Integration of SpringBoot and Kafka
-
Kafka's practice of cutting peaks and filling valleys
-
Data pipeline and streaming (just understand)
Producers of Kafka
-
Kafka consumers
-
Deep understanding of Kafka
-
Reliable data transfer
-
Integration of Spring and Kafka
-
Integration of SpringBoot and Kafka
-
Kafka's practice of cutting peaks and filling valleys
-
Data pipeline and streaming (just understand)
[external chain picture transferring... (img-2I0nCH70-1628482566683)]