我下载了最新的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`.

我已经下载了Java11,并在gradle.properties个版本中添加了Java11.

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home

JAVA_主页显示JAVA 11,但当我运行时,它不工作-

我该怎么办?

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

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版,那么应该在设置中更改它.你可以在Preferences英镑内找到它→ Build, Execution, DeploymentBuild ToolsGradle→ *格雷德尔·JDK.

Enter image description here

Android相关问答推荐

有人能帮我在应用程序上使用模拟位置时避免被发现吗?我已经反编译并粘贴了一个代码,S小文件

try 用Jetpack Compose理解视图模型和导航

如何将两个变量传递给Nav主机,然后将其传递给另一个屏幕?

LaunchedEffect没有延迟时应用程序崩溃

在c中更新MVVMCross中TextView的Alpha#

如何在 compose android中将图像覆盖在另一个图像上

如何在我的sqlite数据库中获取某个玩家的分数

将 React Native 应用程序背景带到前台

通知使用默认语言,屏幕显示多种语言,同时通过 AppCompatDelegate 设置应用程序语言

列表更改时,RecyclerView 中的列表不会更新

如何以编程方式通过 whatsapp android 共享图像和文本

在 react native 中设置 react-native-paper 组件的样式

为什么集成React Native时compileSdkVersion错误?

是什么让 Android Studio 中的按钮变成紫色?加上新手的其他奇怪行为

Jetpack Compose:如何绘制这样的路径/线

Android Studio:如何添加应用程序质量洞察窗口以查看 Android Studio 中的 Crashlytics 数据?

compose FontFamily 错误:必须初始化变量

为什么使用 React Native 和 expo 创建的 APK 体积这么大?

Google Play 服务登录在 Unity Android 上无法正常运行

使用 Android 字符串数组在 Room 中迁移