【错误记录】Android Studio 配置 AspectJ 报错 ( Failed to create Jar file C:\xxx\aspectjtools-1.8.10.jar. )

简介: 【错误记录】Android Studio 配置 AspectJ 报错 ( Failed to create Jar file C:\xxx\aspectjtools-1.8.10.jar. )

文章目录

一、报错信息

二、解决方案

三、Gradle 与 Gradle 插件版本适配





一、报错信息


在 Android Studio 中配置 AspectJ , 配置后的 Module 下的 build.gradle 如下 : 这是在 AS 中配置 AspectJ 的固定配置 ;


buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.aspectj:aspectjtools:1.8.10'
        classpath 'org.aspectj:aspectjweaver:1.8.10'
    }
}
plugins {
    id 'com.android.application'
}
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.example.aop_demo"
        minSdkVersion 18
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
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()) {
        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;
            }
        }
    }
}
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation files('libs/aspectjrt.jar')
}


报错信息如下 :


A problem occurred configuring project ':app'.
> Failed to create Jar file C:\Users\octop\.gradle\caches\jars-8\98ff8367eea2b127f04c33920487762b\aspectjtools-1.8.10.jar.
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'.
Caused by: org.gradle.api.GradleException: Failed to create Jar file C:\Users\octop\.gradle\caches\jars-8\98ff8367eea2b127f04c33920487762b\aspectjtools-1.8.10.jar.
Caused by: java.lang.IllegalArgumentException
  at org.objectweb.asm.ClassReader.<init>(ClassReader.java:259)
  at org.objectweb.asm.ClassReader.<init>(ClassReader.java:177)
  at org.objectweb.asm.ClassReader.<init>(ClassReader.java:163)
  ... 6 more

image.png



Executing tasks: [clean] in project D:\002_Project\002_Android_Learn\AOP_Demo



FAILURE: Build failed with an exception.


* What went wrong:
A problem occurred configuring project ':app'.
> Failed to create Jar file C:\Users\octop\.gradle\caches\jars-8\98ff8367eea2b127f04c33920487762b\aspectjtools-1.8.10.jar.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 529ms







二、解决方案


尝试过很多方案后 , 发现是 Gradle 版本 和 Gradle 插件版本的问题 , 这个配置很旧 , 需要使用老版本的的 Gradle 和 对应的插件版本 ;


Gradle 版本 : 在 \gradle\wrapper\gradle-wrapper.properties 中配置 5.6.4 5.6.45.6.4 版本的 Gradle ;


distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip


Gradle 插件版本 : 在 3.6.1 3.6.13.6.1 版本的 Gradle 插件 ;


buildscript {
    dependencies {
        classpath "com.android.tools.build:gradle:3.6.1"
    }
}


image.png






三、Gradle 与 Gradle 插件版本适配


参考文档 : https://developer.android.google.cn/studio/releases/gradle-plugin


image.png

目录
相关文章
|
8天前
|
Android开发
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
33 1
|
28天前
|
Android开发
Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
30 3
|
28天前
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associate
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associate
31 2
|
7天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
24 1
|
7天前
|
安全 编译器 API
Android HAL深入探索(5): 调试HAL报错与解决方案
Android HAL深入探索(5): 调试HAL报错与解决方案
10 1
|
8天前
|
API Android开发
Android Framework增加API 报错 Missing nullability on parameter
Android Framework增加API 报错 Missing nullability on parameter
9 1
|
21天前
|
Android开发 开发者
安卓投屏神器 Scrcpy安 报错ERROR: Could not find any ADB device
使用Scrcpy安卓投屏工具时遇到报错,问题根源是未开启开发者模式。解决步骤:进入设置,点击【关于手机】→连续点击版本号激活开发者模式,然后在【系统设置】→【开发者选项】中开启USB调试。参照此方法后可正常执行。Scrcpy软件下载链接和GitHub页面也已提供。
27 1
|
1月前
|
Android开发
Android 开发 读取excel文件 jxl.jar包
Android 开发 读取excel文件 jxl.jar包
8 0
|
1月前
|
Android开发
android studio 重新将module中的代码加入到自己项目中,报错找不到SO文件。
android studio 重新将module中的代码加入到自己项目中,报错找不到SO文件。
9 1
|
Android开发
【错误记录】Android Studio 编译报错 ( Installed Build Tools revision 31.0.0 is corrupted )
【错误记录】Android Studio 编译报错 ( Installed Build Tools revision 31.0.0 is corrupted )
669 0
【错误记录】Android Studio 编译报错 ( Installed Build Tools revision 31.0.0 is corrupted )