此编译器不支持 API 级别 31。请使用 30 或更早的 API 级别

Lak*_*hna 20 android android-studio

我将现有的 android 项目升级到 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
}
Run Code Online (Sandbox Code Playgroud)

但我无法使用 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
Run Code Online (Sandbox Code Playgroud)

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

小智 16

尝试将其添加到您的build.gradle文件中:

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  kotlinOptions {
    jvmTarget = '1.8'
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于任何遇到此问题的人,您需要升级您的 gradle(gradle-wrapper-propertise),在我的例子中,我从 6.7.1 升级到 6.9 (2认同)

Ora*_*nge 10

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