skip to Main Content

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
}

7

Answers


  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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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!

    Login or Signup to reply.
  5. 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.

    Login or Signup to reply.
  6. i meet the same problem. i solved the problem now.

    i reset my android studio by delete C:Users<UserName>AppDataLocalGoogleAndroidStudio<Version>, C:Users<UserName>AppDataRoamingGoogleAndroidStudio<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.

    Login or Signup to reply.
  7. 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}AppDataRoamingGoogleAndroidStudio2022.1options
    

    or press Win + R and enter the following:

    %userprofile%AppDataRoamingGoogleAndroidStudio2022.1options
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search