我下载了最新的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"
}

当前回答

在Mac上,只有将JAVA_HOME设置为嵌入到Android Studio的JDK 11才有帮助。

我在命令行中使用这个命令:

export JAVA_HOME=ENTER_PATH_TO_JDK_HERE

PATH_TO_JDK可以是/Users/userName/Library/Application\ Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/203.7583922/Android\ Studio.app/Contents/jre/Contents/Home

其他回答

我的问题是我使用的是Arctic Fox(4.2)之前的Android Studio版本。而且似乎只有Arctic Fox支持JDK 11。

如果你正在使用MS App Center,当你在Android项目中使用Java 11时,你可能会面临同样的问题。如果您正在寻找MS应用程序中心问题的解决方案。这可能对你有帮助!

访问https://appcenter.ms/users/user1/apps/yourapp/build/branches 单击设置图标转到生成配置 向下滚动到环境并打开。 设置JAVA_HOME为$(JAVA_HOME_11_X64)

这是我在macOS上修复它的方法。

./gradlew --version

点击首选项→构建、执行、部署→Gradle

将Gradle JDK更改为最新版本。

在安装Android Studio之前,我只安装了一个JDK (Microsoft build OpenJDK 17)。由于未知原因,JDK 1.8已经随Android Studio安装,并将其设置为Gradle默认的/Users/<username>/Library/Java/JavaVirtualMachines/corretto-1.8.0_322

我把这个添加到gradle中解决了这个问题。属性文件:

org.gradle.java.home=/usr/lib/jvm/java-11-openjdk-amd64

但是您的Java 11可能位于不同的目录中。

以Windows为例:

org.gradle.java.home=C\:\\Program Files\\Java\\jdk-13.0.2

当我在北极狐构建的终端中开始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.