在Eclipse中已经有AJDT插件集成了AspectJ编译器的使用和关键字的声明。但是在Android Studio中没有这样的官方插件。因此,这里讲一下如何在Android Studio中使用AspectJ。详细了解AspectJ的使用请看Android基于AOP的非侵入式监控
AspectJ的使用核心就是它的编译器,它就做了一件事,将AspectJ的代码在编译期插入目标程序当中,运行时跟在其它地方没什么两样,因此要使用它最关键的就是使用它的编译器去编译代码ajc。ajc会构建目标程序与AspectJ代码的联系,在编译期将AspectJ代码插入被切出的PointCut中,已达到AOP的目的。
因此,无论在什么IDE上(如果使用命令行就可以直接使用ajc编译了),问题就是让IDE使用ajc作为编译器编译代码。
1、插件:网上有人在github上提供了集成的插件gradle-android-aspectj-plugin,一开始我也是用的这个,但是在项目当中,无法兼容databinding,这个问题现在作者依然没有解决。
2、Gradle配置:国外还有一个大牛在build文件中添加了一些脚本,也能够在AS中使用,但是脚本定义的任务有点麻烦(Gradle基础不好的看不懂,我就是)。不过正是这位大牛解决了我项目上遇到的问题。文章地址:Aspect Oriented Programming in Android
下面就介绍一下第二种方法的具体的使用步骤。
compile 'org.aspectj:aspectjrt:1.8.9'
这里,主工程和Module都需要对构建脚本添加一些任务,目的就是为了建立两者的通信,使得IDE使用ajc编译代码。
build.gradle(app):
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.aspectj:aspectjtools:1.8.9'
}
}
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
dependencies {
compile project(':aspectjlibrary')
}
android {
compileSdkVersion 21
buildToolsVersion '22.0.1'
buildTypes {
debug {
minifyEnabled false // should disable proguard in debug builds
}
}
defaultConfig {
applicationId "com.example.lingyimly.try2"
minSdkVersion 15
targetSdkVersion 21
}
lintOptions {
abortOnError true
}
}
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.5",
"-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;
}
}
}
}
build.gradle(module):
import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
apply plugin: 'com.android.library'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'org.aspectj:aspectjtools:1.8.9'
classpath 'org.aspectj:aspectjweaver:1.8.9'
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.aspectj:aspectjrt:1.8.9'
compile 'com.android.support:appcompat-v7:22.2.1'
}
android {
compileSdkVersion 22
buildToolsVersion '23.0.1'
lintOptions {
abortOnError false
}
}
android.libraryVariants.all { variant ->
LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin)
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.5",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", plugin.project.android.bootClasspath.join(
File.pathSeparator)]
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler)
def log = project.logger
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:
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}
package com.example.lingyimly.aspectj;
import android.util.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/** * Created by lingyi.mly on 2016/5/27. */
@Aspect
public class TraceAspect {
private String TAG = "TAG ";
private static final String POINT_METHOD = "execution(* com.example.lingyimly.try3.MainActivity.*(..))";
private static final String POINT_CALLMETHOD = "call(* com.example.lingyimly.try3.MainActivity.*(..))";
@Pointcut(POINT_METHOD)
public void methodAnnotated(){}
@Pointcut(POINT_CALLMETHOD)
public void methodCallAnnotated(){}
@Around("methodAnnotated()")
public Object aronudWeaverPoint(ProceedingJoinPoint joinPoint) throws Throwable {
joinPoint.proceed();
String result = "----------------------------->aroundWeaverPoint";
Log.e(TAG,"----------------------------->aroundWeaverPoint");
return result;//替换原方法的返回值
}
@Before("methodCallAnnotated()")
public void beforecall(JoinPoint joinPoint){
Log.e(TAG,"beforecall");
}
}
05-27 21:01:30.618 25192-25192/? E/TAG: beforecall
05-27 21:01:30.678 25192-25192/? E/TAG: beforecall
05-27 21:01:30.678 25192-25192/? E/TAG: ----------------------------->aroundWeaverPoint
05-27 21:01:30.678 25192-25192/? E/TAG: MainActivity ----------------------------->aroundWeaverPoint
05-27 21:01:30.678 25192-25192/? E/TAG: ----------------------------->aroundWeaverPoint
05-27 21:39:20.108 27390-27390/? E/TAG: MainActivity From MainActivity
AndroidAOP