使用Xposed 风格的api hook自身app的类无效
Closed this issue · 2 comments
KuNgia09 commented
测试环境:
小米10 Android 11
pine使用的最新版本
implementation 'top.canyie.pine:core:0.2.9'
implementation 'top.canyie.pine:xposed:0.1.0'
使用Pine 风格api hook sum函数可以hook
private void hookSumByPineStyle(ClassLoader loader){
Class mainActivity = XposedHelpers.findClass("org.fengyue.targetapk.MainActivity", loader);
try {
if (mainActivity != null) {
Method mid = mainActivity.getDeclaredMethod("sum", int.class, int.class);
if (mid != null) {
MethodHook.Unhook unhook = Pine.hook(mid, new MethodHook() {
@Override
public void beforeCall(Pine.CallFrame callFrame) throws Throwable {
super.beforeCall(callFrame);
int a = (int) callFrame.args[0];
int b = (int) callFrame.args[1];
Log.d(TAG, "hook MainActivity sum method a:" + a + ";b:" + b);
}
@Override
public void afterCall(Pine.CallFrame callFrame) throws Throwable {
super.afterCall(callFrame);
callFrame.setResult(1000);
}
});
Log.d(TAG, "hook MainActivity success");
}
}
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
使用xposed风格 hook不生效
private void hookSumByXposedStyle(ClassLoader loader) {
Class mainActivity=XposedHelpers.findClass("org.fengyue.targetapk.MainActivity",loader);
XC_MethodHook.Unhook unhook = XposedHelpers.findAndHookMethod(mainActivity, "sum", int.class, int.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Log.d(TAG, "get sum original result:" + (String) param.getResult());
param.setResult(20000);
}
});
if (unhook != null) {
Log.d(TAG, "hook MainActiivty sum method success");
} else {
Log.e(TAG, "hook MainActiivty sum method failed");
}
}
canyie commented
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Log.d(TAG, "get sum original result:" + (String) param.getResult());
param.setResult(20000);
}
你的打 log 的那行代码把原方法返回的结果强制转换成了 String,从你的代码来看该方法返回的结果应该是个整数,这里会触发一个 ClassCastException 导致执行被中断,所以底下的 setResult 没有实际调用到。
这个问题只要看一眼 log 就能看见的……
KuNgia09 commented
Thanks