Android环境搭建整合包,Android Studio 3.3 配置aspectJ : app-module和library-modlue使用注意,消除javaCompile过时警告...

文章目录

总结

在 app 中的配置与 library 中的配置是有点点不一样的。app中用applicationVariants library中用 libraryVariants

在哪个 module 中进行aop 操作,就需要在那个 module 中进行配置。

不同的 module 无法跨 module 进行 aop 操作。就是说 a-module,有个使用了@Aspect 的类要进行 aop 操作,但在 具体切点规则中需要找到其它 module中的类、方法、属性进行 aop 操作,是不行的。

环境: aspectJ 1.9.+ 支持 java9

配置

root/build.gradle

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"

}

}

复制代码

module/build.gradle

在需要 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 。

dependencies

在需要 aop 操作的 module/build.gradle 中,

implementation "org.aspectj:aspectjrt:$versions.aspectj"

复制代码

依赖后,可以使用 AspectJ 中的注解

WARNING: javaCompile 过时

提示要用 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

}

}

}

}

}

复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值