这个编译器不支持 API 级别为 31。请使用 API 级别为 30 或更早的版本。

20

我将现有的安卓项目升级到了API级别31,使用Java语言进行开发。同时修改了build.gradle文件

compileSdkVersion 31
defaultConfig {
    applicationId 'com.app.app'
    minSdkVersion 16
    targetSdkVersion 31
    versionCode 91
    versionName '4.0.1'
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}

但是我无法使用AVD或真实设备调试应用程序。我遇到了这个错误。

> Task :app:mergeProjectDexDebug
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
AGPBI: {"kind":"warning","text":"An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier","sources":[{}],"tool":"D8"}
An API level of 31 is not supported by this compiler. Please use an API level of 30 or earlier

我想我需要更新编译器,我该怎么做?


2
你可能需要启用Java 8。 - cmak
2个回答

16

尝试将以下内容添加到您的 build.gradle 文件中:

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  kotlinOptions {
    jvmTarget = '1.8'
  }
}

2
对于任何遇到此问题的人,您需要升级gradle(graddle-wrapper-propertise),在我的情况下,我从6.7.1升级到6.9。 - mazen amr
不错。我发现有人在三个不同的build.gradle文件中将jvmtarget设置为JavaVersion.VERSION_1_8。将其重置为数字字符串似乎解决了问题。 - Sinc

10

看起来你使用的gradle版本不支持D8。 但更改gradle版本可能是一件大事,特别是对于维护旧项目的人来说。 而且D8用于减小APK文件大小。这只是一个警告,不是错误。 因此,合理的做法可能是...忽略这个消息。 :)


1
这个答案比它所得到的赞誉要好得多。 - edhnb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接