在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
在Eclipse中已经有AJDT插件集成了AspectJ编译器的使用和关键字的声明。但是在Android Studio中没有这样的官方插件。
AspectJ的使用核心就是它的编译器,它就做了一件事,将AspectJ的代码在编译期插入目标程序当中,运行时跟在其它地方没什么两样,因此要使用它最关键的就是使用它的编译器去编译代码ajc。ajc会构建目标程序与AspectJ代码的联系,在编译期将AspectJ代码插入被切出的PointCut中,已达到AOP的目的。
因此,无论在什么IDE上(如果使用命令行就可以直接使用ajc编译了),问题就是让IDE使用ajc作为编译器编译代码。
创建module(Android Library),然后添加AspectJ依赖,必须添加至module中,添加至APP工程中ajc编译器是不会重构目标代码的。
使用方法:
编写build脚本,添加任务,使得IDE使用ajc作为编译器编译代码。
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// 版本界限:As-3.0.1 + gradle4.4-all (需要配置r17的NDK环境)
// 或者:As-3.2.1 + gradle4.6-all (正常使用,无警告)
classpath 'org.aspectj:aspectjtools:1.8.9'
classpath 'org.aspectj:aspectjweaver:1.8.9'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
创建module(Android Library),然后添加AspectJ依赖,必须添加至module中,添加至APP工程中ajc编译器是不会重构目标代码的。
// 版本界限:As-3.0.1 + gradle4.4-all (需要配置r17的NDK环境)
// 或者:As-3.2.1 + gradle4.6-all (正常使用,无警告)
buildscript { // 编译时用Aspect专门的编译器,不再使用传统的javac
repositories {
mavenCentral()
}
dependencies {
classpath 'org.aspectj:aspectjtools:1.8.9'
classpath 'org.aspectj:aspectjweaver:1.8.9'
}
}
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.demo.login"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.+'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'org.aspectj:aspectjrt:1.8.13'
}
// 版本界限:As-3.0.1 + gradle4.4-all (需要配置r17的NDK环境)
// 或者:As-3.2.1 + gradle4.6-all (正常使用,无警告)
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
final def log = project.logger
final def variants = project.android.applicationVariants
variants.all { variant ->
if (!variant.buildType.isDebuggable()) {
log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
return;
}
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.8",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)]
log.debug "ajc args: " + Arrays.toString(args)
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler);
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break;
case IMessage.WARNING:
log.warn message.message, message.thrown
break;
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}
4、编写AspectJ切面程序代码
public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 登录点击事件(用户行为统计)
@ClickBehavior("登录")
public void login(View view) {
Log.e(TAG, "模拟接口请求……验证通过,登录成功!");
}
// 用户行为统计(友盟统计?!后台要求自己统计)
@ClickBehavior("我的专区")
@LoginCheck
public void area(View view) {
Log.e(TAG, "开始跳转到 -> 我的专区 Activity");
startActivity(new Intent(this, OtherActivity.class));
}
// 用户行为统计
@ClickBehavior("我的优惠券")
@LoginCheck
public void coupon(View view) {
Log.e(TAG, "开始跳转到 -> 我的优惠券 Activity");
startActivity(new Intent(this, OtherActivity.class));
}
// 用户行为统计
@ClickBehavior("我的积分")
@LoginCheck
public void score(View view) {
Log.e(TAG, "开始跳转到 -> 我的积分 Activity");
startActivity(new Intent(this, OtherActivity.class));
}
}
```使用注解 添加 用户点击痕迹(行为统计) IoC容器
```java
@Target(ElementType.METHOD) // 目标作用在方法之上
@Retention(RetentionPolicy.RUNTIME)
public @interface ClickBehavior {
String value();
}
用户登录检测
```java
// 用户登录检测
@Target(ElementType.METHOD) // 目标作用在方法之上
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginCheck {
}
定义切面类
@Aspect // 定义切面类
public class ClickBehaviorAspect {
private final static String TAG = "netease >>> ";
// 1、应用中用到了哪些注解,放到当前的切入点进行处理(找到需要处理的切入点)
// execution,以方法执行时作为切点,触发Aspect类
// * *(..)) 可以处理ClickBehavior这个类所有的方法
@Pointcut("execution(@com.demo.login.annotation.ClickBehavior * *(..))")
public void methodPointCut() {}
// 2、对切入点如何处理
@Around("methodPointCut()")
public Object jointPotin(ProceedingJoinPoint joinPoint) throws Throwable {
// 获取签名方法
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
// 获取方法所属的类名
String className = methodSignature.getDeclaringType().getSimpleName();
// 获取方法名
String methodName = methodSignature.getName();
// 获取方法的注解值(需要统计的用户行为)
String funName = methodSignature.getMethod().getAnnotation(ClickBehavior.class).value();
// 统计方法的执行时间、统计用户点击某功能行为。(存储到本地,每过x天上传到服务器)
long begin = System.currentTimeMillis();
Log.e(TAG, "ClickBehavior Method Start >>> ");
Object result = joinPoint.proceed(); // MainActivity中切面的方法
long duration = System.currentTimeMillis() - begin;
Log.e(TAG, "ClickBehavior Method End >>> ");
Log.e(TAG, String.format("统计了:%s功能,在%s类的%s方法,用时%d ms",
funName, className, methodName, duration));
return result;
}
}
@Aspect // 定义切面类
public class LoginCheckAspect {
private final static String TAG = "LoginCheckAspect ";
// 1、应用中用到了哪些注解,放到当前的切入点进行处理(找到需要处理的切入点)
// execution,以方法执行时作为切点,触发Aspect类
// * *(..)) 可以处理ClickBehavior这个类所有的方法
@Pointcut("execution(@com.demo.login.annotation.LoginCheck * *(..))")
public void methodPointCut() {}
// 2、对切入点如何处理
@Around("methodPointCut()")
public Object jointPotin(ProceedingJoinPoint joinPoint) throws Throwable {
Context context = (Context) joinPoint.getThis();
if (true) { // 从SharedPreferences中读取
Log.e(TAG, "检测到已登录!");
return joinPoint.proceed();
} else {
Log.e(TAG, "检测到未登录!");
Toast.makeText(context, "请先登录!", Toast.LENGTH_SHORT).show();
context.startActivity(new Intent(context, LoginActivity.class));
return null; // 不再执行方法(切入点)
}
}
}
运行效果
``统计了:登录功能,在MainActivity类的login方法,用时0 ms
ClickBehavior Method Start >>>
检测到已登录!
ClickBehavior Method End >>>
统计了:我的专区功能,在MainActivity类的area方法,用时20 ms
d: handleWindowVisibility: no activity for token android.os.BinderProxy@45be194
ClickBehavior Method Start >>>
ClickBehavior Method End >>>
统计了:我的优惠券功能,在MainActivity类的coupon方法,用时13 ms
d: handleWindowVisibility: no activity for token android.os.BinderProxy@98962e
ClickBehavior Method Start >>>
检测到已登录!
ClickBehavior Method End >>>
统计了:我的积分功能,在MainActivity类的score方法,用时15 ms
d: handleWindowVisibility: no activity for token android.os.BinderProxy@aa09db`java
`
点击下载源码