1.新建一个plugin:

import com.android.build.gradle.AppPluginimport com.android.build.gradle.LibraryPluginimport org.aspectj.bridge.IMessageimport org.aspectj.bridge.MessageHandlerimport org.aspectj.tools.ajc.Mainimport org.gradle.api.Pluginimport org.gradle.api.Projectimport org.gradle.api.tasks.compile.JavaCompileclass AspectJPlugin implements Plugin {    @Override void apply(Project project) {        def hasApp = project.plugins.withType(AppPlugin)        def hasLib = project.plugins.withType(LibraryPlugin)        if (!hasApp && !hasLib) {            throw new IllegalStateException("'android' or 'android-library' plugin required.")        }        final def log = project.logger        final def variants        if (hasApp) {            variants = project.android.applicationVariants        } else {            variants = project.android.libraryVariants        }        project.dependencies {            // TODO this should come transitively            debugCompile 'org.aspectj:aspectjrt:1.8.6'    //app 运行时需要aspectjrt        }        variants.all { variant ->            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.lifecycle "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;                    }                }            }        }    }}

在每个variant的JavaCompile Task加入aspectj的运行代码:

new Main().run(args, handler);

运行aspectJ的参数如下:

String[] args = [                        "-showWeaveInfo",                        "-1.5",                        "-inpath", javaCompile.destinationDir.toString(),  //class的输出目录,作为aspectJ的输入,如 -inpath, D:\work\code\MaterializeYourApp\app\build\intermediates\classes\debug,切面定义文件可以在源文件里定义                        "-aspectpath", javaCompile.classpath.asPath,  //依赖的jar包,切面定义文件可以在包含在第三方依赖中                        "-d", javaCompile.destinationDir.toString(), //输出class的目录                        "-classpath", javaCompile.classpath.asPath,  //依赖的jar包                        "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator) //-bootclasspath, C:\Users\yangwei-os\AppData\Local\Android\Sdk\platforms\android-25\android.jar                ]

2. apply plugin: AspectJPlugin

3. 如果切面文件定义在源文件中,需要在工程的build.gradle中加入

compile 'org.aspectj:aspectjrt:1.8.6'

更多相关文章

  1. 自定义dialog
  2. xml中自定义图形
  3. 自定义线程池管理类
  4. 自定义的水平进度条 layer-list
  5. Android(安卓)--------- 自定义VIew
  6. 解决Android启动显示空白界面的问题,自定义进入软件前的背景图片
  7. Android之自定义最简单的竖向引导页
  8. 自定义React Native Modal,支持全屏弹框
  9. uniapp自定义弹窗组件|Modal模态框|Loading加载框

随机推荐

  1. 回到基础:理解 JavaScript DOM[每日前端夜
  2. Webpack5.0 新特性尝鲜实战 [每日前端夜
  3. 怎样使用React Context API [每日前端夜
  4. 7个开放式的 HTML 面试题及回答策略 [每
  5. 尝鲜 ES2019 的新功能 [每日前端夜话0x38
  6. 表弟的数学题,我竟然用了python才解出来
  7. 7 个开放式 CSS 面试题及回答策略 [每日
  8. 在北京看场雪为什么这么难?
  9. 用python对2019年二手房价格进行数据分析
  10. 基于 Babel 的 npm 包的最小化设置 [每日