149
votes

I download the newest Android Studio, I want to run the Android Jetpack Compose Project, But when I run , I got the error:

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

I already download Java 11, And add java 11 in gradle.properties.

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

The JAVA_HOME shows Java 11, But I run, It doesn't work, /Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home

How should I do?

My Android Studio Version enter image description here

My Java Version

    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)

My 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"

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"
}
9

9 Answers

283
votes

Make sure that your gradle is using proper JDK. Try running ./gradlew --version in your project's directory, output should be something like this:

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

If JVM points to version 1.8 then you should change it in settings. You can find it in Preferences > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK

enter image description here

8
votes

You can solve it in this simple way.

  1. Go to https://www.oracle.com/java/technologies/javase-jdk16-downloads.html

  2. Download the jdk zipfile of your system. (Lets say windows)

  3. Extract it in any folder with high permissions under your PC main directory

  4. Now open and Go to your Android studio project structure located at File > Project structure

  5. And paste the directory where you unzipped the jdk version here, As you see, mine is jdk 16 as at now and installed in that directory as seen in the pictureenter image description here

7
votes

I got this error when started gradlew ktlintFormat in Terminal of Arctic Fox build.

  1. I opened Project Structure (File > Project Structure) and selected Java 11, but it didn't help even after restart of Android Studio.

enter image description here

  1. Then I changed Java path in Settings (see the accepted answer), but it didn't help. You should set it.

  2. I tried to change Windows system variables and changed JAVA_HOME.

(A short way: run cmd with administrative rights and write: setx /M JAVA_HOME "C:\Program Files\Java\jdk-11.0.11", then restart Android Studio)

Press Win+X, press "System", enter "va".

enter image description here

enter image description here

I pressed "Edit" button and "Browse Directory...", then selected JRE (or JDK) folder (without bin) of Java 11. Then OK, OK (as always).

enter image description here

  1. I opened cmd (command line in Windows) and typed: %JAVA_HOME%. It wrote me this error:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

Looking at https://confluence.atlassian.com/doc/setting-the-java_home-variable-in-windows-8895.html I found that we should replace Program Files with Progra~1 (yes, this DOS trick still works). So, replace JAVA_HOME again like in the picture below.

enter image description here

  1. Restart Android Studio or reset a computer.
5
votes

I got this error when i update the one of dependency in android studio that requires a JAVA 11. you just need to follow these steps.

(1) Download the JAVA 11, for mac download dmg and for windows zip file. link : Download JAVA 11

(2) select options at Android studioenter image description here

Follow the steps and choose your newly saved JAVA Sdkenter image description here

Follow the steps and choose your newly saved JAVA Sdkenter image description here

(Follow the steps and choose your newly saved JAVA Sdk)

1
votes

New Version Gradle version need Java 11. You need to download java 11 and update in to gradle.

  1. Firstly need to check your current jdk version. './gradlew --version' Use this comment in your project terminal to know current using jdk version. You will get below image JdK version

If JVM points to version 1.8 then you should change it in settings.

For Ubuntu and Windows -> File-> Settings-> Build,Execution,Deployment-> Build Tools-> Gradle-> Gradle JDK. Please check below image

Windows

For Mac ->Preferences > Build->Execution, Deployment -> Build Tools -> Gradle -> Gradle JDK

Its work for me perfectly.Happy coding

0
votes

I ran into a similar problem when trying to run the benchamrking samples for the first time.

The instructions to run the sample were to execute

./gradlew macrobenchmark:cC

in the IDE terminal, but in my case this resulted with the same error.

I had tried the accepted answer above from M Tomczyński above, but that didn't work for me. I believe this was as a result of my existing config for JAVA_HOME (which can be found here).

This also meant that option 2, changing my JAVA_HOME config, wasn't an option.

The change which did work for me though was option 3 in the suggestions above. I checked the path for Java 11 from the answer above and added that into gradle.properties, and then sync'd gradle as per the IDE prompt, and re-ran the macrobenchamark gradle command successfully.

org.gradle.java.home=/Applications/Android Studio Preview.app/Contents/jre/Contents/Home

0
votes

For me its as simple as making sure I'm using the Gradle JDK located in [Android Studio install path]/jre. Try it, might also work for you

0
votes

On Mac only setting JAVA_HOME to JDK 11 embedded to Android Studio helped.

I did it with this command in the command line: export JAVA_HOME=ENTER_PATH_TO_JDK_HERE.

PATH_TO_JDK could be something like /Users/userName/Library/Application\ Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/203.7583922/Android\ Studio.app/Contents/jre/Contents/Home

0
votes

I have concluded that Gradle calls the version of Java installed on the system, regardless of Android Studio settings.

On macOS, check the list of installed Java versions in the system with the command:

/usr/libexec/java_home -V

If there is no java 11 or higher among them, you will get an error like the author of the question.

The solution is to install the Java 11 or higher.

The second solution is to make a symlink in the folder ~/Library/Java/JavaVirtualMachines/ to the JRE embedded in Android Studio with the command:

ln -s "/Applications/Android Studio.app/Contents/jre" "~/Library/Java/JavaVirtualMachines/Android Studio Default JRE"

Additionally you can add this line to ~/.zshenv:

export JAVA_HOME=/usr/libexec/java_home

You can get installation paths for different java versions if needed:

/usr/libexec/java_home -v 11

or

/usr/libexec/java_home -v 1.8