追踪bug的时候由于问题是处在类加载器上,所以今天看了下android的类加载过程,总得来说,类加载原理和jvm是相同的,都是采用双亲委派模型(深入jvm里有讲),简单地说就是一个类加载器在加载一个类时,先交给它的父加载器,一直到BootClassLoader,如果它的父加载器没有找到相应的class文件,那么再把加载任务传给孩子,一层层往下传,直到找到相应的class文件为止,如果最终都没有找到class文件,那么加载失败。
* Loads classes and resources from a repository. One or more class loaders are
* installed at runtime. These are consulted whenever the runtime system needs a
* specific class that is not yet available in-memory. Typically, class loaders
* are grouped into a tree where child class loaders delegate all requests to
* parent class loaders. Only if the parent class loader cannot satisfy the
* request, the child class loader itself tries to handle it.
以上是官方描述
在android中有三种类加载器
DexClassLoader:可以加载apk,jar以及dex文件
PathClassLoader:只可以加载本地文件系统的apk
在类加载器的责任链中,
此三种类加载器的父亲加载器都是BootClassLoader,这个BootClassLoader来自于java.lang包。