我下载了最新的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,但当我运行时,它不工作 /图书馆/ Java / Home / JavaVirtualMachines / jdk-11.0.10.jdk /内容

我该怎么办?

我的Android Studio版本

我的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

构建。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"
}

当前回答

macOS用户

macOS中绑定的JDK是1.8,但现在我们需要11。让Gradle使用AndroidStudio的JDK来构建项目:

到文件<PROJECT_ROOT>/gradle。属性,添加行:

org.gradle.java.home=/Users/.../Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/.../Android Studio.app/Contents/jre/Contents/Home

注意:如果你没有使用Jetbrains Toolbox,并且你已经在/Application中安装了Android Studio,请使用这个路径(感谢@AndrewBekhtold):

org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/Contents/Home

其他回答

我envyronment

Windows 10 Android Studio Dolphin | 2021.3.1补丁1 颤振3.3.9

我是如何解决这个问题的…

如果在Gradle属性和环境变量中设置Java版本似乎没有任何效果,有可能是Gradle中的全局设置阻止了你的设置。

C:\Users\<yourUserName>\.gradle

寻找一个名为gradle.properties的文件。如果

org.gradle.java.home

,则其值将覆盖您在项目中使用的任何设置。

因此,解决方案是删除这一行,或者在那里设置你想要的java版本。在我的情况下,我得到了它的工作是编辑文件,看起来像

#org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true

org.gradle.java.home=C:\\Program Files\\Microsoft\\jdk-11.0.12.7-hotspot

当我在北极狐构建的终端中开始gradlew ktlintFormat时,我得到了这个错误。

I opened Project Structure (menu File → Project Structure) and selected Java 11, but it didn't help even after a restart of Android Studio. Then I changed the Java path in Settings (see the accepted answer), but it didn't help. You should set it. I tried to change Windows system variables and changed JAVA_HOME. (A short way: run cmd with administrative rights and write: setx /M JAVA_HOME "C:\Program Files\Java\jdk-11.0.11", and then restart Android Studio) Press Win+X, press "System", and enter "va". I pressed Edit button and "Browse Directory...", and then selected JRE (or JDK) folder (without bin) of Java 11. Then OK, and OK (as always). I opened cmd (command line in Windows) and typed: %JAVA_HOME%. It wrote me this error: 'C:\Program' is not recognized as an internal or external command, operable program or batch file. Looking at Setting the JAVA_HOME Variable in Windows, I found that we should replace Program Files with Progra~1 (yes, this DOS trick still works). So, replace JAVA_HOME again like in the picture below (C:\Progra~1\Android\Androi~1\jre). Restart Android Studio or reset the computer.

我正在开发一个Flutter应用程序,它没有在我的手机上运行,说明Android Gradle插件需要Java 11。

我的Android Studio版本非常旧。我没有看到屏幕截图中显示的任何选项。在系统环境变量中设置JAVA_HOME后,我更新了Android Studio。它自动选择了Java 11。

在Windows中,我在没有安装更新OpenJDK 11的情况下,从CMD将React Native v0.67升级到0.68时得到了这个错误。

首先我安装:

choco source add -n chocolatey -s 'https://chocolatey.org/api/v2/'

然后我运行这个:

choco install -y nodejs-lts openjdk11 // Without nodejs-lts I have already installed Node.js

如果你不想使用JDK 11,仍然想使用JDK 1.8,你可以恢复Android插件,如下所示。

plugins {
    id 'com.android.application' version '4.2.0' apply false
    id 'com.android.library' version '4.2.0' apply false
}