如何使用
在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
下面就介紹一下第二種方法的具體的使用步驟。
Step
1、創建AS原工程
2、創建module(Android Library),然後添加AspectJ依賴,必須添加至module中,添加至APP工程中ajc編譯器是不會重構目標代碼的。
compile 'org.aspectj:aspectjrt:1.8.9'
3、編寫build腳本,添加任務,使得IDE使用ajc作爲編譯器編譯代碼。
這裏,主工程和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;
}
}
}
}
4、編寫AspectJ切面程序代碼
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");
}
}
5 運行結果
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