如何将Android Studio默认的gradle JDK更改为11

nhjlsmyf  于 2023-01-21  发布在  Android
关注(0)|答案(4)|浏览(1534)

单击“运行”时收到此消息:

> 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`.

Error message "Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8"在设置中更改Gradle JDK的答案有效。但仅适用于该项目。当我创建新项目时,Android Studio会自动再次使用默认版本1.8。
我有11可用,因为只是不自动使用它。我尝试的事情:已在此处将JAVA_HOME env更改为JDK 11的位置,但仍收到相同的错误。
无效缓存/重新启动一堆时间不起作用

如何设置一次并将其应用于所有未来的新项目?

附加应用程序/build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.grid"
        minSdk 22
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}

顶级构建分级

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
eni9jsuy

eni9jsuy1#

可能看起来晚了,但是我通过安装所需的java jdk修复了我的,顺便安装了jdk 17。这是在从我的浏览器下载并安装之后。然后你可以使用检查你的java版本;“java --version”命令在cmd上,那么你可以重新启动你的pc并打开android studio.然后检查gradlew的jvm--version命令在android studio中的终端上,如果它的更新,你是好的去

bwitn5fc

bwitn5fc2#

安装了多个Android Studio?

简短回答:

只要删除另一个版本,它应该会修复这个问题。

长答案:

我安装了两个版本的Android StudioAndroid 4.2Android Arctic Fox。两个Android Studio都安装了不同的Gradle JDK版本。
这是我在北极狐Gradle设置中看到的。(默认选择是1. 8)

我的理论是:
如果您注意到Gradle JDK安装在应用程序/Android Studio/内容/
我假设Gradle设置页面会显示所有已安装的Gradle JDK版本,并且设置页面中没有设置默认版本的选项,因此第一个Gradle版本将成为默认版本。
我不知道它是按字母顺序排序还是基于下载时间戳
但我相信它是基于时间戳的。最早安装的版本会排在列表的首位。
这里有一个支持性的观察,当我删除Android Studio 4. 2和Ctrl+Z(撤销)时,列表顺序发生了变化。

免责声明:

我可能是完全错误的,因为我没有任何官方文件,链接,支持这一说法。这只是一个理论。
而且我也明白这不是一个理想的解决方案,因为删除旧的Android Studio可能不是每个人的选择。

pcrecxhr

pcrecxhr3#

您是否尝试过:应用模块构建. gradle文件。

android {
    // ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
    }
}

此外,请尝试选择默认提供的Android Studio SDK

vu8f3i0k

vu8f3i0k4#

删除您通过SDK管理器安装的任何较旧的SDK平台

重新启动Android Studio,创建一个新项目。

相关问题