Custom class loading/overriding Android-native classes

放肆的年华 提交于 2020-01-01 09:05:16

问题


Main goal is to override Android system class (Activity, View etc) with my own implementation.

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

ClassLoader for custom class loading is implemented, loading non-system class (custom class) works.

But when I try to load Activity with my implementation - it doesn't load, because ClassLoader already has this class in its cache:

/**
 * 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);
}

How can I change class loader to inject my own class instead of system?


回答1:


I have found this solution from a blog post. I know it is rather against stack overflow policies to post a link but the text is too big to be transfered.

The idea is to write some C code that overrides the low-level class loading mechanism and thus override the way a method is executed. I hope this might be some help to someone.




回答2:


Once a class is loaded by RootClassLoader, it can not be loaded again unless it is unloaded first. However, unloading a class is a process that is automatically managed by the DVM. I'm also troubled by the same problem.



来源:https://stackoverflow.com/questions/8397780/custom-class-loading-overriding-android-native-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!