[关闭]
@TryLoveCatch 2022-04-26T08:35:51.000000Z 字数 13037 阅读 556

Android知识体系之ClassLoader

Android知识体系


参考

谈谈 Android 中的 PathClassLoader 和 DexClassLoader

谈谈 Android 中的 PathClassLoader 和 DexClassLoader

预备知识

  1. 了解 android 基本 ClassLoader 知识

看完本文可以达到什么程度

  1. 了解 PathClassLoader 和 DexClassLoader 区别

文章概览

起因

说起 Android 中的 PathClassLoader 和 DexClassLoader,先提出一个疑问,PathClassLoader 和 DexClassLoader 有什么区别呢?
关于答案,我斗胆猜测一下,大家心中的回答一定是 PathClassLoader 是用来加载已经安装的 apk 的,DexClassLoader 是用来加载存储空间的 dex / apk 文件的。为什么这样说呢,因为之前我也一直这样理解的,而且网上大部分文章中也都是这样讲解的。
那为何突然又谈起 PathClassLoader 和 DexClassLoader 呢?起因是我在前段时间写了一些插件化的 demo,当时忘记了 PathClassLoader 和 DexClassLoader 这回事,直接用 PathClassLoader 去加载插件了,竟然也可以加载成功???一丝丝的困惑浮现在我英俊帅气的脸庞上,聪明的小脑瓜里打上了一个小小的问号。于是乎去翻了一下源码,就有了这篇文章。

先放结论

先放结论,PathClassLoader 和 DexClassLoader 都能加载外部的 dex/apk,只不过区别是 DexClassLoader 可以指定 optimizedDirectory,也就是 dex2oat 的产物 .odex 存放的位置,而 PathClassLoader 只能使用系统默认位置。但是这个 optimizedDirectory 在 Android 8.0 以后也被舍弃了,只能使用系统默认的位置了。

我们这里先基于 android 5.0 代码来分析,然后再看看其他系统版本的一些区别。(选取 5.0 是因为此时 art 的源码还比较简单~)

ClassLoader 的构造函数

BaseDexClassLoader 构造函数

PathClassLoader 和 DexClassLoader 都是继承了 BaseDexClassLoader,这里先看一下 BaseDexClassLoader 的构造函数。

  1. public class BaseDexClassLoader extends ClassLoader {
  2. private final DexPathList pathList;
  3. /**
  4. * Constructs an instance.
  5. *
  6. * @param dexPath the list of jar/apk files containing classes and
  7. * resources, delimited by {@code File.pathSeparator}, which
  8. * defaults to {@code ":"} on Android
  9. * @param optimizedDirectory directory where optimized dex files
  10. * should be written; may be {@code null}
  11. * @param libraryPath the list of directories containing native
  12. * libraries, delimited by {@code File.pathSeparator}; may be
  13. * {@code null}
  14. * @param parent the parent class loader
  15. */
  16. public BaseDexClassLoader(String dexPath, File optimizedDirectory,
  17. String libraryPath, ClassLoader parent) {
  18. super(parent);
  19. this.pathList = new DexPathList(this, dexPath, libraryPath, optimizedDirectory);
  20. }
  21. }

BaseDexClassLoader 构造函数有四个参数,含义如下:

通过构造函数我们大概可以了解到 BaseDexClassLoader 的运行方式,传入 dex 文件,然后进行优化,保存优化后的 dex 文件到 optimizedDirectory 目录。

PathClassLoader 构造函数

接着我们再看 PathClassLoader 的构造函数。

  1. /**
  2. * Provides a simple {@link ClassLoader} implementation that operates on a list
  3. * of files and directories in the local file system, but does not attempt to
  4. * load classes from the network. Android uses this class for its system class
  5. * loader and for its application class loader(s).
  6. */
  7. public class PathClassLoader extends BaseDexClassLoader {
  8. public PathClassLoader(String dexPath, ClassLoader parent) {
  9. super(dexPath, null, null, parent);
  10. }
  11. /**
  12. * Creates a {@code PathClassLoader} that operates on two given
  13. * lists of files and directories. The entries of the first list
  14. * should be one of the following:
  15. *
  16. * <ul>
  17. * <li>JAR/ZIP/APK files, possibly containing a "classes.dex" file as
  18. * well as arbitrary resources.
  19. * <li>Raw ".dex" files (not inside a zip file).
  20. * </ulyanzheng>
  21. *
  22. * The entries of the second list should be directories containing
  23. * native library files.
  24. *
  25. * @param dexPath the list of jar/apk files containing classes and
  26. * resources, delimited by {@code File.pathSeparator}, which
  27. * defaults to {@code ":"} on Android
  28. * @param libraryPath the list of directories containing native
  29. * libraries, delimited by {@code File.pathSeparator}; may be
  30. * {@code null}
  31. * @param parent the parent class loader
  32. */
  33. public PathClassLoader(String dexPath, String libraryPath,
  34. ClassLoader parent) {
  35. super(dexPath, null, libraryPath, parent);
  36. }
  37. }

关于 PathClassLoader 有一点稍微注意一下,代码注释中对 PathClassLoader 的介绍是,用来操作文件系统上的一系列文件和目录 的 ClassLoader 实现。其中并没有提到只能加载安装后的 apk 文件。
PathClassLoader 有两个构造函数,区别在于传给 BaseDexClassLoader 的 libraryPath 是否为空。最终调用 BaseDexClassLoader 构造函数时,传入的 optimizedDirectory 为空。

DexClassLoader 构造函数

再来看看 DexClassLoader 的构造函数。和 BaseDexClassLoader 构造函数的参数是一样的。

  1. public class DexClassLoader extends BaseDexClassLoader {
  2. /**
  3. * Creates a {@code DexClassLoader} that finds interpreted and native
  4. * code. Interpreted classes are found in a set of DEX files contained
  5. * in Jar or APK files.
  6. *
  7. * <p>The path lists are separated using the character specified by the
  8. * {@code path.separator} system property, which defaults to {@code :}.
  9. *
  10. * @param dexPath the list of jar/apk files containing classes and
  11. * resources, delimited by {@code File.pathSeparator}, which
  12. * defaults to {@code ":"} on Android
  13. * @param optimizedDirectory directory where optimized dex files
  14. * should be written; must not be {@code null}
  15. * @param librarySearchPath the list of directories containing native
  16. * libraries, delimited by {@code File.pathSeparator}; may be
  17. * {@code null}
  18. * @param parent the parent class loader
  19. */
  20. public DexClassLoader(String dexPath, String optimizedDirectory,
  21. String librarySearchPath, ClassLoader parent) {
  22. super(dexPath, new File(optimizedDirectory), librarySearchPath, parent);
  23. }
  24. }

通过上面对构造函数的分析,我们可以明白,PathClassLoader 和 DexClassLoader 关键不同点,在 optimizedDirectory 参数上,PathClassLoader 传入的是 null,而 DexClassLoader 传入的是用户指定的目录。

optimizedDirectory 参数的处理

既然知道了区别在 optimizedDirectory,那就来看看 BaseDexClassLoader 里是怎么处理 optimizedDirectory 的。

DexPathList 处理

在 BaseDexClassLoader 里,直接将 optimizedDirectory 透传给了 DexPathList。
这里先简单介绍一下 DexPathList。
DexPathList 里有两个成员变量,dexElements 用来保存 dex 和资源列表,nativeLibraryDirectories 用来保存 native 库列表。

  1. class DexPathList {
  2. private final Element[] dexElements;
  3. private final File[] nativeLibraryDirectories;
  4. }

在 DexPathList 中,使用 optimizedDirectory 的路径是:

  1. DexPathList -> makeDexElements -> loadDexFile

这里要看一下 loadDexFile 方法。

  1. class DexPathList {
  2. private static DexFile loadDexFile(File file, File optimizedDirectory)
  3. throws IOException {
  4. if (optimizedDirectory == null) {
  5. return new DexFile(file);
  6. } else {
  7. String optimizedPath = optimizedPathFor(file, optimizedDirectory);
  8. return DexFile.loadDex(file.getPath(), optimizedPath, 0);
  9. }
  10. }
  11. }

在 DexPathList 中,会为每一个 DEX 文件创建一个 DexFile 对象,创建方式有两种,optimizedDirectory 为空时,调用 DexFile(file) 创建,否则调用 DexFile.loadDex()。
这样对于 optimizedDirectory 的处理就流转到 DexFile 里了。

DexFile 处理

https://www.androidos.net.cn/android/5.0.1_r1/xref/libcore/dalvik/src/main/java/dalvik/system/DexFile.java
其实在 DexFile.loadDex 里,也是直接调用了 DexFile 的构造函数

  1. class DexFile {
  2. public DexFile(File file) throws IOException {
  3. this(file.getPath());
  4. }
  5. public DexFile(String fileName) throws IOException {
  6. // 调用 openDexFile 处理 dex
  7. mCookie = openDexFile(fileName, null, 0);
  8. mFileName = fileName;
  9. guard.open("close");
  10. }
  11. private DexFile(String sourceName, String outputName, int flags) throws IOException {
  12. // ...
  13. // 调用 openDexFile 处理 dex
  14. mCookie = openDexFile(sourceName, outputName, flags);
  15. mFileName = sourceName;
  16. guard.open("close");
  17. }
  18. static public DexFile loadDex(String sourcePathName, String outputPathName,
  19. int flags) throws IOException {
  20. return new DexFile(sourcePathName, outputPathName, flags);
  21. }
  22. private static long openDexFile(String sourceName, String outputName, int flags) throws IOException {
  23. // 最终调用 native 方法
  24. return openDexFileNative(new File(sourceName).getAbsolutePath(),
  25. (outputName == null) ? null : new File(outputName).getAbsolutePath(),
  26. flags);
  27. }
  28. private static native long openDexFileNative(String sourceName, String outputName, int flags);
  29. }

DexFile 代码不多,上面基本上就是主要代码了。我们可以看到,不管调用 DexFile 哪个构造函数,最后都会通过 openDexFileNative 进行处理,区别就在于 outputName 参数是否为空,而 outputName 参数,就是上面一路传递下来的 optimizeDirectory 参数。
我们再回顾一下调用的链路:

  1. PathClassLoader.constructor / DexClassLoader.constructor -> BaseDexClassLoader.constructor -> DexPathList.constructor -> DexPathList.makeDexElements -> DexPathList.loadDexFile -> DexFile.constructor / DexFile.loadDex -> DexFile.openDexFile -> DexFile.openDexFileNative

再继续往下看,就走到了 native 逻辑。native 逻辑可以下载 art 源码对照查看。

native 处理

openDexFileNative 对应的 native 逻辑在 dalvik_system_DexFile.cc 里的 DexFile_openDexFileNative 方法。
在 DexFile_openDexFileNative 里主要做事情是处理 DEX 文件,并生成 .odex 文件到 optimizedDirectory 里。
这里关于 optimizedDirectory 的处理路径是:

  1. DexFile_openDexFileNative -> ClassLinker::OpenDexFilesFromOat

在 OPenDexFilesFromOat 里有这样一段处理逻辑:

  1. ClassLinker::OpenDexFilesFromOat() {
  2. // ...
  3. if (oat_location == nullptr) {
  4. // 如果 oat_location 为空,就使用默认的 dalvikcache
  5. const std::string dalvik_cache(GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA)));
  6. cache_location = GetDalvikCacheFilenameOrDie(dex_location, dalvik_cache.c_str());
  7. oat_location = cache_location.c_str();
  8. }
  9. // ...
  10. if (Runtime::Current()->IsDex2OatEnabled() && has_flock && scoped_flock.HasFile()) {
  11. // Create the oat file.
  12. open_oat_file.reset(CreateOatFileForDexLocation(dex_location, scoped_flock.GetFile()->Fd(),
  13. oat_location, error_msgs));
  14. }
  15. }

上面方法里的 oat_location 就是 optimizeDirectory 传入到 native 中的化身。这里有一个判断逻辑,如果 oat_location 为空的话,就采用默认的 dalvikcache 路径。之后调用 CreateOatFileForDexLocation 去优化 DEX 文件了。
而 dalvikcache 是通过 GetDalvikCacheOrDie 获取的。

  1. // art/runtime/utils.cc
  2. std::string GetDalvikCacheOrDie(const char* subdir, const bool create_if_absent) {
  3. CHECK(subdir != nullptr);
  4. // 这里的 AndroidData 就是 /data 目录
  5. const char* android_data = GetAndroidData();
  6. const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data));
  7. const std::string dalvik_cache = dalvik_cache_root + subdir;
  8. if (create_if_absent && !OS::DirectoryExists(dalvik_cache.c_str())) {
  9. // Don't create the system's /data/dalvik-cache/... because it needs special permissions.
  10. if (strcmp(android_data, "/data") != 0) {
  11. int result = mkdir(dalvik_cache_root.c_str(), 0700);
  12. if (result != 0 && errno != EEXIST) {
  13. PLOG(FATAL) << "Failed to create dalvik-cache directory " << dalvik_cache_root;
  14. return "";
  15. }
  16. result = mkdir(dalvik_cache.c_str(), 0700);
  17. if (result != 0) {
  18. PLOG(FATAL) << "Failed to create dalvik-cache directory " << dalvik_cache;
  19. return "";
  20. }
  21. } else {
  22. LOG(FATAL) << "Failed to find dalvik-cache directory " << dalvik_cache;
  23. return "";
  24. }
  25. }
  26. return dalvik_cache;
  27. }

GetDalvikCacheOrDie 获取的就是 /data/dalvik-cache/ 目录。
这里我们回顾一下之前提出的问题,避免迷失在茫茫代码中。
我们的问题是 optimizedDirectory 参数传空和不为空有什么区别,PathClassLoader 传入的 optmizedDirectory 为空,而 DexClassLoader 传入的 optimizedDirectory 是用户自定义的目录。
回看一下调用链路。

  1. PathClassLoader.constructor / DexClassLoader.constructor -> BaseDexClassLoader.constructor -> DexPathList.constructor -> DexPathList.makeDexElements -> DexPathList.loadDexFile -> DexFile.constructor / DexFile.loadDex -> DexFile.openDexFile -> DexFile.openDexFileNative -> DexFile_openDexFileNative -> ClassLinker::OpenDexFilesFromOat

到这里我们就可以得出结论了,optmizedDirectory 不为空时,使用用户定义的目录作为 DEX 文件优化后产物 .odex 的存储目录,为空时,会使用默认的 /data/dalvik-cache/ 目录。
所以印证了开头的结论,PathClassLoader 其实并不是只能加载安装后的 APK,也可以加载其他 DEX/JAR/APK 文件,只不过生成的 .odex 文件只能存储在系统默认路径下。
被误导多年的谜题终于解开了。耳边不禁响起柯南破案的 BGM。

其他系统版本上进行验证

不过上述的分析是在 5.0 源码下进行的,我们再选取 4.4 和 8.0 看一下。为什么选取这两个版本呢?首先 4.4 和 5.0 是 ART 和 Dalvik 的分水岭,而 8.0 以后对 PathClassLoader 有些改动。

Android 4.4

有了上面的分析基础,我们分析 4.4 的代码就顺畅的多了。一路从 Java 分析到 native。
Java 层代码没有什么变动,native 的入口还是 DexFile_openDexFileNative。之后的代码就有了些许不一样。

  1. DexFile_openDexFileNative() {
  2. // ...
  3. if (outputName.c_str() == NULL) {
  4. dex_file = linker->FindDexFileInOatFileFromDexLocation(dex_location, dex_location_checksum);
  5. } else {
  6. std::string oat_location(outputName.c_str());
  7. dex_file = linker->FindOrCreateOatFileForDexLocation(dex_location, dex_location_checksum, oat_location);
  8. }
  9. // ...
  10. }

这里和 5.0 的区别就是 根据 outputName 也就是 optimizedDirectory 是否为空,调用了两个不同的函数。
而 FindDexFileInOatFileFromDexLocation 里的逻辑就又有些熟悉了。

  1. ClassLinker::FindDexFileInOatFileFromDexLocation() {
  2. // ...
  3. std::string oat_cache_filename(GetDalvikCacheFilenameOrDie(dex_location));
  4. return FindOrCreateOatFileForDexLocationLocked(dex_location, dex_location_checksum, oat_cache_filename);
  5. }

默认也是获取到 dalvikcache 目录作为 .odex 文件的存储路径。

Android 8.0

在 8.0 系统上,事情发生了一些微弱的变化,我们看看 BaseDexClassLoader 的构造函数。

  1. class BaseDexClassLoader {
  2. /**
  3. * Constructs an instance.
  4. * Note that all the *.jar and *.apk files from {@code dexPath} might be
  5. * first extracted in-memory before the code is loaded. This can be avoided
  6. * by passing raw dex files (*.dex) in the {@code dexPath}.
  7. *
  8. * @param dexPath the list of jar/apk files containing classes and
  9. * resources, delimited by {@code File.pathSeparator}, which
  10. * defaults to {@code ":"} on Android.
  11. * @param optimizedDirectory this parameter is deprecated and has no effect
  12. * @param librarySearchPath the list of directories containing native
  13. * libraries, delimited by {@code File.pathSeparator}; may be
  14. * {@code null}
  15. * @param parent the parent class loader
  16. */
  17. public BaseDexClassLoader(String dexPath, File optimizedDirectory,
  18. String librarySearchPath, ClassLoader parent) {
  19. super(parent);
  20. this.pathList = new DexPathList(this, dexPath, librarySearchPath, null);
  21. if (reporter != null) {
  22. reporter.report(this.pathList.getDexPaths());
  23. }
  24. }
  25. }

一个很明显的变化就是,optimizedDirectory 被弃用了,传给 DexPathList 的 optimizedDirectory 直接为空,不管外面传进来什么值。
也就是说,在 8.0 上,PathClassLoader 和 DexClassLoader 其实已经没有什么区别了。DexClassLoader 也不能指定 optimizedDirectory 了。

而在 DexFile_openDexFileNative 中,可以看到,javaOutputName 参数也已经被弃用了。

  1. static jobject DexFile_openDexFileNative(JNIEnv* env,
  2. jclass,
  3. jstring javaSourceName,
  4. jstring javaOutputName ATTRIBUTE_UNUSED,
  5. jint flags ATTRIBUTE_UNUSED,
  6. jobject class_loader,
  7. jobjectArray dex_elements) {
  8. }

之后对 DEX 文件的处理链路如下:

  1. DexFile_openDexFileNative -> DexLocationToOdexNames -> OatFileManager::OpenDexFilesFromOat -> OatFileAssistant::OatFileAssistant -> OatFileAssistant::DexLocationToOdexFilename -> DexLocationToOdexNames

在 DexLocationToOdexNames 方法里,对 .odex 文件的路径做了处理

  1. static bool DexLocationToOdexNames(const std::string& location,
  2. InstructionSet isa,
  3. std::string* odex_filename,
  4. std::string* oat_dir,
  5. std::string* isa_dir,
  6. std::string* error_msg) {
  7. CHECK(odex_filename != nullptr);
  8. CHECK(error_msg != nullptr);
  9. // The odex file name is formed by replacing the dex_location extension with
  10. // .odex and inserting an oat/<isa> directory. For example:
  11. // location = /foo/bar/baz.jar
  12. // odex_location = /foo/bar/oat/<isa>/baz.odex
  13. // Find the directory portion of the dex location and add the oat/<isa>
  14. // directory.
  15. size_t pos = location.rfind('/');
  16. if (pos == std::string::npos) {
  17. *error_msg = "Dex location " + location + " has no directory.";
  18. return false;
  19. }
  20. std::string dir = location.substr(0, pos+1);
  21. // Add the oat directory.
  22. dir += "oat";
  23. if (oat_dir != nullptr) {
  24. *oat_dir = dir;
  25. }
  26. // Add the isa directory
  27. dir += "/" + std::string(GetInstructionSetString(isa));
  28. if (isa_dir != nullptr) {
  29. *isa_dir = dir;
  30. }
  31. // Get the base part of the file without the extension.
  32. std::string file = location.substr(pos+1);
  33. pos = file.rfind('.');
  34. if (pos == std::string::npos) {
  35. *error_msg = "Dex location " + location + " has no extension.";
  36. return false;
  37. }
  38. std::string base = file.substr(0, pos);
  39. *odex_filename = dir + "/" + base + ".odex";
  40. return true;
  41. }

看到上面的处理就是在 DEX 文件同级目录下添加一个 oat/ 文件作为 .odex 的存储目录。

总结


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注