Android Gradle中集成AspectJ

1.新建一個(gè)plugin:

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile

class AspectJPlugin implements Plugin<Project> {
    @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 運(yùn)行時(shí)需要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;
                    }
                }
            }
        }
    }
}

在每個(gè)variant的JavaCompile Task加入aspectj的運(yùn)行代碼:

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

運(yùn)行aspectJ的參數(shù)如下:

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'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 學(xué)習(xí)android的同學(xué)都知道android工程從使用android studio開(kāi)發(fā)以后就使用了[gradle作...
    qndroid閱讀 1,312評(píng)論 0 1
  • 1.Aspect用戶行為統(tǒng)計(jì) 他不生成代碼,他是生產(chǎn)的搬運(yùn)工去做行為統(tǒng)計(jì) AOP切面編程 在AOP術(shù)語(yǔ)體系中,切面...
    waterge閱讀 537評(píng)論 0 0
  • 什么是gradle Gradle 是新一代的自動(dòng)化構(gòu)建工具,它是一個(gè)獨(dú)立的項(xiàng)目,跟 AS、Android 無(wú)關(guān),官...
    ZSGZ_AD閱讀 5,969評(píng)論 2 11
  • 前言 最近遇到了問(wèn)題,大概是 APPT2 ERROR 錯(cuò)誤,這個(gè)錯(cuò)誤很常見(jiàn),說(shuō)的是 .9圖片 有問(wèn)題,但是網(wǎng)上的...
    xiongmao_123閱讀 1,434評(píng)論 0 0
  • 前言 為什么需要學(xué)Gradle? Gradle 是 Android 現(xiàn)在主流的編譯工具,雖然在Gradle 出現(xiàn)之...
    真笨笨魚閱讀 1,595評(píng)論 0 0

友情鏈接更多精彩內(nèi)容