AOP-Aspectjx

AOP-Aspectjx

通过预编译和运行期动态代理实现程序功能统一维护的一种技术
具有无侵入性和修改方便的特点

https://github.com/HujiangTechnology/gradle_plugin_android_aspectjx

注意:高版本gradle可能无法编译通过

目前编译通过的版本:com.android.tools.build:gradle:3.5.2

注意:有存在兼容性问题的风险,谨慎带到线上环境

使用

类名上声明 @Aspect ,使该类具有切面功能

1
2
3
4
5
6
7
@Aspect
public class PerformanceApp {

@Around("call(* com.benben.starttest.MainActivity.**(..))")
public void getTime(ProceedingJoinPoint joinPoint) {
}
}

语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Before("execution(* android.app.Activity.on**(..))")
public void method1(JoinPoint joinPoint) {
// ...
}

@After("call(* android.app.Activity.on**(..))")
public void method2(JoinPoint joinPoint) {
// ...
}

@Around("execution(* android.app.Activity.on**(..))")
public void method3(ProceedingJoinPoint joinPoint) {
// ...
}

第一个*代表匹配任意的方法返回值,后面的语法代码匹配所有Activity中on开头的方法,两个 .. 代表任意参数

  • 插入类型
    • call:出入在函数体里面
    • execution:插入在函数体外面
  • 执行点
    • @Before:切入点前面执行
    • @After:切入点后面执行
    • @Around:切入点之前、之后
  • 插入点
    • ActionBeforeAfter 时,方法参数为 JoinPoint
    • ActionAround 时,方法参数为 ProceedingJoinPoint
注意

ART-Hook 的区别:ART 可以 Hook 系统方法,AspectJ 只能 Hook 非系统方法