hook

根项目build添加:

classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'

app的添加

apply plugin: 'android-aspectjx'

插件:

compileOnly("org.aspectj:aspectjrt:1.9.5")

1、@Aspect:把当前类标识为一个切面供容器读取
2、@Before:在切点方法之前执行
3、@Around:属于环绕增强,能控制切点执行前,执行后
4、@Pointcut:
5、@AfterReturning:切点方法返回后执行
6、@AfterThrowing:切点方法抛异常执行
7、@After:在切点方法之后执行

@Aspect
public class HookByAspect {
    private static final String TAG = "Hook-";

    /**
     * 打印System.loadLibrary调用
     *
     * @param libName
     */
    @Before("call(public void java.lang.System.loadLibrary(java.lang.String))" +
            " && args(libName)")
    public void loadLibrary(String libName) {
        Log.i(TAG, "System.loadLibrary:" + libName + getStackTrace(new Exception()));
    }

    /**
     * 拦截所有Activity onCreate函数
     * 可以做一些业务
     *
     * @param joinPoint
     * @param bundle
     */
    @Before("execution(void android.app.Activity+.onCreate(android.os.Bundle))" +
            "&& !within(androidx.fragment.app.FragmentActivity)" +
            "&& !within(androidx.activity.ComponentActivity)" +
            "&& !within(androidx.core.app.ComponentActivity)" +
            "&& !within(androidx.appcompat.app.AppCompatActivity)" +
            "&& args(bundle)")
    public void onCreate(JoinPoint joinPoint, Bundle bundle) {
        Log.i(TAG, "Activity.onCreate:" + joinPoint.getTarget());
        if (joinPoint.getTarget() != null && joinPoint.getTarget() instanceof Activity) {
            Activity activity = (Activity) joinPoint.getTarget();
            // com.george.mytest.MainActivity
            String className = activity.getClass().getName();
            Log.d(TAG, "onCreate: className:" + className);
            // 做一些业务处理
        }
    }

    /**
     * 拦截startActivity函数;
     * 可以阻断startActivity跳转逻辑
     *
     * @param joinPoint
     */
    @Around("call(void *..startActivity*(..))"
            + "&& !within(android..*)"
            + "&& !within(androidx..*)")
    public void startActivity(ProceedingJoinPoint joinPoint) {
        Log.i(TAG, "startActivity: " + joinPoint.getTarget());
        if (joinPoint.getTarget() != null
                && (joinPoint.getTarget() instanceof Activity || joinPoint.getTarget() instanceof android.app.Fragment || joinPoint.getTarget() instanceof androidx.fragment.app.Fragment)
                && joinPoint.getArgs() != null
                && joinPoint.getArgs().length > 0
                && joinPoint.getArgs()[0] instanceof Intent) {
            Activity activity;
            if (joinPoint.getTarget() instanceof androidx.fragment.app.Fragment) {
                activity = ((androidx.fragment.app.Fragment) joinPoint.getTarget()).getActivity();
            } else if (joinPoint.getTarget() instanceof android.app.Fragment) {
                activity = ((android.app.Fragment) joinPoint.getTarget()).getActivity();
            } else {
                activity = (Activity) joinPoint.getTarget();
            }
            Intent intent = (Intent) joinPoint.getArgs()[0];
            String className = "";
            if (intent.getComponent() != null) {
                className = intent.getComponent().getClassName();
            } else if (intent.getAction() != null) {
                className = intent.getAction();
            }
            // com.george.mytest.MainActivity
            Log.d(TAG, "startActivity: className:" + className);
            // 做一些业务
            // 如果不调用proceed,可以阻断执行跳转
            Log.d(TAG, "startActivity: 可以阻断执行");
//            proceed(joinPoint);
        } else {
            // 继续执行
            Log.d(TAG, "startActivity: 继续执行");
            proceed(joinPoint);
        }
    }

    /**
     * 执行原始逻辑
     *
     * @param joinPoint
     */
    private void proceed(ProceedingJoinPoint joinPoint) {
        try {
            joinPoint.proceed();
        } catch (Throwable throwable) {
            Log.e(TAG, "proceed error " + throwable);
        }
    }

    /**
     * 调用栈信息
     *
     * @param throwable
     * @return
     */
    private String getStackTrace(Throwable throwable) {
        try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
            throwable.printStackTrace(pw);
            String[] strings = sw.toString().split("\n");
            for (String str : strings) {
                if (str.contains("Activity")) {
                    return str;
                } else if (str.contains("Application")) {
                    return str;
                }
            }
            return sw.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

你可能感兴趣的:(hook)