xposed代码示例

package com.example.xposedhook;

import android.util.Log;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class HookStatic implements IXposedHookLoadPackage {

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        if(loadPackageParam.packageName.equals("com.cxa.xposeddemo"));
        Class<?> clazz=XposedHelpers.findClass("com.cxa.xposeddemo.Demo",loadPackageParam.classLoader);
        XposedHelpers.setStaticIntField(clazz,"字段名",100);
        XposedHelpers.setStaticObjectField(clazz,"字段名","字段值");
        //Hook构造函数  XC_MethodHook是抽象类需要实现其方法
        XposedHelpers.findAndHookConstructor(clazz,new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                Log.d("cxa","无参构造函数");
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                Log.d("cxa","无参构造函数后");
            }
        });

        //Hook构造函数  XC_MethodHook是抽象类需要实现其方法
        // clazz后面可以跟参数:格式String.class或者int.class传字节码:[类型.class]
        XposedHelpers.findAndHookConstructor(clazz,String.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                param.args[0] ="cxa修改后"; //修改第一个参数
                //Log.d("cxa","无参构造函数");
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                Log.d("cxa","有参构造函数后");
            }
        });

        //hook函数
        XposedHelpers.findAndHookMethod(clazz, "", String.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                Log.d("cxa","函数before");
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                Log.d("cxa","函数after");
            }
        });


    }
}

上一篇:2020.06.29 ssm项目整合练习中LogAop日志类


下一篇:初探java安全之反射