运行时 android 上的字节码操作

Bytecode manipulation on android at runtime

我想知道为什么无法在运行时对 android 进行字节码操作?是因为目前没有支持的库还是因为DEX格式是不可操作的

在运行时,加载 class 后,无法修改 class 的定义。对于任何类 JVM 系统都是如此,包括 Android 的 Dalvik。但是,您可以修改 classes after compilation but before inclusion into APK,我已经为 Android 应用工具。构建 APK 后,您不能进行任何更改,因为 APK 已签名以验证存档的内容。

不要与 Objective-C 中称为方法调配的类似技术混淆,它确实允许您在运行时有效地更改对象定义。 Objective-C 运行时是可变的,JVM 运行时不是。

如果您有一个外部 dex 想要在加载到 Android 应用程序之前对其进行修改,您也可以这样做,但是同样,一旦 classes 被加载一个ClassLoader,它们不能被修改。

嗯,有DexClassLoader,至少可以从apk外部获取类。如何在 .jar 中创建 classes.dex 是另一回事。引用文档:

A class loader that loads classes from .jar and .apk files containing a classes.dex entry. This can be used to execute code not installed as part of an application.

This class loader requires an application-private, writable directory to cache optimized classes. Use Context.getCodeCacheDir() to create such a directory