applicationVariants
library中用 libraryVariants
环境:
aspectJ 1.9.+ 支持 java9
buildscript {
...
def versions = [:]
ext.versions = versions
versions.aspectj = '1.9.3.RC1'
dependencies {
classpath "org.aspectj:aspectjrt:$versions.aspectj"
classpath "org.aspectj:aspectjrt:$versions.aspectj"
}
}
在需要 aop 操作的 module/build.gradle 中
比如在 app-module 中
apply plugin: 'com.android.application'
...
android { ... }
//aspectj start
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()) {//非 debug 的 buildtype,打印 log退出;必要时可以不用这段判断
log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
return
}
JavaCompile javaCompile = variant.javaCompile//警告:studio 3.3过时了
// def javaCompile = variant.getJavaCompileProvider()//还会引起其它异常
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.9",
"-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
}
}
}
}
//aspectj end
dependencies { ... }
若是在 library-module 中,只需要将 applicationVariants
换成 libraryVariants
。
在需要 aop 操作的 module/build.gradle 中,
implementation "org.aspectj:aspectjrt:$versions.aspectj"
依赖后,可以使用 AspectJ 中的注解
提示要用 javaCompileProvider; 用了之后还是报错的,需要用 javaCompileProvider.configure;其它之前使用javaCompile字符串的地方,都改用 it 就可以了。最终如下,来个 libraryVariants 的:
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.libraryVariants
variants.all { variant ->
if (!variant.buildType.isDebuggable()) {//非 debug 的 buildtype,打印 log退出;必要时可以不用这段判断
log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
return
}
variant.javaCompileProvider.configure {
it.doLast {
String[] args = ["-showWeaveInfo",
"-1.9",
"-inpath", it.destinationDir.toString(),
"-aspectpath", it.classpath.asPath,
"-d", it.destinationDir.toString(),
"-classpath", it.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
}
}
}
}
}