Android Studio 错误 "Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8"

标签 android android-studio gradle android-gradle-plugin

我下载了最新的 Android Studio,我想运行 Android Jetpack Compose Project,但是当我运行它时,我得到了错误:

> Failed to apply plugin 'com.android.internal.application'.
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changing `org.gradle.java.home` in `gradle.properties`.
我已经下载了 Java 11 并在 gradle.properties 中添加了 Java 11。
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home
JAVA_HOME 显示 Java 11,但是当我运行时,它不起作用 -/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home我应该怎么做?
我的 Android Studio 版本
Enter image description here
我的 Java 版本
java 11.0.10 2021-01-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.10+8-LTS-162)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.10+8-LTS-162, mixed mode)
我的 gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip
build.gradle 类路径
classpath "com.android.tools.build:gradle:7.0.0-alpha13"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31"
文件 build.gradle
plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 30

    defaultConfig {
        applicationId "com.example.testandroid3"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = "11"
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.4.31'
    }
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(11))
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
}

最佳答案

确保您的 Gradle 使用正确的 JDK。
尝试运行 ./gradlew --version在您的项目目录中。输出应该是这样的:

Gradle 7.0-rc-2
------------------------------------------------------------

Build time:   2021-04-01 21:26:39 UTC
Revision:     912a3368b654b71250dfc925a20d620393

Kotlin:       1.4.31
Groovy:       3.0.7
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.10 (Ubuntu 11.0.10+9-Ubuntu-0ubuntu1.20.10)
OS:           Linux 5.11.4-051104-generic amd64
如果 JVM 指向 1.8 版,那么您应该在设置中更改它。您可以在首选项 → 构建、执行、部署 → 构建工具 → Gradle → *Gradle JDK 中找到它。
Enter image description here

关于Android Studio 错误 "Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66980512/

相关文章:

android - Gingerbeard(2.3.7) 上的 INSTALL_FAILED_DEXOPT

android - GoogleBaseGameActivty 扩展了 BaseGameActivty(AndEngine) 崩溃

android-studio - 如何修改 gradle 文件以在 NDK 目录中运行 Make?

java - Gradle sourceCompatibility 对子项目没有影响

android - 如何使您的 TextView 背景透明?

android - 添加新插件后 cordova_plugins.js 未更新

java - Android:使用 setBackgroundDrawable 设置后位图变小

android - Android Studio 2.0 中没有空白 Activity 选项来创建

android - 如何在 gradle 上添加我的框架

android - 执行 connectedCheck 后,Realm 生成的类包含在覆盖率报告中