自定义类加载/覆盖 Android 原生类

发布于 2024-12-19 21:15:35 字数 1019 浏览 2 评论 0原文

主要目标是用我自己的实现覆盖 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-12-26 21:15:35

我从博客文章中找到了此解决方案。我知道发布链接是相当违反堆栈溢出政策的,但文本太大而无法传输。

这个想法是编写一些覆盖低级类加载机制的 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.

动听の歌 2024-12-26 21:15:35

类一旦被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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文