自定义类加载/覆盖 Android 原生类
主要目标是用我自己的实现覆盖 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);
}
如何更改类加载器以注入我自己的类而不是系统?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从博客文章中找到了此解决方案。我知道发布链接是相当违反堆栈溢出政策的,但文本太大而无法传输。
这个想法是编写一些覆盖低级类加载机制的 C 代码,从而覆盖方法的执行方式。我希望这对某人有帮助。
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.
类一旦被RootClassLoader加载,就不能再次加载,除非先卸载。然而,卸载类是一个由 DVM 自动管理的过程。我也被同样的问题所困扰。
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.