在工程里添加一个名为“liba_aspectj”的Module,包含AspectJ相关功能代码, 如图28-1所示:图28-1AOPAspect.java里是AspectJ功能代码,AOPTimer.java里是计时功能代码。此Module的build.gradle文件内容如下:
如图28-1所示:图28-1AOPAspect.java里是AspectJ功能代码,AOPTimer.java里是计时功能代码。此Module的build.gradle文件内容如下:
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 {         jcenter()    }     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 23     buildToolsVersion '23. 0. 2'     lintOptions {
  abortOnError false    } } android. libraryVariants. all { variant ->     LibraryPlugin plugin = project. plugins. getPlugin( LibraryPlugin)     JavaCompile javaCompile = variant. javaCompile     javaCompile. doLast {         String[] args = ["-showWeaveInfo",                         "-1. 8",// 当前 使用 的 JDK 版本                         "-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;            }        }    } }
如要在application模块的代码里使用AspectJ库,需要在build.gradle文件里添加如下代码:
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' dependencies {     compile project(': liba_ aspectj') } … 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;            }        }    } }
如要在library模块的代码里使用AspectJ库,需要在build.gradle文件里添加如下代码:
import com. android. build. gradle. LibraryPlugin 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. library' … dependencies {         compile project(': liba_ aspectj') } android. libraryVariants. all { variant ->     LibraryPlugin plugin = project. plugins. getPlugin( LibraryPlugin)     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", 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;            }        }    } }