自定义类加载/覆盖Android本机类

use*_*195 8 java reflection android classloader dynamic-class-loaders

主要目标是使用我自己的实现覆盖Android系统类(Activity,View等).

http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html

实现了自定义类加载的ClassLoader,加载非系统类(自定义类).

但是当我尝试使用我的实现加载Activity时 - 它没有加载,因为ClassLoader已经在其缓存中有这个类:

/**
 * Returns the class with the specified name if it has already been loaded
 * by the virtual machine or {@code null} if it has not yet been loaded.
 *
 * @param className
 *            the name of the class to look for.
 * @return the {@code Class} object or {@code null} if the requested class
 *         has not been loaded.
 */
protected final Class<?> findLoadedClass(String className) {
    ClassLoader loader;
    if (this == BootClassLoader.getInstance())
        loader = null;
    else
        loader = this;
    return VMClassLoader.findLoadedClass(loader, className);
}
Run Code Online (Sandbox Code Playgroud)

如何更改类加载器以注入我自己的类而不是系统?

小智 0

类一旦被RootClassLoader加载,就不能再次加载,除非先卸载。然而,卸载类是一个由 DVM 自动管理的过程。我也被同样的问题所困扰。