Android 与 sourceCompatibility = JavaVersion.VERSION_11 给出错误错误:“包 android.app 不存在”

tm1*_*701 3 android-studio

当使用 JDK 11 目标时,我收到此错误:

错误:包 android.app 不存在

我看到了升级到“Canary”AS 版本的解决方案。我更喜欢使用测试版或稳定版 Android Studio。我定期更新它。不打算仅仅为了升级而进行重大改变。

因此,这效果很好:即使使用 Android Studio“项目设置”和带有“JDK 11”的“JDK”。

当 build.gradle 具有这些编译选项时,一切正常。

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
Run Code Online (Sandbox Code Playgroud)

这给出了提到的错误:

compileOptions {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}
Run Code Online (Sandbox Code Playgroud)

我的 build.grade 项目文件是:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.1'
        classpath 'org.ajoberstar:grgit:3.3'
    }
}

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        google()
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 build.grade 模块文件的部分内容是:

apply plugin: 'com.android.application'
repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
    google()
}
Run Code Online (Sandbox Code Playgroud)

如何使用 (Oracle) JDK 11 编译器正常构建?

tm1*_*701 6

刚刚收到 Android Studio / Gradle 7 升级。

现在一切正常!

  1. 使用它作为您的编译器选项:
compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
}
Run Code Online (Sandbox Code Playgroud)
  1. 在设置中您会看到 JDK 11 已被选择。如果未自动选择,您将可以选择该 JDK 11 版本。

在此输入图像描述