美文网首页
Xposed插件获取控件id和所在的activity名

Xposed插件获取控件id和所在的activity名

作者: small瓜瓜 | 来源:发表于2020-05-16 10:58 被阅读0次

    有时候反编译一个软件,会发现软件被混淆了,想要找到指定的控件还是挺麻烦的。这时候我们就可以直接通过Xposed插件找到了,废话不多说,看下面的方法。

    首先要知道如何编写一个Xposed插件,如果不清楚可以看这里

    首先先编写一个类实现View.OnClickListener接口,这个用于替换原始的监听类

    import android.content.Context;
    import android.util.Log;
    import android.view.View;
    
    import de.robv.android.xposed.XposedBridge;
    
    public class HookOnClickListener implements View.OnClickListener {
        private View.OnClickListener original;
    
        public HookOnClickListener(View.OnClickListener original) {
            this.original = original;
        }
    
        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            int id = v.getId();
            XposedBridge.log("context:" + context.getClass().getName());
            XposedBridge.log("View Id:" + id);
            original.onClick(v);
        }
    }
    

    通过拦截ViewsetOnClickListener,然后用我们编写HookOnClickListener类替换原始的类。

    import android.view.View;
    
    import de.robv.android.xposed.IXposedHookLoadPackage;
    import de.robv.android.xposed.XC_MethodHook;
    import de.robv.android.xposed.XposedBridge;
    import de.robv.android.xposed.XposedHelpers;
    import de.robv.android.xposed.callbacks.XC_LoadPackage;
    
    public class HookLogic implements IXposedHookLoadPackage {
    
        @Override
        public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
    
    //        String testClassName = "com.tencent.mobileqq";
    //
    //        if (!testClassName.startsWith(lpparam.packageName)) {
    //            return;
    //        }
    
            XposedBridge.log("HookLogic >> 进来了");
    
            XposedHelpers.findAndHookMethod(View.class, "setOnClickListener", View.OnClickListener.class, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    HookOnClickListener hookOnClickListener =
                            new HookOnClickListener((View.OnClickListener) param.args[0]);
                    param.args[0] = hookOnClickListener;
                }
            });
        }
    
    }
    

    上面的类我们也可以这样编写:

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import de.robv.android.xposed.IXposedHookLoadPackage;
    import de.robv.android.xposed.XC_MethodHook;
    import de.robv.android.xposed.XposedBridge;
    import de.robv.android.xposed.XposedHelpers;
    import de.robv.android.xposed.callbacks.XC_LoadPackage;
    
    public class HookLogic implements IXposedHookLoadPackage {
    
        @Override
        public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
    
    
            /**
             * 获取activity
             */
            XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    XposedBridge.log("=\t=\t=\t=\t=\nthis activity:" + param.thisObject);
                    XposedBridge.log("this activity class:" + param.thisObject.getClass().getName());
                }
            });
    
            /**
             * 获取布局id
             */
    
            if (null != XposedHelpers.findClassIfExists("androidx.appcompat.app.AppCompatActivity", lpparam.classLoader)) {
                XposedHelpers.findAndHookMethod(AppCompatActivity.class, "setContentView", int.class, new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("this activity layout id:" + param.args[0]);
                    }
                });
            } else {
                XposedHelpers.findAndHookMethod(Activity.class, "setContentView", int.class, new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("this activity layout id:" + param.args[0]);
                    }
                });
            }
    
            /**
             * 获取view的id
             */
            XposedHelpers.findAndHookMethod(View.class, "setOnClickListener", View.OnClickListener.class, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    HookOnClickListener hookOnClickListener =
                            new HookOnClickListener((View.OnClickListener) param.args[0]);
                    param.args[0] = hookOnClickListener;
                }
            });
    
        }
    
    }
    

    这样就可以了,让我们看下效果:


    相关文章

      网友评论

          本文标题:Xposed插件获取控件id和所在的activity名

          本文链接:https://www.haomeiwen.com/subject/swrlohtx.html