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

当前回答

我看到这个是因为在我的Bitbucket服务器上设置了管道,我需要从docker-android-sdk/android-28镜像构建,该镜像与openjdk-8打包在一起。

我在bitbucket-pipeline中添加了下面这行代码。在运行Gradle构建之前安装openjdk-11:

- apt-get install -y openjdk-11-jdk

其他回答

注意:这是一个解决方案,如果你在Azure DevOps管道上得到这个错误

如果您在项目中使用Java 11,但构建应用程序的Azure DevOps Pipeline设置为使用Java 1.8,则会出现此错误。

要修复它,添加以下(用于窗口池):

    - task: JavaToolInstaller@0
      inputs:
        versionSpec: '11'
        jdkArchitectureOption: 'x64'
        jdkSourceOption: 'PreInstalled'

在macOS M1上,我通过以下步骤解决了这个问题。

打开文件~/。zshrc在Visual Studio Code 增加了这句话: export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/Contents/Home" 保存文件 用修改后的文件重新加载终端 源~ / . zshrc 通过运行测试更改是否已加载 echo $ JAVA_HOME 验证显示的位置是否正确。 通过运行,验证Gradle显示正确的Java 11版本 cd安卓 / gradlew——版本

结果如下:

------------------------------------------------------------
Gradle 7.3.3
------------------------------------------------------------

Build time:   2021-12-22 12:37:54 UTC
Revision:     6f556c80f945dc54b50e0be633da6c62dbe8dc71

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          11.0.12 (JetBrains s.r.o. 11.0.12+0-b1504.28-7817840)
OS:           Mac OS X 12.2.1 aarch64

试图通过运行重新构建它:

yarn android

它成功了。

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

在Android Studio Arctic Fox(4.2)中,将Gradle JDK更改为11对我来说很有效:

步骤1:

导航到:菜单文件→设置→构建、执行、部署→构建工具→Gradle

步骤2:

将Gradle JDK更改为11,在Gradle Projects下面。

步骤3:

申请,并重建项目!

在Android Studio 4.2 (Arctic Fox), 2020.3.1 Patch 3中,之前的答案都不适合我。

只添加org.gradle.java。home=<Path-to-JDK-v11> to gradle。Properties在我的案例中发挥了作用。

如果您使用的是Linux,只需添加org.gradle.java.home=/opt/android-studio/jre。