深入理解Android中的ClassLoader

本文详细比较了Android中的ClassLoader与JVM的区别,解释了Android为何禁止使用defineClass方法加载本地class,并介绍了DexClassLoader和PathClassLoader在加载class文件中的作用。此外,文章还讨论了反射和类加载在Android中的实际应用,如通过VirtualApk框架间接访问未公开API。
摘要由CSDN通过智能技术生成

相比其他讲Android ClassLoader的文章,这篇有什么优点?

相比其他多数东拼西凑的文章,这篇文章能从头到脚帮你梳理一下Android中ClassLoader的知识点,以及代码层面帮你实战 加深理解。

JVM和android的虚拟机在ClassLoader上有何不同?

还记得在我之前的JVM那篇文章的结尾,我自定义了一个ClassLoader,通过读取了一个本地class文件,然后传递了 一个byte数组 到defineClass这个方法里从而成功的加载了一个本地class,但是在android中,这种方法是不被允许的。 我们来看看源码:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

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 {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

架构师筑基包括哪些内容

我花了将近半个月时间将:深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!



这份资料就包含了所有Android初级架构师所需的所有知识!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

支,都有对应的目录内容与知识点!**

[外链图片转存中…(img-6F1dXyDE-1712469020626)]
[外链图片转存中…(img-Ec1lY4yD-1712469020626)]
这份资料就包含了所有Android初级架构师所需的所有知识!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值