110

I'm working on MacOS and just starting with react-native.

One of the first steps to get started is to run: react-native run-android or react-native run-ios. But I'm getting this error:

react-native run-android

Result:

Scanning folders for symlinks in /Users/ric/myprojs/albums/node_modules (6ms)
Starting JS server...
Building and installing the app on the device (cd android && ./gradlew installDebug)...

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '9.0.1'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/android-setup.html

This is what I get when I run java --version:

java --version

Result:

java 9.0.1
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
mbp:albums ric$ java --version
java 9.0.1
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

Edit

My javac version:

javac --version

Result:

javac 9.0.1

Any idea on how to fix this?

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Xar
  • 7,572
  • 19
  • 56
  • 80

11 Answers11

209

In your PROJECT_PATH/android/gradle/wrapper/gradle-wrapper.properties file, update your distributionUrl to the following:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip

See related gradle distributions documentation

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Muhammad Adeel
  • 2,877
  • 1
  • 22
  • 18
  • Where can I find that `gradle.wrapper.properties` file? Thanks – Xar Oct 21 '17 at 20:15
  • 4
    should be in **app-folder/android/gradle/wrapper/gradle-wrapper.properties** – Muhammad Adeel Oct 21 '17 at 20:17
  • Thank you! That fixed my problem. For some reason I had the older version `gradle-2.14.1-all.zip` – Xar Oct 21 '17 at 20:40
  • 6
    After I changed the gradle version to your suggestion it gives me another error: Could not initialize class com.android.sdklib.repository.AndroidSdkHandler – leMale Oct 22 '17 at 20:45
  • 8
    Why does this help though? React-Native uses gradle-2.14.1 by default when you init a new application, so why do we need a different one? – Waltari Oct 23 '17 at 07:39
  • 11
    getting this error after adding this > javax/xml/bind/annotation/XmlSchema – Milon Dec 19 '17 at 13:37
  • @DinIslamMilon - see this answer for an in-depth discussion of what you're seeing: https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j/43574427#43574427. I had it go away when I either 1. switched my default system JDK to 8 or earlier OR 2. upgrade to gradle 4.9 – Del Aug 23 '18 at 03:30
  • > Update your distributionUrl to the following I had already told the wrapper to upgrade itself, so the URL was already set. react-native is still failing when invoking ./gradlew commands inside my app folder. – Del Aug 23 '18 at 03:31
  • 15
    You need to use latest version of Gradle. For example, to support Java 11, you can use `gradle-4.10-all.zip` – Andrey Kolpakov Oct 16 '18 at 13:26
  • I then had to update my build.gradle as detailed in https://stackoverflow.com/questions/53709282/cannot-add-task-wrapper-as-a-task-with-that-name-already-exists – oliversisson Apr 17 '19 at 09:52
25

Really the fastest way to get around this error is to use JDK 8. Except when you really need to use JDK9.

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Don't forget to change your enviroment variable JAVA_HOME to the new JDK version!

Xen_mar
  • 8,330
  • 11
  • 51
  • 74
  • 1
    related link to switch java version https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-os-x – Akshay Vijay Jain Oct 13 '18 at 14:49
16

Make sure that you have JDK8 installed and then set JAVA_HOME into JDK8

for example:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home

or use .bash_* files to set this variable

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
bobu
  • 908
  • 8
  • 14
  • 1
    I had this in my .zshrc on mac: `export JAVA_HOME=$(/usr/libexec/java_home)` Changing it to `export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)` picks the latest installed version of Java v8, and fixed this same issue. – Greg Haygood Aug 03 '18 at 02:23
8
vi ~/.bash_profile

Then add the following lines:

export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

Now source the file to make the above changes effective in the current shell:

source ~/.bash_profile 
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
sotheng ski
  • 81
  • 2
  • 3
7

On MacOS, I ran the following commands:

brew cask uninstall java
brew tap caskroom/versions
brew cask install java8 "or just brew cask install java to install latest version"
Andres Felipe
  • 4,292
  • 1
  • 24
  • 41
3

I was getting this error, but for a more recent version of Java - 10.0.2.

To fix my error, I followed the steps mentioned by @Blacktoviche in this Github issue: https://github.com/facebook/react-native/issues/17688

When I opened the android folder inside my project via Android Studio, I was prompted with Android Gradle plugin update recommended. At the time, I was following a slightly outdated tutorial that told me to click Don't remind me again for this project.

However, as @Blacktoviche recommended in the github issue linked above, sometimes it's necessary to update the Gradle plugin. After updating, I also had to click Install Build Tools 27... and sync project (I don't remember the exact version).

After I followed these steps, I was able to successfully run my react native app on the Android Studio emulator.

Hope this helps someone!

Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
1

---- December 2018 --- keep attention --- React Native does not work with Java versions higher than 8, so you need to uninstall your 9.0.1 version. To do so, execute the following commands:

cd /Library/Java/JavaVirtualMachines/
ls -l

Identify the folder containing your Java Version, then remove it

sudo rm -rf jdk.x.x_xxx.jdk

After that you can download and install java 8 from oracle again. Refer to jdk8 download page

Guilherme Ferreira
  • 2,209
  • 21
  • 23
  • "React Native does not work with Java versions higher than 8" absolutely incorrect. I have a build running off JDK 13 right now. – Ian Kemp Feb 07 '20 at 12:08
  • My answer is from DEC 2018. Get React Native from 2018, and then run on a JDK 13 and then if it works come here and update your answer. – Guilherme Ferreira Feb 07 '20 at 13:11
0

On Ubuntu, you should run these command lines in the terminal:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt install oracle-java8-installer
sudo apt install oracle-java8-set-default
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
0

If you are using windows; Make sure you add your jdk path to the Environment path variable. Then open your android folder from your react native project into android studio. It will update necessary things for you automatically.

Shaon
  • 2,496
  • 26
  • 27
0

I usually get this error when I forget to build the project using Android Studio. A simple sync and upgrading Gradle will do the work.

Nick Rameau
  • 1,258
  • 14
  • 23
0

I solve this by downgrading my JDK to 8.

Gurzoni
  • 99
  • 7