【错误记录】Android Studio 编译报错 ( The dependency contains Java 8 bytecode. Please enable desugaring by )

2023-03-30 15:22:53 浏览数 (1)

文章目录

  • 一、报错信息
  • 二、解决方案

一、报错信息


在 Android Studio 中编译工程时报如下错误信息 :

代码语言:javascript复制
AGPBI: {"kind":"error","text":"Invoke-customs are only supported starting with Android O (--min-api 26)","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"error","text":"Static interface methods are only supported starting with Android N (--min-api 24): Landroidx/constraintlayout/core/motion/utils/TypedValues$Attributes;getId(Ljava/lang/String;)I","sources":[{}],"tool":"D8"}

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDexDebug'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Failed to transform constraintlayout-2.1.0.aar (androidx.constraintlayout:constraintlayout:2.1.0) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, dexing-enable-desugaring=false, dexing-incremental-transform=true, dexing-is-debuggable=true, dexing-min-sdk=18, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingNoClasspathTransform: C:Usersoctop.gradlecachestransforms-2files-2.1fe17b6099b802ba9bf740bdfca370160constraintlayout-2.1.0-runtime.jar.
         > Error while dexing.
           The dependency contains Java 8 bytecode. Please enable desugaring by adding the following to build.gradle
           android {
               compileOptions {
                   sourceCompatibility 1.8
                   targetCompatibility 1.8
               }
           }
           See https://developer.android.com/studio/write/java8-support.html for details. Alternatively, increase the minSdkVersion to 26 or above.

   > Failed to transform constraintlayout-core-1.0.0.jar (androidx.constraintlayout:constraintlayout-core:1.0.0) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, dexing-enable-desugaring=false, dexing-incremental-transform=true, dexing-is-debuggable=true, dexing-min-sdk=18, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingNoClasspathTransform: C:Usersoctop.gradlecachesmodules-2files-2.1androidx.constraintlayoutconstraintlayout-core1.0.03b64bb29201ac1b5cb418bee55e9ae3d7940486constraintlayout-core-1.0.0.jar.
         > Error while dexing.
           The dependency contains Java 8 bytecode. Please enable desugaring by adding the following to build.gradle
           android {
               compileOptions {
                   sourceCompatibility 1.8
                   targetCompatibility 1.8
               }
           }
           See https://developer.android.com/studio/write/java8-support.html for details. Alternatively, increase the minSdkVersion to 24 or above.


* 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 25s
55 actionable tasks: 3 executed, 52 up-to-date

二、解决方案


上述报错是由于在 主应用 中 , 设置的 Java 版本为

1.7

版本 , 但是在依赖库 " constraintlayout-core-1.0.0.jar " 中 , 使用的 Java 版本是

1.8

版本 , 因此报错 ;

这里按照 错误提示中的修改方法 , 将 " build.gradle # android # compileOptions " 中的 sourceCompatibility 和 targetCompatibility 属性修改为

1.8

即可 ;

代码语言:javascript复制
           android {
               compileOptions {
                   sourceCompatibility 1.8
                   targetCompatibility 1.8
               }
           }

也可以写成如下形式 ;

代码语言:javascript复制
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

0 人点赞