18

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
  • 1,329
  • 3
  • 18
  • 39
  • 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

8 Answers8

5

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 also understand that it is not an ideal solution. As deleting old Android Studio may not be the option for everyone.

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
  • 1
    I had your same versions but I didn't have another AS installed. So I ended up removing the JAVA 1.8 which was located on JavaMachine folder and it worked – Dantalian Aug 31 '22 at 15:07
5

2023 Update

I was trying to update from VERSION 1.8 to VERSION 11. Every time I close the Studio and open it again, it resets. Went through the solutions on the web and found none working. I couldn't find the file that held JDK information. The file has been changed in the newer releases.

New Solution:

Step 1: Open the Android Studio and keep it open.

Step 2: Go to the location given below:

C:\Users\{username}\AppData\Roaming\Google\AndroidStudio2022.1\options

or press Win + R and enter the following:

%userprofile%\AppData\Roaming\Google\AndroidStudio2022.1\options

Your latest version of Android Studio might be different.

Step 3: Delete the file jdk.table.xml from the options folder.

Step 4: Change the Gradle JDK to the new JDK in the settings, so that the Studio can save the new path in the file. (Important: Else Studio assigns JDK from it's memory)

Step 5: Restart Android Studio.

This fixed the issue for me. The newly created jdk.table.xml will have the updated JDK values.

  • I'd say set JDK to embedded JDK that comes with Android studio. Also this fixed gradle's another error `A problem occurred configuring project ':app'. NDK is not installed` for me. – GreenMarty Apr 26 '23 at 16:51
4

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

Neo
  • 449
  • 1
  • 5
  • 9
2

Here is what I was able to do to fix this after a bunch of googling and SO-ing:

Find the folder for your current version of AS. On Mac it was ~/<username>/Library/Application Support/Google/AndroidStudio<version>

Note: I kept messing in version4.1 stuff until I realized that I needed 2022.1 because the text wrapped and my brain kept seeing 202 2.1 instead of 2022.1.

in that folder is an options folder with a file named jdk.table.xml

I opened that in a file editor and after some messing around found this out.

my XML had all my JDKs in here. at the top was 1.8 with some other xml entries for defaults like this

<application>
  <component name="ProjectJdkTable">
    <jdk version="2">
      <name value="Kotlin SDK" />
      <type value="KotlinSDK" />
      <homePath value="$KOTLIN_BUNDLED$" />
      <roots>
        <annotationsPath>
          <root type="composite" />
        </annotationsPath>
        <classPath>
          <root type="composite" />
        </classPath>
        <javadocPath>
          <root type="composite" />
        </javadocPath>
        <sourcePath>
          <root type="composite" />
        </sourcePath>
      </roots>
      <additional />
    </jdk>
    <jdk version="2">
      <name value="Android API 31 Platform" />
      <type value="Android SDK" />
      <version value="AdoptOpenJDK (OpenJ9) version 11.0.11" />
      <homePath value="$USER_HOME$/Library/Android/sdk" />
      <roots>
        <annotationsPath>
          <root type="composite">
            <root url="jar://$USER_HOME$/Library/Android/sdk/platforms/android-31/data/annotations.zip!/" type="simple" />
          </root>
        </annotationsPath>
        <classPath>
          <root type="composite">
            <root url="jar://$USER_HOME$/Library/Android/sdk/platforms/android-31/android.jar!/" type="simple" />
            <root url="file://$USER_HOME$/Library/Android/sdk/platforms/android-31/data/res" type="simple" />
          </root>
        </classPath>
        <javadocPath>
          <root type="composite" />
        </javadocPath>
        <sourcePath>
          <root type="composite">
            <root url="file://$USER_HOME$/Library/Android/sdk/sources/android-31" type="simple" />
          </root>
        </sourcePath>
      </roots>
      <additional jdk="1.8" sdk="android-31" />
    </jdk>
    <jdk version="2">
      <name value="1.8" />
      <type value="JavaSDK" />
      <version value="AdoptOpenJDK (HotSpot) version 1.8.0_292" />
      <homePath value="/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home" />
      <roots>
    ......

I changed the info in the Android 31 Platform entry to match what I wanted, in this case Java 11. It now looks like this

<jdk version="2">
      <name value="Android API 31 Platform" />
      <type value="Android SDK" />
      <version value="AdoptOpenJDK (OpenJ9) version 11.0.11" />
      <homePath value="$USER_HOME$/Library/Android/sdk" />
      <roots>
        <annotationsPath>
          <root type="composite">
            <root url="jar://$USER_HOME$/Library/Android/sdk/platforms/android-31/data/annotations.zip!/" type="simple" />
          </root>
        </annotationsPath>
        <classPath>
          <root type="composite">
            <root url="jar://$USER_HOME$/Library/Android/sdk/platforms/android-31/android.jar!/" type="simple" />
            <root url="file://$USER_HOME$/Library/Android/sdk/platforms/android-31/data/res" type="simple" />
          </root>
        </classPath>
        <javadocPath>
          <root type="composite" />
        </javadocPath>
        <sourcePath>
          <root type="composite">
            <root url="file://$USER_HOME$/Library/Android/sdk/sources/android-31" type="simple" />
          </root>
        </sourcePath>
      </roots>
      <additional jdk="11.0.11" sdk="android-31" />
    </jdk>

make sure to change the version tag, as well as the additional tag at the bottom to match your JDK of choice

I then cut the 1.8 jdk tag and moved it to the bottom in case something needed it.

I repeated this for all the entries in the file that defaulted to 8, I think there were four--one each for android 29, 30, 31, and 32.

I restarted AS and created a new project and it worked.

I have not run into build or run issues yet.

  • This is the best answer. It can be simplified slightly further: just edit the `additional` elements, nothing else. (At least that worked great for me.) – Greg Price Feb 27 '23 at 04:36
  • One bit of general advice worth mentioning: it's a good idea to copy the whole `Google/AndroidStudioVERSION` directory somewhere else before making these edits. You probably won't need it; but if something does go wrong, you'll really want to be able to see exactly what you changed (try `git diff --no-index OLDCOPY NEWCOPY`), and/or to restore the old version exactly. And it's like 2 MB in size, so very cheap insurance. – Greg Price Feb 27 '23 at 04:40
1

What worked for me on windows is this;

Go to C:\Users<username>.jdks (Make sure you can view hidden files)

then delete the corretto-1.8.0_352 folder, this is what is set as the default gradle JDK which causes the error. Open android studio and create a new project, this worked fine because android studio had to choose another JDK as default, since 1.8 has already ben deleted, hence, no more path of the drop down list of Gradle JDK. For me, the new default gradle jdk is now jbr-17.

Note the path within android studio: Android Studio -> Settings -> Build, Execution, Deployment -> Gradle -> Gradle JDK

Screenshot of android studio Gradle JDK window

  • This should be achievable from the IDE, you should be able to remove and add JDKs without having to mess with the filesystem. Or is there a known bug that forces this action? – Martin May 08 '23 at 06:31
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 08 '23 at 06:31
0

i meet the same problem. i solved the problem now.

i reset my android studio by delete C:\Users\<UserName>\AppData\Local\Google\AndroidStudio<Version>, C:\Users\<UserName>\AppData\Roaming\Google\AndroidStudio<Version>. (you'd better close android studio before), then restart android studio.you will see the gradle sdk is default set as android studio default sdk.

as you can see, the solution will delete your android studio configuration. if you have important configuration in android studio, you should back up the configuration to restore.

Ting
  • 21
  • 3
-2

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
  • 418
  • 3
  • 10
  • 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
-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!

cliveleehere
  • 316
  • 1
  • 6