'How to change Android Studio default gradle JDK to 11

Getting this message when I click run:

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

The answers from Error message "Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8" to change the Gradle JDK in settings work. but only for that one project. When I create a new project, Android Studio automatically uses default 1.8 again.

I have 11 available, AS just doesn't automatically use it. Things I tried: changed the JAVA_HOME env to the location for JDK 11 here, but still get the same error.

Invalid cache /restart a bunch of time does nothing

How to set it once and apply it to all future new projects?

enter image description here

Attaching app/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.gradle

// 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
}


Solution 1:[1]

Might seem late, but I fixed mine by just installing the java jdk needed, installed jdk 17 by the way. That's after downloading it from my browser and installing it. then you could check your java version on your device using; "java --version" command on cmd, then you could just restart your pc and open android studio. then check the jvm of gradlew --version command on terminal in android studio and if it's updated you are good to go

Solution 2:[2]

Multiple Android Studio Installed?

Short Answer:

Just delete the other version and it should fix the problem.

Long answer:

I had two versions of Android Studio installed Android 4.2 and Android Arctic Fox. Both Android Studio had different Gradle JDK version installed.

This is what I was seeing in Arctic Fox Gradle Settings. (Default selection was 1.8)

enter image description here

My theory:

If you notice Gradle JDK is installed under Applications/Android Studio/Content/

It's my assumption that Gradle Settings page pulls up all the installed Gradle JDK versions. And there is no option to set the default version in Settings page. So the first Gradle version becomes the default.

I don't know if it is alphabetically sorted or based on Download timestamp.

But I believe it is based on the timestamp. The oldest installed version gets to the top in the list.

Here is a supporting observation. When I deleted Android Studio 4.2 and ctrl+z (Undo) it the list order changed.

enter image description here

Disclaimer:

I could be completely wrong as I don't have any official doc, link that support this claim. It is just a theory.
And I understand that is is not an ideal solution as deleting old Android Studio may not be the option for everyone.

Solution 3:[3]

Remove any older sdk platforms you have installed via the SDK manager

Make sure to click on the show package details.

Restart Android Studio, create a new project. Voila!

Solution 4:[4]

Have you tried: app module build.gradle file.

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

Also try to select the default provided Android Studio SDK enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 onyekachuwkwu_emmanuel
Solution 2
Solution 3 cliveleehere
Solution 4 Akhha8