首页 > 学院 > 开发设计 > 正文

插件化知识详细分解及原理 之ClassLoader及dex加载过程

2019-11-06 10:03:25
字体:
来源:转载
供稿:网友

上一篇插件化知识详细分解及原理 之代理,hook,反射,接着上篇我们这一篇说ClassLoader及dex加载过程。

为了解决65535这个问题,Google提出了multidex方案,即一个apk文件可以包含多个dex文件。 不过值得注意的是,除了第一个dex文件以外,其他的dex文件都是以资源的形式被加载的, 换句话说就是在application初始化前将dex文件注入到系统的ClassLoader中的。 根据Android虚拟机的类加载机制,同一个类只会被加载一次,所以热修复也使用了这样的机制,要让修复后的类替换原有的类就必须让补丁包的类被优先加载,也就是插入到原有dex之前。

首先需要知道的是android中两个主要的Classloader,PathClassLoader和DexClassLoader, 它们都继承自BaseDexClassLoader,这两个类有什么区别呢?其实看一下它们的源码注释就一目了然了。

Android系统通过PathClassLoader来加载系统类和主dex中的类。 而DexClassLoader则用于加载其他dex文件中的类。 他们都是继承自BaseDexClassLoader,具体的加载方法是findClass。

为什么说PathClassLoader只能加载系统类和主dex的类呢,我们看一下这个类的源代码 所有ClassLoader的源码都在/libcore/dalvik/src/main/java/dalvik/system/目录下

PathClassLoader: /** * PRovides a simple {@link ClassLoader} implementation that Operates on a list * of files and directories in the local file system, but does not attempt to * load classes from the network. Android uses this class for its system class * loader and for its application class loader(s). */ public class PathClassLoader extends BaseDexClassLoader { /** * Creates a {@code PathClassLoader} that operates on a given list of files * and directories. This method is equivalent to calling * {@link #PathClassLoader(String, String, ClassLoader)} with a * {@code null} value for the second argument (see description there). * * @param dexPath the list of jar/apk files containing classes and * resources, delimited by {@code File.pathSeparator}, which * defaults to {@code ":"} on Android * @param parent the parent class loader */ public PathClassLoader(String dexPath, ClassLoader parent) { super(dexPath, null, null, parent); } /** * Creates a {@code PathClassLoader} that operates on two given * lists of files and directories. The entries of the first list * should be one of the following: * * <ul> * <li>JAR/ZIP/APK files, possibly containing a "classes.dex" file as * well as arbitrary resources. * <li>Raw ".dex" files (not inside a zip file). * </ul> * * The entries of the second list should be directories containing * native library files. * * @param dexPath the list of jar/apk files containing classes and * resources, delimited by {@code File.pathSeparator}, which * defaults to {@code ":"} on Android * @param libraryPath the list of directories containing native * libraries, delimited by {@code File.pathSeparator}; may be * {@code null} * @param parent the parent class loader */ public PathClassLoader(String dexPath, String libraryPath, ClassLoader parent) { super(dexPath, null, libraryPath, parent); } }

我们翻译一下他的注释,大概的意思是说 PathClassLoader被用来加载本地文件系统上的文件或目录,但不能从网络上加载, 关键是它被用来加载系统类和我们的应用程序,这也是为什么它的两个构造函数中调用父类构造器的时候第二个参数传null, 那第二个参数代表什么呢我们看一下DexClassLoader的源码

DexClassLoader: /** * A class loader that loads classes from {@code .jar} and {@code .apk} files * containing a {@code classes.dex} entry. This can be used to execute code not * installed as part of an application. * * <p>This class loader requires an application-private, writable directory to * cache optimized classes. Use {@code Context.getDir(String, int)} to create * such a directory: <pre> {@code * File dexOutputDir = context.getDir("dex", 0); * }</pre> * * <p><strong>Do not cache optimized classes on external storage.</strong> * External storage does not provide access controls necessary to protect your * application from code injection attacks. */ public class DexClassLoader extends BaseDexClassLoader { /** * Creates a {@code DexClassLoader} that finds interpreted and native * code. Interpreted classes are found in a set of DEX files contained * in Jar or APK files. * * <p>The path lists are separated using the character specified by the * {@code path.separator} system property, which defaults to {@code :}. * * @param dexPath the list of jar/apk files containing classes and * resources, delimited by {@code File.pathSeparator}, which * defaults to {@code ":"} on Android * @param optimizedDirectory directory where optimized dex files * should be written; must not be {@code null} * @param libraryPath the list of directories containing native * libraries, delimited by {@code File.pathSeparator}; may be * {@code null} * @param parent the parent class loader */ public DexClassLoader(String dexPath, String optimizedDirectory, String libraryPath, ClassLoader parent) { super(dexPath, new File(optimizedDirectory), libraryPath, parent); } }

看一下构造上的注释,参数解释如下:

dexPath: 需要被加载的dex文件地址,可以多个,用File.pathSeparator分割

optimizedDirectory: dex文件被加载后会被编译器优化,优化之后的dex存放路径, 不可以为null。注意,注释中也提到需要一个应用私有的可写的一个路径, 以防止应用被注入攻击,并且给出了例子 File dexOutputDir = context.getDir(“dex”, 0);

libraryPath:包含libraries的目录列表,同样用File.pathSeparator分割,如果没有则传null就行了

parent:父类构造器

到这里估计会问不是说了optimizedDirectory不能传null吗,那PathClassLoader怎么传了null呢, 我们继续看一下BaseClassLoader的findClass,

public class BaseDexClassLoader extends ClassLoader { private final DexPathList pathList; public BaseDexClassLoader(String dexPath, File optimizedDirectory, String libraryPath, ClassLoader parent) { super(parent); this.pathList = new DexPathList(this, dexPath, libraryPath, optimizedDirectory); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { List<Throwable> suppressedExceptions = new ArrayList<Throwable>(); 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; } }

构造方法中创建了一个叫pathList的DexPathList类型的对象,然后在findClass的时候 代码转移到了pathList的findClass中,DexPathList又是什么呢我们进去看一下。

/** * Constructs an instance. * * @param definingContext the context in which any as-yet unresolved * classes should be defined * @param dexPath list of dex/resource path elements, separated by * {@code File.pathSeparator} * @param libraryPath list of native library directory path elements, * separated by {@code File.pathSeparator} * @param optimizedDirectory directory where optimized {@code .dex} files * should be found and written to, or {@code null} to use the default * system directory for same */ public DexPathList(ClassLoader definingContext, String dexPath, String libraryPath, File optimizedDirectory) { if (definingContext == null) { throw new NullPointerException("definingContext == null"); } if (dexPath == null) { throw new NullPointerException("dexPath == null"); } if (optimizedDirectory != null) { if (!optimizedDirectory.exists()) { throw new IllegalArgumentException( "optimizedDirectory doesn't exist: " + optimizedDirectory); } if (!(optimizedDirectory.canRead() && optimizedDirectory.canWrite())) { throw new IllegalArgumentException( "optimizedDirectory not readable/writable: " + optimizedDirectory); } } this.definingContext = definingContext; this.dexElements = makeDexElements(splitDexPath(dexPath), optimizedDirectory); this.nativeLibraryDirectories = splitLibraryPath(libraryPath); }

我们先翻一下一下注释,第四个参数说optimizedDirectory 如果为空, 那么使用系统默认的文件夹,为什么PathClassLoader传空就行呢,其实是 因为我们的应用已经安装并优化了,优化后的dex存在于/data/dalvik-cache目录下,这就是系统默认的文件夹, 这就是说为什么我们只能用DexClassLoader去加载其他类了

我们接着看一下最后两句代码

this.dexElements = makeDexElements(splitDexPath(dexPath), optimizedDirectory); this.nativeLibraryDirectories = splitLibraryPath(libraryPath);

赋值了一个dexElements的数组,这个其实就是存放我们dex文件的数组, nativeLibraryDirectories 就是lib库了,我们看一下makeDexElements方法

/** * Makes an array of dex/resource path elements, one per element of * the given array. */ private static Element[] makePathElements(List<File> files, File optimizedDirectory, List<IOException> suppressedExceptions) { List<Element> elements = new ArrayList<>(); /* * Open all files and load the (direct or contained) dex files * up front. */ for (File file : files) { File zip = null; File dir = new File(""); DexFile dex = null; String path = file.getPath(); String name = file.getName(); if (path.contains(zipSeparator)) { String split[] = path.split(zipSeparator, 2); zip = new File(split[0]); dir = new File(split[1]); } else if (file.isDirectory()) { // We support directories for looking up resources and native libraries. // Looking up resources in directories is useful for running libcore tests. elements.add(new Element(file, true, null, null)); } else if (file.isFile()) { if (name.endsWith(DEX_SUFFIX)) { // Raw dex file (not inside a zip/jar). try { dex = loadDexFile(file, optimizedDirectory); } catch (IOException ex) { System.logE("Unable to load dex file: " + file, ex); } } else { zip = file; try { dex = loadDexFile(file, optimizedDirectory); } catch (IOException suppressed) { /* * IOException might get thrown "legitimately" by the DexFile constructor if * the zip file turns out to be resource-only (that is, no classes.dex file * in it). * Let dex == null and hang on to the exception to add the tea-leaves for * when findClass returns null. */ suppressedExceptions.add(suppressed); } } } else { System.logW("ClassLoader referenced unknown path: " + file); } if ((zip != null) || (dex != null)) { elements.add(new Element(dir, false, zip, dex)); } } return elements.toArray(new Element[elements.size()]); }

方法将加载dex文件,并创建Element类型加入到elements数组中,我们再看findClass方法,因为在BaseClassLoader的findClass方法调用了DexPathList的findClass方法

public Class findClass(String name, List<Throwable> suppressed) { for (Element element : dexElements) { DexFile dex = element.dexFile; if (dex != null) { Class clazz = dex.loadClassBinaryName(name, definingContext, suppressed); if (clazz != null) { return clazz; } } } if (dexElementsSuppressedExceptions != null) { suppressed.addAll(Arrays.asList(dexElementsSuppressedExceptions)); } return null;}

DexPathList的findClass也很简单,dexElements是维护dex文件的数组, 每一个item对应一个dex文件。DexPathList遍历dexElements,从每一个dex文件中查找目标类,在找到后即返回并停止遍历。

好了,我们的dex大概加载过程就很清楚了。本篇说的内容并没有什么技术含量,只是顺着一条主线梳理了下整个过程,对于搞明白一些细节可能还是有点帮助。下一篇我们说应用的启动过程。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表