Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

A, BaseDexClassLoader

How was DexElements created? Implementation details for makeDexElements:

1. How to create DexElements

The BaseDexClassLoader class has three constructors, and only two of them are analyzed (because the other one is just an extension of one, there is no change in how DexElements are loaded). First of all, the parent can’t be omitted. The following attributes are two different ways to create DexElements, one of which is to pass in a bytes array and the other two are to use dexFilePath. (The constructor also takes a lot of arguments, but is not necessary for this problem.) MakeInMemoryDexElements is called for bytes arrays, and makeDexElements is called for dexPath arrays to create DexElements.

2. Implementation details:

When you create a ClassLoader, you create a DexElement array, that is, a Dex array. When it’s created, it creates a set of exception types and it adds them to the set if there’s an error loading DexFile, and then the constructor executes and reads the data from the set of exceptions and saves it to a local variable. There are also two cases of creating an Element array based on the ClassLoader’s two constructors:

If it is dexPath, the File directories of the passed DexClassLoader will be parsed into a set of files, and makeDexElements will be called to create an element array.

So Dex is Element and three cases are added to the Element array:

Create an Element by traversing the folder and passing in the traversal parameter File

If the file ends in dex, call loadDexFile to load the DexFile structure. If the file is not empty, then create Element and pass in the DexFile structure and a null(this represents that the file with the suffix dex was successfully loaded into memory)3. If the File is traversed but does not end in a. DexFile, loadDexFile is still called to load the DexFile structure, creating an Element whether it is empty or not, but passing the traversal parameter File if it is empty. If not empty, create the dexFile attribute and pass in the parameter File(this represents whether the File with the.dex suffix was successfully loaded into memory).

Element has two constructors: the first constructor takes a parsed dexFile as a File object. The second constructor takes one more argument than the first constructor. The first argument is a dexFile object

If it is a byte array, DexFile is manually created and loadDexFile is used instead of dexPath. In this case, Element is created directly (the verification process is delayed because it has been converted to bytes), that is, the. Dex file is successfully loaded into memory

LoadDexFile implementation details

If the optimized odex file path is not passed, create a DexFile (traversed dex file, classloader, Element array after dex conversion) and return it. [Element stored in the DexFile object is passed in when it is created]

If you enter the odex directory, use dexfile. loadDex to create a DexFile structure.

Finally, it will enter the Native layer to load