6

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
}
BabyishTank
  • 865
  • 1
  • 9
  • 29
  • can you show your build.gradle file? – Nullish Byte Dec 05 '21 at 06:14
  • @SyedAffanHamdani of course, added gradle files – BabyishTank Dec 06 '21 at 15:53
  • ``` kotlinOptions { jvmTarget = '1.8' }``` is the problem. change it to: ```kotlinOptions { jvmTarget = '11' }``` should fix it. – Nullish Byte Dec 06 '21 at 21:48
  • Have you found a solution? JAVA_HOME is set to a version JDK version 13 but every time I created a new project I get the message `> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.` and I need change it in the IDE settings. I'm on macOS with Android Studio Bumblebee | 2021.1.1 Patch 1. – Klemens Zleptnig Feb 09 '22 at 17:38
  • @KlemensZleptnig sadly no – BabyishTank Feb 09 '22 at 17:56
  • 1
    I had 2 Android Studio installed. Android Studio Arctic Fox and Android Studio 4.1. I guess because of that I had 2 versions of Gradle JDK. I deleted Android Studio 4.1 and it fixed the issue. I am guessing when you install a new version of Android Studio it installs Gradle JDK under Application/Android Studio 2. And may be... the drop-down in Android Studio pulls up all the available JDK alphabetically. I don't know it is just a wild theory of mine. – Rohit Singh Feb 09 '22 at 21:48
  • @RohitSingh Yeah it looks like it takes the first one from the list (alphabetically) ‍♂️ – Klemens Zleptnig Feb 10 '22 at 18:34

4 Answers4

3

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

1

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.

Rohit Singh
  • 14,672
  • 7
  • 83
  • 77
0

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!

cliveleehere
  • 322
  • 1
  • 6
-1

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

Akhha8
  • 194
  • 9
  • 2
    Thanks for your help. using VERSION_11 cause this problem for me https://stackoverflow.com/q/65542665/5777189, that's the reason I am using VERSION_1.8 Anyway, this work well for this project. But when I create a new project, that project is using 1.8 again. any clue? – BabyishTank Dec 07 '21 at 06:13