android的classLoader可以看出来是有defineClass这个方法的,但是这些方法都统一返回了异常,也就是说 android官方不允许我们用JVM那套方法来加载class,为了更清晰,我复制一段源码出来。
protected final Class<?> defineClass(String name, byte[] b, int off, int len,
ProtectionDomain protectionDomain)
throws ClassFormatError
{
throw new UnsupportedOperationException(“can’t load this type of class file”);
}
很直观,能看出来这里是什么意思,再接着往下看
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { // First, check if the class has already been loaded Class<?> c = findLoadedClass(name);
if (c == null) {
try {
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
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
c = findClass(name);
}
}
return c;
}
这里看一下,一样很直观,可以看出来这里的loadClass和JVM中的loadClass的流程一模一样。也是符合双亲委托机制。
既然Android中不能用defineClass的方法读取一个class文件的byte,那么android如何加载class文件的?
有一定基础的人应该知道,android中的类加载器有两种,DexClassLoader和PathClassLoader。我们下面就来看看这2个 ClassLoader是如何加载class文件的吧。
package dalvik.system;
import dalvik.system.BaseDexClassLoader;
import java.io.File;
public class DexClassLoader extends BaseDexClassLoader {
public DexClassLoader(String dexPath, String optimizedDirectory, String librarySearchPath, ClassLoader parent) {
super((String)null, (File)null, (String)null, (ClassLoader)null);
throw new RuntimeException(“Stub!”);
}
}
public class PathClassLoader extends BaseDexClassLoader {
public PathClassLoader(String dexPath, ClassLoader parent) {
super((String)null, (File)null, (String)null, (ClassLoader)null);
throw new RuntimeException(“Stub!”);
}
public PathClassLoader(String dexPath, String librarySearchPath, ClassLoader parent) {
super((String)null, (File)null, (String)null, (ClassLoader)null);
throw new RuntimeException(“Stub!”);
}
}
嗯 能看出来 这2个都是派生自BaseDexClassLoader类,
public class BaseDexClassLoader extends ClassLoader {
public BaseDexClassLoader(String dexPath, File optimizedDirectory, String librarySearchPath, ClassLoader parent) {
throw new RuntimeException(“Stub!”);
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
throw new RuntimeException(“Stub!”);
}
protected URL findResource(String name) {
throw new RuntimeException(“Stub!”);
}
protected Enumeration findResources(String name) {
throw new RuntimeException(“Stub!”);
}
public String findLibrary(String name) {
throw new RuntimeException(“Stub!”);
}
protected synchronized Package getPackage(String name) {
throw new RuntimeException(“Stub!”);
}
public String toString() {
throw new RuntimeException(“Stub!”);
}
}
看到这可能很多人就懵逼了,这是啥意思,实际上这里代表的就是这个类执行的时候,会让ROM里的类来实际执行,这里android studio定位到的源码 只是告诉我们,嗯 这里有一个这样的类。但是实际的实现是放在Rom中做的。 很多人到这里可能还是无法理解是什么意思,其实这个地方在很多开源项目中都有实际用法,比如你要反射调用api里没有暴露出来给你的类怎么办呢?这个类在rom里是有的 但是api并没有暴露出来给你。就可以用这种写法了。我举个滴滴开源框架VirtualApk的例子:
看到没有滴滴也是这么做的,将这些类放在自己的框架内暴露出来(注意包名要和ROM中的一样)这样就可以在其他地方反射调用了。 我们随便找个源码进去看一看:
package android.app;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
/**
- @author johnsonlee
*/
public final class ActivityThread {
public static ActivityThread currentActivityThread() {
throw new RuntimeException(“Stub!”);
}
public static boolean isSystem() {
throw new RuntimeException(“Stub!”);
}
public static String currentOpPackageName() {
throw new RuntimeException(“Stub!”);
}
public static String currentPackageName() {
throw new RuntimeException(“Stub!”);
}
public static String currentProcessName() {
throw new RuntimeException(“Stub!”);
}
public static Application currentApplication() {
throw new RuntimeException(“Stub!”);
}
public ApplicationThread getApplicationThread() {
throw new RuntimeException(“Stub!”);
}
public Instrumentation getInstrumentation() {
throw new RuntimeException(“Stub!”);
}
public Looper getLooper() {
throw new RuntimeException(“Stub!”);
}
public Application getApplication() {
throw new RuntimeException(“Stub!”);
}
public String getProcessName() {
throw new RuntimeException(“Stub!”);
}
public final ActivityInfo resolveActivityInfo(final Intent intent) {
throw new RuntimeException(“Stub!”);
}
public final Activity getActivity(final IBinder token) {
throw new RuntimeException(“Stub!”);
}
final Handler getHandler() {
throw new RuntimeException(“Stub!”);
}
private class ApplicationThread extends ApplicationThreadNative {
}
}
你看ActivityThread这个类,有过源码基础的同学应该都知道这个类是rom里我们api里没有这个类的。用这种方法暴露出来以后就可以使用它了。
扯远了,回到正题,既然api 源码里看不到真正的实现 我们只好去rom代码里看看了 这里推荐一个在线查看源码的网站
这里明显看出来我们的DexClassLoader和pathClassLoader构造函数传参上面就有不同,DexClassLoader需要提供一个额外的path路径,这个path路径我们看下注释很容易理解:用来存放解压缩以后的dex文件。(apk和jar包解压缩都可以 只要解压缩以后的是dex文件),这个path就是解压缩的目标路径。但是PathClassLoader就没有这个构造函数。只能直接操作dex文件。 总结: DexClassLoader可以加载任何路径的apk/dex/jar PathClassLoader只能加载/data/app中的apk,也就是已经安装到手机中的apk。这个也是PathClassLoader作为默认的类加载器的原因,因为一般程序都是安装了,在打开,这时候PathClassLoader就去加载指定的apk(解压成dex,然后在优化成odex)就可以了
继续跟源码 跟到BaseClassLoader
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

写在最后
对程序员来说,很多技术的学习都是“防御性”的。也就是说,我们是在为未来学习。我们学习新技术的目的,或是为了在新项目中应用,或仅仅是为了将来的面试。但不管怎样,一定不能“止步不前”,不能荒废掉。
![
文章以下内容会给出阿里与美团的面试题(答案+解析)、面试题库、Java核心知识点梳理等
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
片转存中…(img-FLr125ot-1713679363116)]
[外链图片转存中…(img-umRxmHSi-1713679363117)]
文章以下内容会给出阿里与美团的面试题(答案+解析)、面试题库、Java核心知识点梳理等
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!