Reputation: 9326
I downloaded the newest Android Studio, and I wanted to run the Android Jetpack Compose Project, but when I ran it, 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 downloaded Java 11 and added 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 when I run, it doesn't work -
/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home
How should I do?
My Android Studio version
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"
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"
}
Upvotes: 921
Views: 800932
Reputation: 27
For Android Studio Hedgehog | 2023.1.1 Patch 1
in VS code in terminal - ~/.zshrc
ctrl+click on it it will open file
set path export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"
save it run it.
Happy coding!!
Upvotes: 1
Reputation: 126563
Using Flutter
with Android Studio
, I had the message:
Android Studio Error "Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8"
I tried to define in "Project Structure" Java 11 as the version used for my project with no success.
But setting the path of Java 11 into my gradle.properties
org.gradle.java.home=C:\\Users\\jorgesys\\.jdks\\corretto-11.0.20
my application was created with no problems
Upvotes: 0
Reputation: 119
For macbook m chips
first check JVM check for this case it is 1.8.0_39
then go to project structure (file>project structure) on sdk section select add sdk then click download JDK With a version matching with JVM for 1.8 download version 18 etc
Upvotes: 2
Reputation: 757
For windows (maybe will work on other systems too):
Upvotes: 0
Reputation: 2041
What worked for me on MacOS was first, find where Java Home is:
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
then update my JAVA_HOME export in ~/.bash_profile
to reflect the output of that command:
export JAVA_HOME="/usr/local/Cellar/openjdk/17.0.1_1/libexec/openjdk.jdk/Contents/Home"
Upvotes: 0
Reputation: 4834
Note: this is a solution if you are getting this error on the Azure Pipelines.
If you are using Java 11 in your project, but the Azure DevOps pipeline that builds the application is set to use Java 1.8, you will get this error.
To fix it, add following (for Windows pool):
- task: JavaToolInstaller@0
inputs:
versionSpec: '11'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
Upvotes: 2
Reputation: 929
In Android Studio I just did this:
Preferences → Build,Execution,Deployment → Build Tools → Gradle
Easy peasy.
Upvotes: 39
Reputation: 929
When I upgraded to Android Studio 15 (Dolphin) I got this error, and it is sooooo easy to fix. This answer is the easiest, fastest and the best...
In Android Studio I just did this:
Preferences → Build,Execution,Deployment → Build Tools → *Gradle. Select Java 11 or 12, etc..
Easy peasy
See the picture.
Upvotes: 25
Reputation: 7495
If setting the Java version in the Gradle properties and in the Environment Variables seems not to have any effect, chances are that there is a Global setting from Gradle blocking yours.
Go to
C:\Users\<yourUserName>\.gradle
and look for a file named gradle.properties
. If
org.gradle.java.home
is set there, then its value will override any setting you use in the project.
So, the solution would be either remove that line, or set right there the java version you wish. In my case I got it working be editing the file as to look like:
#org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
org.gradle.java.home=C:\\Program Files\\Microsoft\\jdk-11.0.12.7-hotspot
Upvotes: 3
Reputation: 311
Add this to file gradle.properties:
org.gradle.java.home=<path to JDK 11>
Example:
C:\\Program Files\\Java\\jdk-11.0.15
Upvotes: 14
Reputation: 7188
Changing the Gradle JDK in Android Studio Preferences didn't work for me.
But I was able to correct the JDK by adding the org.gradle.java.home
path in the gradle-wrapper.properties
file.
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
# I had to add this line (macOS)
org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/Contents/Home
I found the JAVA_HOME path by going to menu File → Project Structure → SDKs → Choose the JDK you want to use → JDK home path.
After that, I was able again to run Gradle Wrapper (gradlew
) commands.
Upvotes: 5
Reputation: 2633
My environment:
You need not to switch Java 11 or higher. You can follow my steps as below.
Solved by:
Changing gradle classpath at android/build.gradle file under dependencies,
classpath 'com.android.tools.build:gradle:7.1.2'
TO
classpath 'com.android.tools.build:gradle:4.1.0'
Upvotes: 2
Reputation: 136
Just add the below property in file gradle.properties.
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
(This is for macOS. Give the path as per OS). This fix will work for any Android Studio version.
Upvotes: 2
Reputation: 1370
⚠️ Note: this is a solution if you are getting this error on the GitHub Actions Pipeline
If you are using Java 11 in your project, but the GitHub Action Pipeline that builds the application is set to use Java 1.8, you will get this error.
To fix it, replace the following:
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
with:
- name: set up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
Upvotes: 10
Reputation: 4994
The bundled JDK into macOS is 1.8, but now we need 11. Make Gradle use AndroidStudio's JDK for building the project:
Into file <PROJECT_ROOT>/gradle.properties
, add line:
org.gradle.java.home=/Users/.../Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/.../Android Studio.app/Contents/jre/Contents/Home
Note: If you're not use Jetbrains Toolbox and you've installed Android Studio into /Application
, use this path instead (thanks to @AndrewBekhtold):
org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/Contents/Home
Upvotes: 11
Reputation: 28845
I got this error when I started gradlew ktlintFormat
in the Terminal of Arctic Fox build.
I opened Project Structure (menu File → Project Structure) and selected Java 11, but it didn't help even after a restart of Android Studio.
Then I changed the Java path in Settings (see the accepted answer), but it didn't help. You should set it.
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"
, and then restart Android Studio)
Press Win+X, press "System", and enter "va".
I pressed Edit button and "Browse Directory...", and then selected JRE (or JDK) folder (without bin
) of Java 11. Then OK, and OK (as always).
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 Setting the JAVA_HOME Variable in Windows, 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 (C:\Progra~1\Android\Androi~1\jre).
Restart Android Studio or reset the computer.
Upvotes: 34
Reputation: 301
If you just modify the JDK version from 1.8 to 11 in Preferences → Build, Execution, Deployment → Build Tools → Gradle, you may still get that error.
By running the command ./gradlew --version
, you can see the JDK version is still 1.8. So, we need to change the JAVA_HOME
value in ~/.bash_profile
to your real JDK-11 location. Here is the diff after modifying:
- export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
+ export JAVA_HOME=/Users/xx/Library/Java/JavaVirtualMachines/corretto-11.0.14.1/Contents/Home
Then, reopen your Android Studio, and everything will work well.
Upvotes: 2
Reputation: 783
I was working on a Flutter application and it was not running on my phone stating that Android Gradle plugin requires Java 11.
My Android Studio version was very old. I was not seeing any of the options shown in the screenshots. I updated Android Studio after setting the JAVA_HOME in system environment variables. And it automatically picked Java 11.
Upvotes: 2
Reputation: 770
On macOS M1 I solved it by the following steps.
Opened file ~/.zshrc in Visual Studio Code
Added this line:
export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/Contents/Home"
Saved the file
Reloaded the terminal with modified file by running
source ~/.zshrc
testing if the changes have been loaded by running
echo $JAVA_HOME
Verified that the correct location is displaying.
Verified that the Gradle is displaying correct Java 11 version by running
cd android
./gradlew --version
And here is the result:
------------------------------------------------------------
Gradle 7.3.3
------------------------------------------------------------
Build time: 2021-12-22 12:37:54 UTC
Revision: 6f556c80f945dc54b50e0be633da6c62dbe8dc71
Kotlin: 1.5.31
Groovy: 3.0.9
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 11.0.12 (JetBrains s.r.o. 11.0.12+0-b1504.28-7817840)
OS: Mac OS X 12.2.1 aarch64
Tried to build it again by running:
yarn android
And it's succeeded.
Upvotes: 42
Reputation: 916
I downloaded the 11 JDK again and it works. This saves you time as you don’t have to clear the cache, build, and rebuild your project.
Upvotes: 31
Reputation: 184
In Windows I got this error when upgrading React Native v0.67 to 0.68 from CMD of the directory without installing update OpenJDK 11.
First I install:
choco source add -n chocolatey -s 'https://chocolatey.org/api/v2/'
Then I run this:
choco install -y nodejs-lts openjdk11 // Without nodejs-lts I have already installed Node.js
Upvotes: 2
Reputation: 699
For the latest version, this works for my Windows machine
Menu File → Project Structure
Choose the JDK 11 from you machine that you installed
Click Apply and OK.
Restart your Android Studio after these changes
Now, you're good to go
Upvotes: 9
Reputation: 865
I solved it by adding this to the gradle.properties file:
org.gradle.java.home=/usr/lib/jvm/java-11-openjdk-amd64
But your Java 11 may be located in a different directory.
Example path for Windows:
org.gradle.java.home=C\:\\Program Files\\Java\\jdk-13.0.2
Upvotes: 6
Reputation: 61
If you run into this problem from the terminal in Android Studio, you should configure JAVA_HOME in your environment into Java 11, set the Gradle JDK in Preferences can't solve. I guess that running Gradle from the terminal and running Gradle from the IDE are not the same process and they use different JDK location.
Upvotes: 4
Reputation: 2945
If you don't want to use JDK 11 and still want to use JDK 1.8, you can revert back the Android plugins as below.
plugins {
id 'com.android.application' version '4.2.0' apply false
id 'com.android.library' version '4.2.0' apply false
}
Upvotes: 4
Reputation: 759
All the solutions here are helpful for Java 11. But if you don't want to upgrade to Java 11 then you should just downgrade Android Gradle plugin (ADP) to 4.X as shown below:
Upvotes: 10
Reputation: 1497
This is how I fixed it on macOS.
./gradlew --version
Go to Preference → Build, Execution, Deployment → Gradle
Change the Gradle JDK to your latest version.
I have only one JDK (Microsoft build OpenJDK 17) installed before installing Android Studio. For unknown reasons, a JDK 1.8 has been installed along Android Studio and set as the Gradle default at /Users/<username>/Library/Java/JavaVirtualMachines/corretto-1.8.0_322
Upvotes: 43
Reputation: 5119
If you are using MS App Center, you may face the same kind of issue when you use Java 11 in your Android project. If you are looking a solution for MS App Center issue. This may help you!
https://appcenter.ms/users/user1/apps/yourapp/build/branches
Build configuration
Environment
and turn on.JAVA_HOME
to $(JAVA_HOME_11_X64)
Upvotes: 16
Reputation: 46
None of the previous answers worked for me in Android Studio 4.2 (Arctic Fox), 2020.3.1 Patch 3.
Only adding org.gradle.java.home=<Path-to-JDK-v11>
to gradle.properties
worked in my case.
If you are using Linux, just add org.gradle.java.home=/opt/android-studio/jre
.
Upvotes: 3