Android Aspectj 接入 - 非组件

Android Aspectj 接入 - 非组件,第1张

概述前言Android中接入Aspectj目前常用的方式有两种,一种是项目非组件的方式接入,即我们在使用切片的module下加入编织脚本。另一种方式是组件的方式接入,就是以GradlePlugin的方式接入。对于使用的module,直接apply我们定义的Plugin就行。我个人比较推荐第二种,因为只 前言

  AndroID 中接入 Aspectj 目前常用的方式有两种,一种是项目非组件的方式接入,即我们在使用切片的 module 下加入编织脚本。另一种方式是组件的方式接入,就是以 Gradle Plugin 的方式接入。对于使用的 module ,直接 apply 我们定义的 Plugin 就行。我个人比较推荐第二种,因为只需要维护一个脚本即可。非组件的方式接入,在涉及编织的 module 下都需加入编织脚本。由于脚本依赖于 Gradle ,所以接入前说明一下本文的环境,于不同Gradle Version 可能需要稍作修改。

接入先说一下当前环境:

AndroID Studio 4.0.1

Gradle Version 6.1.1

AndroID Gradle Plugin Version 4.0.2非组件接入

1. 在 Project bulID.gradle 中添加如下:

buildscript {    ...    dependencIEs {        ...        classpath 'org.aspectj:aspectjtools:1.9.5'    }}

2. 在 Module build.gradle 中添加如下:

> 指定编织 Java Version

compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8}

> 添加 Aspectj 依赖

implementation 'org.aspectj:aspectjrt:1.9.6'

> 添加编织脚本,放在最后即可。注意下面编织是针对 application module的,如果是 library Module 下面这个行代码中的 applicationVariants 应该替换为 libraryVariants 。

import org.aspectj.brIDge.IMessageimport org.aspectj.brIDge.MessageHandlerimport org.aspectj.tools.ajc.Mainfinal def log = project.logger//注意:如果是 library Module 下面这个行代码中的 applicationVariants 应该替换为 libraryVariantsfinal 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            }        }    }}

以非组件接入的方式,涉及到需要编织代码的 module 都需要添加以上的编织脚本,然后会引发相应的错误。可能的接入错误看这篇

组件接入

 

总结

以上是内存溢出为你收集整理的Android Aspectj 接入 - 非组件全部内容,希望文章能够帮你解决Android Aspectj 接入 - 非组件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1055221.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存