Java Xposed中的findAndHookMethod代码错误。救命!! (NoSuchMethodError)

时间:2020-05-27 16:39:05

标签: java xposed

挂钩目标:ButtonClickCount(int a)

如果下面的代码

public int ButtonClickCount()
{
    return 1;
}

成功,代码如下。

    findAndHookMethod(
            "com.study.MainActivity",
            lpparam.classLoader,
            "ButtonClickCount",

            new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    XposedBridge.log("before");
                }

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    XposedBridge.log("after");
                    param.setResult(10000);
                }
            }

    );
}

但是,如果ButtonClickCount方法接收到参数值,则会引发NoSuchMethodError。

public int ButtonClickCount(int a)
{
    return a++;
}

这是findAndHookMethod中的错误,我不知道要在代码中添加什么内容。

1 个答案:

答案 0 :(得分:1)

我解决了。

示例代码1

public int ButtonClickCount() {
    return 1;
}

    findAndHookMethod(
            "com.study.MainActivity",
            lpparam.classLoader,
            "ButtonClickCount",
            new XC_MethodHook() 

示例代码2

public int ButtonClickCount(int a) {
    return a++;
}

    findAndHookMethod(
            "com.study.MainActivity",
            lpparam.classLoader,
            "ButtonClickCount",
            int.class,
            new XC_MethodHook() 

示例代码3

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView((int) R.layout.activity_main);
    this.btn = (Button) findViewById(R.id.btn);
    this.up = (TextView) findViewById(R.id.up);
    this.btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

    findAndHookMethod(
            "com.study.MainActivity",
            lpparam.classLoader,
            "ButtonClickCount",
            Bundle.class,
            new XC_MethodHook() 

从上面的示例代码中可以看到,如果钩住不带参数值的方法,则只需放置类名,类加载器,方法名和对象。

但是,如果像示例代码2中那样从要挂钩的方法中收到一个称为int的int参数值,则必须在findAndHookMethod中添加int.class。如果不添加它,您将在Xposed应用程序中看到NoSuchMethodError。

如果您尝试像示例代码3中那样钩住onCreate方法,则onCreate方法将使用Bundle类型的参数。因此,您需要在findAndHookMethod中添加Bundle.class。

此DOC帮助我解决了问题。 https://api.xposed.info/reference/de/robv/android/xposed/XposedHelpers.html#findAndHookMethod(java.lang.String,%20java.lang.ClassLoader,%20java.lang.String,%20java.lang.Object...)

如果这不起作用,请检查您的Android Studio版本。就我而言,我将版本从3.6.2降级到版本2.3.2,然后运行它。

我希望我的解决方案可以帮助某人。

相关问题