Android Aspectj 接入錯誤記錄


接入錯誤

1. org.aspectj.apache.bcel.classfile.ClassFormatException

org.aspectj.apache.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 18

遇到這個問題是添加Aspectj版本號比較低的原因,更新到最新版本即可。目前是2020/12/8 版本號1.9.6 。最新版本號可以去官網查看 

dependencies {
     classpath "com.android.tools.build:gradle:4.0.2"
     classpath 'org.aspectj:aspectjtools:1.9.5'
}
dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'org.aspectj:aspectjrt:1.9.6'
}

2.  Default interface methods are only supported starting with Android N (--min-api 24)

Default interface methods are only supported starting with Android N (--min-api 24): void org.aspectj.lang.ProceedingJoinPoint.stack$AroundClosure(org.aspectj.runtime.internal.AroundClosure)
Stack trace:
com.android.tools.r8.a: Default interface methods are only supported starting with Android N (--min-api 24): void org.aspectj.lang.ProceedingJoinPoint.stack$AroundClosure(org.aspectj.runtime.internal.AroundClosure)
    at com.android.tools.r8.dex.r.a(:60)

這個其實是因為 java8 才支持靜態接口方法的原因,所以我們需要指定 module 編譯 javaVersion。

android {
    ...

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

添加以上配置即可解決。

3 . java.lang.NoSuchMethodError: No static method aspectOf()

java.lang.NoSuchMethodError: No static method aspectOf()

出現錯誤的根本原因是代碼未織入成功。可能造成的原因是

  1. 未在對應 module build.gradle下添加對應編織腳本。
  2. @Aspect 修飾的切片未指定為 public 。

解決方式:

import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
final def log = project.logger
// 這個是 application module
//android.applicationVariants.all{...}
// 這個是 library module
android.libraryVariants.all{ variant ->
    if (!variant.buildType.isDebuggable()) {
        log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
        return
    }

    JavaCompile javaCompile = variant.javaCompiler
    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 build.gradle 下添加以上編織腳本即可正常運行。如果不想每個 module下都天加這樣的編織腳本,我們可以自定義 gradle plugin。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM