React Native Android : FAILURE: Build failed with an exception. MAC OS
- What went wrong:
Execution failed for task ‘:app:compileDebugJavaWithJavac’.
> Task :app:compileDebugJavaWithJavac FAILED
This is an issue with how Gradle is automating the build and the current version of the JDK installed on your machine, follow these steps to fix it, the react native documentation states you need adoptopenjdk8, however you may have other conflicting JDK versions in the same directory.
Firstly: Recreate this error
In order to recreate this Error install a newer version of JAVA Development Kit for example JDK 16, or more than one JDK’s on your mac.
If you run npx react-native run-android, You’ll get the following build fail:
Your build may be failing because :
- More than one JDK’s are installed.
- Incompatible JDK is installed.
- Or no JDK is installed.
This is an issue with how Gradle is automating the build and the current version of the JDK installed on your machine, the react native documentation states we need adoptopenjdk8, however you may have other conflicting JDK versions in the same directory.
So to fix this issue follow these steps:
Step One:
Open up your terminal and navigate to /Library/Java/JavaVirtualMachines by typing the following command:
cd /Library/Java/JavaVirtualMachines
after you have done that type “ls” to see what files are included and take note of the versions, incase you have one or multiple JDK files present like such:
delete them with the following command line :
the only dependency you need is adoptopenjdk-8.jdk as mentioned in the react native docs
however delete that as-well and reinstall it after you have completed these steps.
After you have done so you can check if the files still exist by typing `ls` if the files are deleted.
Then run the following commands to insure all other links, plugins and files are deleted as-well from your Library:
Step Two:
Once all JDK’s have been removed reinstall the correct JDK as mention in the react-native documentations.
Open up a new terminal
https://reactnative.dev/docs/_getting-started-macos-android
This may say adoptopenjdk-8.jdk is already installed:
but go ahead and reinstall it with:
Once installation is complete, you should see the following:
At this point the error should be resolved and your build should work.
It is important to note that this is a build error, since react native uses Gradle to build automations, the conflicting of multiple jdk versions can cause your build to fail.
please refer to the following documentation to get a better understanding on Gradle compilation.
*https://docs.gradle.org/current/userguide/userguide.html*