Fix the Error “Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8”

Java is an ideal programming language used for creating infinite applications. Android studio is also used along with Java as it is a Java Application. Programmers used Android Studio to create applications for Android phones, tablets, Android TV, Android Auto, and Android wear. When you are working with the Java application Android studio, you may experience “Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8”. Check out how the error shows up

How the error occurs

When you downloaded or upgraded to the latest Android Studio to create a project, you end up with the following error

Failed to apply plugin ‘com.android.internal.application’. > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.

When you have Java 11 already downloaded as well as Java 11 is added in gradle.property:

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home

Java 11 is shown by the JAVA_HOME, but as soon as you try to run it, it does not work:

/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home

The Java version you may have:

java 11.0.10 2021-01-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.10+8-LTS-162)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.10+8-LTS-162, mixed mode)

gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip

build.gradle classpath

classpath "com.android.tools.build:gradle:7.0.0-alpha13"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31"

File build.gradle

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

android {
    compileSdk 30

    defaultConfig {
        applicationId "com.example.testandroid3"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = "11"
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.4.31'
    }
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(11))
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
}

This is exactly how you land up in trouble. Check out the solutions to handle it

Solutions To Fix the Error “Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8”

Solution 1 – Gradle should use the correct JDK

To sort the error out, you need to ensure the gradle you have uses the proper and correct JDK. In the directory of your project, run ./gradlew –version. You get the output similar to the following:

Gradle 7.0-rc-2
------------------------------------------------------------

Build time:   2021-04-01 21:26:39 UTC
Revision:     912a3368b654b71250dfc925a20d620393

Kotlin:       1.4.31
Groovy:       3.0.7
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.10 (Ubuntu 11.0.10+9-Ubuntu-0ubuntu1.20.10)
OS:           Linux 5.11.4-051104-generic amd64

You must modify settings if you are having the JVM that still uses the 1.8 version. To do that, you can simply get it in Preference that leads to Build, Execution, Deployment → Build Tools → Gradle → *Gradle JDK. It can fix the error.

Solution 2 – Switch the gradle JDK to 11

This is the easiest way to get rid of the error. You just need to switch the Gradle JSK to 11. To do that, follow the below steps:

  • In case of JVM is set to the 1.8 version, you need to adjust this in the settings.
  • You can simply find it in the Preferences.
  • Next, you need to select Build, Execution, and Deployment using the drop-down.
  • Choose the Build Tools option.
  • You will see the Gradle option there.
  • Set the gradle JDK to 11 for which you came.

It helps you make the error go away.

Solution 3 – Choose the JDK version

To resolve the error, you need to choose the JDK version. To do that, you can simply follow the below steps:

  • First, you need to visit the official website.
  • Download the ZIP JDK file according to the system you use, i.e; Windows.
  • Next, you need to extract that zipped file in any folder you want in the root directory of your system with permissions.
  • Open and visit the Android studio project structure, which can be found in men file that directs to the project structure. 
  • In the end, just paste the directory (where the JDK version is unzipped) here.

Conclusion

We highlighted the solutions to help you successfully remove the error “Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8”. I hope you find it interesting.

I wish you all the best!

Leave a Reply

Your email address will not be published. Required fields are marked *