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

时间:2011-12-06 09:37:51

标签: java android reflection 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);
}

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

2 个答案:

答案 0 :(得分:3)

我在博文中找到了this solution。我知道发布一个链接反对堆栈溢出策略,但文本太大而无法传输。

我们的想法是编写一些覆盖低级类加载机制的C代码,从而覆盖方法的执行方式。我希望这可能对某人有所帮助。

答案 1 :(得分:0)

一旦类被RootClassLoader加载,除非先卸载它,否则不能再次加载它。但是,卸载类是由DVM自动管理的过程。我也对同样的问题感到困扰。