96

I am trying to get React-Native to work with Android V4.2.2 (Genymotion) but I am unable to test the app on the Emulator. When I ran react-native run-android, I get this error Could not run adb reverse: spawnSync

Here is a log

JS server already running.
Running ~/Library/Android/sdk/platform-tools/adb reverse tcp:8081 tcp:8081
Could not run adb reverse: spawnSync ~/Library/Android/sdk/platform-tools/adb ENOENT
Building and installing the app on the device (cd android && ./gradlew installDebug...

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> The SDK directory '~/Library/Android/sdk' does not exist.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.785 secs
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

NOTE: In the log it saids SDK directory does not exist, I have double check that I do have the SDK installed in that directory.

I found my android emulator when executing adb devices

List of devices attached
192.168.56.101:5555 device

I have tried the following steps from Stack Overflow post, but still no luck https://stackoverflow.com/a/38536290/4540216

XPLOT1ON
  • 2,994
  • 2
  • 20
  • 36

15 Answers15

268

I got the same issue. I updated my ANDROID_HOME env variable again it worked for me.

Follow this React-native android-setup documentation

ex:

export ANDROID_HOME=~/Library/Android/sdk

Windows:

set ANDROID_HOME=c:/Users/whoever/AppData/Local/Android/Sdk

macOS Mojave and earlier or bash users:

1 - Open your bash profile:

open .bash_profile

Add this to your bash_profile:

 export ANDROID_SDK=/Users/<your_computer_name>/Library/Android/sdk
 export PATH=/Users/<your_computer_name>/Library/Android/sdk/platform-tools:$PATH

Save and close

Compile your changes

source ~/.bash_profile

For macOS Catalina and zsh users:

Starting with macOS Catalina, your Mac uses zsh as the default login shell and interactive shell. You can make zsh the default in earlier versions of macOS as well.

On your Mac:

Open your .zshrc file:

open ~/.zshrc

If .zshrc file not exist, you need to create one using touch & open.

touch ~/.zshrc

Add this to your .zshrc file

export ANDROID_SDK=/Users/<your_computer_name>/Library/Android/sdk
export PATH=/Users/<your_computer_name>/Library/Android/sdk/platform-tools:$PATH

Save and close

Compile your changes

source ~/.zshrc

Edit: Updated answer for macOS Catalina and zsh users.

SureshCS50
  • 3,608
  • 1
  • 20
  • 27
  • 1
    I have followed the steps in the link it does not work as well as the command you posted (I restarted the shell) – XPLOT1ON Aug 09 '16 at 10:22
  • The error shows that the sdk location is wrong. Can you share your sdk location here. coz, I got the same issue you got and am new to js itself. I solved it by updating my env variable and re-run that project – SureshCS50 Aug 09 '16 at 10:49
  • Inside /Users/Pan/Library/Android/sdk (https://www.dropbox.com/s/pediyswug2h6m8r/Screenshot%202016-08-09%2018.08.31.png?dl=1) – XPLOT1ON Aug 09 '16 at 11:10
  • Please check the following screenshot [https://www.dropbox.com/s/q2tfw3v43qkw9q3/Screen%20Shot%202016-08-09%20at%204.48.42%20pm.png?dl=0](https://www.dropbox.com/s/q2tfw3v43qkw9q3/Screen%20Shot%202016-08-09%20at%204.48.42%20pm.png?dl=0) and am also using genymotion for learning react native android – SureshCS50 Aug 09 '16 at 11:23
  • Hmm it somehow works but do I have to put the command in every time I restart prompt console? I have the exact same command in ~/.bash_profile – XPLOT1ON Aug 09 '16 at 11:56
  • Ya.. I think so, For the first time you run app, we need to update our ANDROID_HOME env variable after that you don't need to refresh your env variable. – SureshCS50 Aug 09 '16 at 12:00
  • 1
    This was never a problem for me because Android Studio does the work under the hood, so making the Android Home available in the bash config fix it – cutiko Jun 15 '19 at 20:08
  • Where do I run/add the command above? How do I get to the bash profile? – Euridice01 Nov 22 '19 at 16:57
  • @Euridice01 open your project location in terminal and paste the above command and your bash_profile will be in your home.. ~/.bash_profile – SureshCS50 Nov 25 '19 at 08:28
  • 1
    In windows you can directly set ANDROID_HOME path in environment variables – Abhi Oct 06 '20 at 17:45
  • Remember to save .zshrc, source and close and open terminal window where you run npx react-native run-android – Patrik Rikama-Hinnenberg Jan 05 '21 at 16:53
67

Starting with macOS Catalina, your Mac uses zsh as the default login shell and interactive shell. You can make zsh the default in earlier versions of macOS as well. More details on zsh from Apple

So on your Mac:

1 - Open your .zshrc file:

open ~/.zshrc

2 - if .zshrc file doesn't exist, you need to create one & open again(Step 1)

touch ~/.zshrc

3 - Add this to your .zshrc file(Add JAVA_HOME to use Android Studio Embedded JDK)

UPDATE: The new Android Studio Electric Eel is using jbr, not jre.

[New code] From Android Studio Electric Eel

export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home
export ANDROID_HOME=/Users/<your_computer_name_here>/Library/Android/sdk
export PATH=$ANDROID_HOME/emulator:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH

[Old code] Before Android Studio Electric Eel

export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home
export ANDROID_HOME=/Users/<your_computer_name_here>/Library/Android/sdk
export PATH=$ANDROID_HOME/emulator:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH

4 - Save and close

5 - Compile your changes

source ~/.zshrc

& make sure to restart your terminal.

AbhinayMe
  • 2,399
  • 1
  • 20
  • 21
20

I also got the same issue. And I updated my ANDROID_HOME env variable again in same cmd and it was worked fine.

> export ANDROID_HOME=~/Android/Sdk 
> export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

good luck

XPLOT1ON
  • 2,994
  • 2
  • 20
  • 36
Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44
16

This answer is for MacOs Catalina or above user or zsh users as your Mac now uses zsh as the default login shell and interactive shell.

This is related to path issues.

If you follow along with the docs of React Native Setting up the development environment guide. Then do the following.

  1. Open ~/.zshrc using editor. In my case I use vim
vim ~/.zshrc
  1. Add the following line for the path.
export ANDROID_HOME="/Users/<yourcomputername>/Library/Android/sdk"
export PATH=$ANDROID_HOME/emulator:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH

Make sure to add the above line correctly else it will give you a weird error.

  1. Save the changes and close the editor.

  2. Finally, now compile your changes

source ~/.zshrc

I get this working in my case. I hope this helps you.

Pratap Sharma
  • 2,633
  • 2
  • 18
  • 32
15

this was my solutions operating system: Linux mint

sudo apt-get install android-tools-adb
frangel02
  • 149
  • 1
  • 4
12

I solved this error with installing adb

On Linux

sudo apt-get install android-tools-adb
Rahmad Al Habib
  • 300
  • 4
  • 7
8

First I have setup the path in .bash_profile like this

export PATH="~/Library/Android/sdk/platform-tools":$PATH
export ANDROID_HOME="~/Library/Android/sdk/platform-tools"

But it does not resolve my problem.

For me by adding following npm script in package.json under script tag worked like charm on Mac.

"android-dev": "adb reverse tcp:8081 tcp:8081 && react-native run-android"

Then I am simply running npm run android-dev and it's all set. Make sure that in your app setting Live reload is enabled already, in this way I can worked on development server on my mobile and see the coding changes immediately in app.

Mohsin Bagwan
  • 333
  • 4
  • 9
8

I was on Linux and in my case the ANDROID_HOME env variable was already added to the .zshrc but the path was incorrect, correcting the path fixed it.

export ANDROID_SDK_ROOT=$HOME/Android/Sdk
export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/emulator:$PATH
export PATH=$PATH:$ANDROID_HOME/tools:$PATH
export PATH=$PATH:$ANDROID_HOME/tools/bin:$PATH
export PATH=$PATH:$ANDROID_HOME/platform-tools:$PATH

add these in the ~/.zshrc file and reload your terminal.

Hamza Maqsood
  • 355
  • 2
  • 11
3

This worked for me,

On Linux

sudo apt-get install android-tools-adb
3

If none of the solution works. Try running source ~/.bash_profile&&react-native run-android

Update this in the package.json file under scripts for android as "android": "source ~/.bash_profile&&react-native run-android"

For MacOS version Catalina and above: "android": "source ~/.zshrc&&react-native run-android"

Ashok
  • 499
  • 4
  • 13
2

For Mac only

If the accepted answer doesn't work for you then check if you have "adb" installed on your system. If not, install adb using homebrew.

Wakas Abbasid
  • 372
  • 2
  • 11
0
  1. touch ~/.zshrc
  2. open ~/.zshrc
  3. add export ANDROID_SDK=/Users/<your_computer_name>/Library/Android/sdk export PATH=/Users/<your_computer_name>/Library/Android/sdk/platform-tools:$PATH
  4. save
  5. source ~/.zshrc
  6. run android
ATEC HUO
  • 3
  • 2
0

I have this issue with React Native in MacOS Monterrey and I solved following the next steps https://github.com/facebook/react-native/issues/28712#issuecomment-617384353

  1. Open a terminal
  2. Execute touch ~/.bash_profile to create a file
  3. Execute open ~/.bash_profile or vim ~/.bash_profile to edit the file
  4. Add the content
export ANDROID_HOME=/Users/yourusername/Library/Android/sdk
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
  1. Execute source ~/.bash_profile If it doesn't work, then

  2. Change "android": "react-native run-android" to "android": "source ~/.bash_profile&&react-native run-android" in your package.json file, and run yarn android

Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47
0

Mac OS Moneterey or any mac version

  1. nano ~/.zshrc
  2. export ANDROID_SDK_ROOT=/Users//Library/Android/sdk
  3. source ~/.zshrc
  4. echo $ANDROID_SDK_ROOT
  5. adb version
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 07 '23 at 14:14
-1

In my situation, I'm on Pop OS(Ubuntu) and I have android-studio installed, so I just

ln -s ~/Android/Sdk/platform-tools/adb /usr/bin/adb 
ding van
  • 315
  • 2
  • 4
  • 14