吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3896|回复: 2
收起左侧

[Android 原创] 通过去classLoader避免Android P反射限制后还原classLoader的方法

[复制链接]
破解project 发表于 2019-2-24 10:17
本帖最后由 破解project 于 2019-2-24 10:22 编辑

接上一帖

https://www.52pojie.cn/thread-854771-1-1.html

我之前说没有限制的话是不对的,由于反射之后接下来的操作都是调用同类的方法没出现问题,所以我以为没有这类限制。一旦调用以下涉及外部类的代码时
[Java] 纯文本查看 复制代码
context.startService(new Intent(context, OtherService.class));

就会出现以下错误
[Java] 纯文本查看 复制代码
java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available


所以为了还原classLoader,修复错误,应该...
[Java] 纯文本查看 复制代码
    /**
     * 恢复classloader:清除loader可能会造成问题
     * (java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available)
     * 反射完成后恢复loader
     *
     * @param cls 需要恢复Loader的类
     */
    public static void restoreLoaderInClass(Class cls) {
        try {
            Field classLoaderField = Class.class.getDeclaredField("classLoader");
            classLoaderField.setAccessible(true);
            //If this object represents a primitive type or void, null is returned.
            if (cls != null && !cls.isPrimitive() && classLoaderField.get(cls) == null) {
                Log.w(TAG, "restoreLoaderInClass: classloader is null!");
                classLoaderField.set(cls, Thread.currentThread().getContextClassLoader());
            }
        } catch (Exception e) {
            Log.e(TAG, "restoreLoaderInClass: ", e);
        }
    }


用法
[Java] 纯文本查看 复制代码
//在当前类下
public class Reflection9 {
    private static void method() {
        //传入当前类
        clearClassLoaderInClass(Reflection9.class);
        //接下来就可以调用隐私API了...
        SystemService.restart("zygote");
        //还原,当然接下来仍会受到反射限制
        restoreLoaderInClass(Reflection9.class);
    }
}

免费评分

参与人数 1吾爱币 +6 热心值 +1 收起 理由
qtfreet00 + 6 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Yancy-Lan 发表于 2019-2-26 19:05
本帖最后由 Yancy-Lan 于 2019-2-26 19:06 编辑

小白,看不懂,,,评分不了,但还是辛苦你了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-8 04:07

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表