45

Full Error --

An exception occurred applying plugin request [id: 'com.android.application']

Failed to apply plugin 'com.android.internal.application'.
Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
Your current JDK is located in C:\Program Files\Java\jdk-11.0.13 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.

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.

Just Created the new project and getting this error.

2
  • 1
    Does this help you?
    – Mirco0
    Commented May 30, 2023 at 8:13
  • Please provide enough code so others can better understand or reproduce the problem.
    – Community Bot
    Commented May 30, 2023 at 14:49

9 Answers 9

57

Step 1: Just Download the Java 17. LINK

Step 2: Set environment variable for Java 17.

Step 3: In Android Studio Go to File → settings → Build, Execution, Deployment → Build Tools → Gradle → Gradle JDK And Select Java 17.

Step 4: Delete the Gradle Cache directory if you still get the error, on Mac it's located at ~/.gradle/caches

BUILD SUCCESSFUL :)

4
  • 8
    Note that there's no need to explicitly install a JDK when you can use the embedded JDK that ships with your Android Studio installation
    – Edric
    Commented May 30, 2023 at 8:56
  • 9
    There is no Gradle JDK option there. Android Studio 2021-2
    – ouai
    Commented Jun 17, 2023 at 19:46
  • I did it with the Embedded JDK as mention in the first comment. Take a look at this answer: stackoverflow.com/a/69064786/2597775. Also check your Gradle and AGP version. Mines are 8.0 and 8.0.2 respectively
    – sebasira
    Commented Aug 23, 2023 at 0:36
  • Steps 14 and 15 from medium.com/@adityandar/… did the trick for me. Specially helpful if you are using an IDE different than Android Studio, i.e., VSCode. Commented Dec 18, 2024 at 11:41
22

If you use MacOS, here is this steps to check:

  1. In your terminal, run npx react-native doctor to check what is issue.

  2. This is issue: enter image description here

  3. Run brew install --cask zulu@17 to install OpenJDK

  4. Run brew info --cask zulu@17 to get path to where cask was installed.

  5. After you install the JDK. JDK will likely be at /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home

  6. Run npx react-native doctor to check again. enter image description here

Read more: https://reactnative.dev/docs/set-up-your-environment?platform=android

2
  • Thanks! This should be marked as the correct answer.
    – Stefan
    Commented Jul 10, 2024 at 7:51
  • Works perfectly. Thanks
    – Ali
    Commented Oct 24, 2024 at 23:50
7

In my case I downloaded gradle JDK version corretto-17 Amazon, and changed Gradle JDK version to corretto and it worked.

Not worked others(ex: jbr-17, embedded JDK Jetbrains 17 and etc.)

5

In my case, I was trying to run to a gradle wrapper command ./gradlew playstorepublish:installDist when I got that error. I followed the steps in the accepted answer but that only got me as far - it didn't fix the error. The next step I tried was to follow the third suggestion provided in error message:

changing org.gradle.java.home in gradle.properties.

I did this by opening the gradle.properties file and adding a new line with:

org.gradle.java.home=C\:\\Program Files\\Android\\Android Studio\\jbr

After doing this, I ran the command again and all was well again.

Or as pointed out in the comment:

org.gradle.java.home=C\:\\Program Files\\Java\\jdk-17
2
  • 2
    org.gradle.java.home=C\:\\Program Files\\Java\\jdk-17
    – Bu Saeed
    Commented May 4, 2024 at 20:10
  • Worth noting that there's also a gradle.propertiesfile in user's home directort (~/.gradle) and in my case it had precedence over the gradle.properties located inside the project. Commented Aug 9, 2024 at 19:37
2

If you're migrating from React Native 0.72 to 0.73, you'll need to update Java from version 17 to 21.
Download Java 21 and after installing it, open the environment variables and update JAVA_HOME to the path, typically located in C:/ProgramFiles/Java/jdk-{your version}.

Next, open Android Studio (I am using the latest version, Hedgehog). Follow these steps:

  • Go to File -> Project Structure.
  • In the mid-left section, you'll find a + icon. Click on it.
  • Select Download SDK and choose the compatible SDK version. You might get it by default else find compatible version and download after it finishes downloading.
  • Click Apply -> OK.
  • Then, open the terminal -> cd android -> Execute ./gradlew --version.
    It might start downloading compatible gradle version if not you'll see gradle version check if the gradle version is compatible to java version you downloaded then
    -> ./gradlew clean if the build is successful, you are ready to run app else if same error persists, run ./gradlew --stop run this twice -> rm -rf ~/.gradle/caches/* -> and try ./gradlew clean again.
1
  • Read this and installed Java 21 due to this answer, which gave me compilation errors. I should have just checked the docs (reactnative.dev/docs/environment-setup) where they recommend a JDK version. Check the docs for the latest. Installed the recommended version, compiled fine with no other changes.
    – tdous
    Commented Apr 8, 2024 at 19:16
0

change jdk path in Environment Variables.

First Download jdk 17.0.10 JDK Download

  1. Type here to search-> Edit the system environment variables
  2. choose Advanced option -> Click on Environment Variables.
  3. In User Variables for **** -> Double click on JAVA_HOME.
  4. opened Edit User Variable.
  5. change variable value -> set path version 17.0.10(Downloaded latest jdk version).
  6. Then click ok and close all files and restart your computer.
  7. Finally Its work.
0

open your gradle.properties file and add the following line:

org.gradle.java.home={path to the JDK17 home directory}

e.g.
org.gradle.java.home=D:/Applications/Java/jdk-17.0.10

0

Ubuntu:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt install openjdk-17-jdk-headless

The last of the 3 commands shall upgrade and automatically switch to version 17 via update-alternatives. Now run java -version and you should see:

openjdk version "17.0.12" 2024-07-16
OpenJDK Runtime Environment (build 17.0.12+7-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 17.0.12+7-Ubuntu-1ubuntu222.04, mixed mode, sharing)

Re-run your build and everything would run perfect.

0

For people encountering this issue with a React Native app on Windows:

The new environment variables setup instructions no longer require a JAVA_HOME variable. It originally pointed to the Android Studio jre.

If you follow the instructions from @noman-baig AND remove the JAVA_HOME entry in your environment variables the issue should be fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.