(1)做用:
Android虚拟机运行的是Dex字节码(将Class文件合并优化生成的产物),ClassLoader用来加载dex文件。java
(2)分类
分为系统类加载器和自定义类加载器。主要的系统类加载器:android
相关源码:
/dalvik/systemapi
(1)构造函数:数组
protected ClassLoader() {
this(checkCreateClassLoader(), getSystemClassLoader());
}
protected ClassLoader(ClassLoader parent) {
this(checkCreateClassLoader(), parent);
}
复制代码
ClassLoader须要传入一个父ClassLoader,若是父ClassLoader不传的时候,经过 getSystemClassLoader方法,建立了一个PathClassLoader安全
public static ClassLoader getSystemClassLoader() {
return SystemClassLoader.loader;
}
static private class SystemClassLoader {
public static ClassLoader loader = ClassLoader.createSystemClassLoader();
}
private static ClassLoader createSystemClassLoader() {
String classPath = System.getProperty("java.class.path", ".");
String librarySearchPath = System.getProperty("java.library.path", "");
return new PathClassLoader(classPath, librarySearchPath, BootClassLoader.getInstance());
}
复制代码
(2)loadClass方法:bash
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {
// First, check if the class has already been loaded
//(1)先判断这个类是否被加载过
Class<?> c = findLoadedClass(name);
if (c == null) {
try {
//(2)若是没被加载,先让父加载器进行加载
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
//(3)若是父加载器加载失败,则自身进行加载
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
c = findClass(name);
}
}
return c;
}
复制代码
双亲委托机制:
(1)除了顶层的类加载器外,其余的类加载器都有本身的父类加载器,在加载类时首先判断这个类是否被加载过,若是已经加载则直接返回。
(2)若是未被加载过,则先尝试让父加载器进行加载,最终全部加载请求都会传递给顶层的加载器中。
(3)当父加载器发现未找到所需的类而没法完成加载请求时,子加载器的findClass方法中进行加载。ide
(1)构造:函数
public BaseDexClassLoader(String dexPath, File optimizedDirectory,
String librarySearchPath, ClassLoader parent) {
this(dexPath, librarySearchPath, parent, null, false);
}
public BaseDexClassLoader(String dexPath,
String librarySearchPath, ClassLoader parent, ClassLoader[] sharedLibraryLoaders,
boolean isTrusted) {
super(parent);
this.sharedLibraryLoaders = sharedLibraryLoaders == null
? null
: Arrays.copyOf(sharedLibraryLoaders, sharedLibraryLoaders.length);
this.pathList = new DexPathList(this, dexPath, librarySearchPath, null, isTrusted);
if (reporter != null) {
reportClassLoaderChain();
}
}
复制代码
(2)DexPathList
DexPathList中维护着一个Element数组,这个数组中Element元素就是Dex文件源码分析
#DexPathList
/*package*/ static class Element {
@UnsupportedAppUsage
private final File path;
/** Whether {@code path.isDirectory()}, or {@code null} if {@code path == null}. */
private final Boolean pathIsDirectory;
@UnsupportedAppUsage
private final DexFile dexFile;
private ClassPathURLStreamHandler urlHandler;
private boolean initialized;
@UnsupportedAppUsage
public Element(DexFile dexFile, File dexZipPath) {
if (dexFile == null && dexZipPath == null) {
throw new NullPointerException("Either dexFile or path must be non-null");
}
this.dexFile = dexFile;
this.path = dexZipPath;
// Do any I/O in the constructor so we don't have to do it elsewhere, eg. toString(). this.pathIsDirectory = (path == null) ? null : path.isDirectory(); } public Element(DexFile dexFile) { this(dexFile, null); } public Element(File path) { this(null, path); } ...... } 复制代码
Android平台上全部ClassLoader的最终parent。Android系统启动时会使用BootClassLoader来预加载经常使用类。优化
class BootClassLoader extends ClassLoader {
private static BootClassLoader instance;
@FindBugsSuppressWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
public static synchronized BootClassLoader getInstance() {
if (instance == null) {
instance = new BootClassLoader();
}
return instance;
}
...
}
复制代码
BootClassLoader的访问修饰符是默认的,只有在同一个包中才能够访问
public PathClassLoader(String dexPath, String librarySearchPath, ClassLoader parent) {
super(dexPath, null, librarySearchPath, parent);
}
@libcore.api.CorePlatformApi
public PathClassLoader(
String dexPath, String librarySearchPath, ClassLoader parent,
ClassLoader[] sharedLibraryLoaders) {
super(dexPath, librarySearchPath, parent, sharedLibraryLoaders);
}
复制代码
public DexClassLoader(String dexPath, String optimizedDirectory,
String librarySearchPath, ClassLoader parent) {
super(dexPath, null, librarySearchPath, parent);
}
复制代码
DexClassLoader能够加载dex文件以及包含dex的压缩文件(apk和jar文件),在其父类BaseDexClassLoader里对".jar",".zip",".apk",".dex"后缀的文件最后都会生成一个对应的dex文件。
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
// First, check whether the class is present in our shared libraries.
if (sharedLibraryLoaders != null) {
for (ClassLoader loader : sharedLibraryLoaders) {
try {
return loader.loadClass(name);
} catch (ClassNotFoundException ignored) {
}
}
}
// Check whether the class in question is present in the dexPath that
// this classloader operates on.
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;
}
复制代码
内部调用了DexPathList的findClass方法
#DexPathList
private Element[] dexElements;
......
public Class<?> findClass(String name, List<Throwable> suppressed) {
for (Element element : dexElements) {
Class<?> clazz = element.findClass(name, definingContext, suppressed);
if (clazz != null) {
return clazz;
}
}
if (dexElementsSuppressedExceptions != null) {
suppressed.addAll(Arrays.asList(dexElementsSuppressedExceptions));
}
return null;
}
public Class<?> findClass(String name, ClassLoader definingContext,
List<Throwable> suppressed) {
return dexFile != null ? dexFile.loadClassBinaryName(name, definingContext, suppressed)
: null;
}
复制代码
DexPathList内部包含了一个DexFile的数组dexElements。类加载的过程,就是遍历这个数组调用了DexFile的loadClassBinaryName方法,最终调用native方法defineClassNative。