After reading the source code dexclassloader, the native function opendexfilenative in the dexfile class will be called.
Next let's take a look at what the opendexfilenative function has done.
The opendexfilenative function exports the opendexfilenative symbol in the code in the art \ Runtime \ native \ dalvik_system_dexfile.cc file.
static JNINativeMethod gMethods[] = { NATIVE_METHOD(DexFile, closeDexFile, "(I)V"), NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"), NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"), NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)I"),};
By observing the global variables above, we can find that the function represented by opendexfilenative is dexfile_opendexfilenative.
In the dexfile_opendexfilenative function, call the dexfile: getchecksum function to obtain the checksum of the ZIP or dex file. If the checksum is obtained, the checksum is saved in the local variable dex_location_checksum.
DexFile::GetChecksum(dex_location, &dex_location_checksum)
If outputname is not null, the following code is executed to convert Dex to oat.
std::string oat_location(outputName.c_str());dex_file = linker->FindOrCreateOatFileForDexLocation(dex_location, dex_location_checksum, oat_location);
-------------------------------------------------------------------
The findorcreateoatfilefordexlocation function is a member function of the classlinker class. Let's take a look at what the findorcreateoatfilefordexlocation function has done.
The classlinker: findorcreateoatfilefordexlocation function calls the classlinker: findorcreateoatfilefordexlocationlocked function.
return FindOrCreateOatFileForDexLocationLocked(dex_location, dex_location_checksum, oat_location);
-------------------------------------------------------------------
Classlinker: In the findorcreateoatfilefordexlocationlocked function.
You can call the classlinker: finddexfileinoatlocation function to determine whether the DEX file has been optimized to an oat file.
// Check if we already have an up-to-date output fileconst DexFile* dex_file = FindDexFileInOatLocation(dex_location, dex_location_checksum, oat_location);
If Dex is not optimized to an oat file, call the classlinker: generateoatfile function to convert Dex to oat.
GenerateOatFile(dex_location, scoped_flock.GetFile().Fd(), oat_location)
In the classlinker: generateoatfile function, fork is a sub-process. In the sub-process, dex2oat is called to convert Dex to oat, and the parent process waits until the sub-process stops running.
// fork and exec dex2oatpid_t pid = fork();
execl(dex2oat, dex2oat, "--runtime-arg", "-Xms64m", "--runtime-arg", "-Xmx64m", "--runtime-arg", "-classpath", "--runtime-arg", class_path, "--runtime-arg", oat_compiler_filter_option,#if !defined(ART_TARGET) "--host",#endif boot_image_option, dex_file_option, oat_fd_option, oat_location_option, NULL);
After the classlinker: generateoatfile function is executed, the oat_dex_file-> opendexfile () function is called to open the DEX file in the memory.
const DexFile* result = oat_dex_file->OpenDexFile();
So far, the conversion from dex to oat has been completed.
How dexclassloader in art converts Dex to oat File Format