44

I'm using react-native 0.43.3 on OSX. I tried to running app on Android and get message

react-native run-android

Scanning 568 folders for symlinks in /Users/ruci.k/project/mayacrew/supermembers/supermembers/node_modules (5ms)
JS server already running.
Building and installing the app on the device (cd android && ./gradlew installDebug)...
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

./gradlew installDebug gave me some errors and fixed it all. Finally build get successed and app is working on Android device.

But react-native run-android command still not work. Only ./gradlew installDebug command work.

Is there anything can I check to use react-native run-android command?

I can't understand how could it happened.

haru
  • 541
  • 1
  • 4
  • 6

22 Answers22

38

Running

export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"

worked for me

  • It works for me too! Thanks for your help! – TDAK Mar 15 '21 at 06:41
  • 18
    Giving ERROR ERROR: JAVA_HOME is set to an invalid directory: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home – Shubham Mishra Apr 17 '21 at 16:24
  • Finally! Thanks – Pablo May 19 '21 at 16:55
  • 1
    Amazing solution, after hours of research. but can you explain abit what it does. I literally copy pasted without knowing what it does haha – Mir Stephen Aug 06 '21 at 18:59
  • 3
    For some reason it seams that the JAVA_HOME path gets set to an incorrect value. This solution simply sets it to the correct one. FYI it might not the same path for everyone!! It's a good idea to write, for example `export JAVA_HOME="/Applications/Android` And then press tab a couple of times, selecting the correct path until you get to `...Contents/Home` . Also a good idea to include this in the .zshrc file (or whatever you're using) – Luis Felipe Zamora Oct 28 '21 at 18:06
  • 1
    wow thanks for all the context on why that worked for you – B-Tron of the Autobots Jul 30 '22 at 02:23
  • Try doing `export JAVA_HOME="/usr/libexec/java_home"` instead to fix the errors @ShubhamMishra is getting – Brandalf Nov 18 '22 at 05:05
  • This actually gives me a different error now, same as ShubhamMishra and Brandalf's does not fix. – PhilCowan Dec 06 '22 at 20:58
30

Run the following command

chmod 755 android/gradlew

inside your app root folder then run

react-native run-android

gypsicoder
  • 5,416
  • 5
  • 24
  • 38
Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
21

After you create your react native app ,try

sudo chmod -R 777 node-modules
sudo chmod -R 777 android 
export ANDROID_HOME=~/Library/Android/sdk

now run

react-native run-android 
WardNsour
  • 313
  • 1
  • 2
  • 16
Guru
  • 922
  • 9
  • 12
16

Try removing android,ios folders and then do react-native upgrade then try again react-native run-android After that if you get an error like

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

then create a file named local.properties inside the folder

android

and insert the following line sdk.dir =/path/to/Android/Sdk After this everything should be fine

Note: If you have made some changes in android/ios folder please keep its backup before removing the folders

Jose Kj
  • 2,912
  • 2
  • 28
  • 40
  • 3
    removing the folders and running the above commands simply throws an error that the android folder is now missing. This https://stackoverflow.com/a/61913747/1103592 solved it for me – Sergey Katranuk Nov 12 '20 at 20:01
11
cd android && sudo ./gradlew clean

This worked for me. Try it out and let me know if it helps folks

4

I have faced the same issue when I have imported the project from the GitHub.

Follow the below steps to run your react-native project in the Android device from the MAC OS

  1. Remove the 'node_modules' folder from the root project.
  2. Run the below command in the project root folder.

npm install

  1. Move to the android folder in the project root folder.

    cd android

  2. Run the following commands one after another to give permissions to gradlew file.

chmod +x ./gradlew

xattr -l ./gradlew

xattr -d com.apple.quarantine ./gradlew

  1. Now, come back to project root folder and run the project in android device.

react-native run-android

Now you can see the launched application on your connected Android device/emulator.

Vinay Bagale
  • 2,361
  • 4
  • 36
  • 45
Sridhar Nalam
  • 527
  • 8
  • 9
4

In Ubuntu, I have used this command and it worked for me:

    sudo react-native run-android
Hadi Samadzad
  • 1,480
  • 2
  • 13
  • 22
3

I had the same issue, and just opened the project in android studio, and that's all. Solved!

  • This works! But this solution is better https://stackoverflow.com/a/66567300/4820421 – TDAK Mar 15 '21 at 06:42
3

this issue come from two reasons

1- Permission issue

How to Fix it

sudo react-native run-android

Or

chmod 755 android/gradlew

2- Mismatch java version with Gradle

make sure you are using Java 8 as $JAVA_HOME environment variable

depending on which terminal you are using to run your code at

For zsh shell, export $JAVA_HOME at ~/.zshenv or ~/.zshrc.
For bash shell, export $JAVA_HOME at ~/.bash_profile or ~/.bashrc.

get installed JDK by this command

/usr/libexec/java_home -V

example for using zsh

vi  ~/.zshrc
// paste the java export inside it 
export JAVA_HOME="JDK_8_Path_from_previous command"

to save your change

:x

make sure your changes applied

source ~/.zshrc

now for testing

echo $JAVA_HOME
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
2

You must change your build.gradle:

compileSdkVersion 28  
buildToolsVersion "28.0.2"  
minSdkVersion 16  
targetSdkVersion 27  

and add this line:

implementation "com.android.support:appcompat-v7:28.0.0"
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
sia
  • 513
  • 4
  • 14
  • Where to add `implementation "com.android.support:appcompat-v7:28.0.0"` – Abdulla Mar 05 '19 at 11:51
  • 1
    Saularis, I know it's too late but may be it will help to someone, put this line `implementation "com.android.support:appcompat-v7:28.0.0"` in `android/app/build.gradle` – Riddhi Jun 30 '21 at 07:31
2

Check whether your emulator is connected or not:

  1. set ANDROID_HOME path: E:\android-sdk-windows

  2. set path D:C:\Program Files\nodejs;C:\Program Files\Java\jdk1.8.0_111\bin;E:\android-sdk-windows\platform-tools

1
  1. go to folder android in terminal
  2. gradlew clean
  3. run react-native run-android
0

maybe install watchman?

https://github.com/facebook/react-native/issues/10056

Otherwise, for me I actually use 'npm start' inside of the folder. npm will know to run the right command to start react native because it's in a react native directory.

LemonPie
  • 806
  • 1
  • 11
  • 23
  • And as your answer, I try to use 'npm start' and it results same with 'react-native start'. Starting JS server working well, just 'react-native run-android' not worked. – haru Apr 28 '17 at 03:37
0

So what i did was to download this jar file from: 1: https://drive.google.com/file/d/1MHSJx-_0RHRzlrXBLAePKeWKQteke0Um/view?usp=sharing and placed it inside android/gradle/wrapper then i went back to the project directory and did npm install i then changed directory to android by doing: cd android then i ran the following commands

chmod +x ./gradlew

xattr -l ./gradlew

Now i went back to the project directory and ran react-native run-android

0
  1. Go to C:\Users\ [username]
  2. Rename or delete ".gradle"
  3. Then run "react-native run-android"
hexhad
  • 1,139
  • 12
  • 14
0

So here are the three solution for this bug , from which one will most probably solve your issue :

1: Adding local.properties file in your android folder

https://stackoverflow.com/questions/32634352/react-native-android-build-failed-sdk-location-not-found

2: Try deleting .gradle file and then build (react-native run-android)

3: Might be possible that under SDK tools NDK(side by side) and CMake are not installed . Steps for the same is Android Studio which include direction for opening sdk manager

Open android folder in Android Studio Click on the SDK Manager (which is at the third position from the top left )

After opening SDK manager you have to go to SDK Tools Installation image for NDK and CMake

Under SDK Tools select NDK(side by side) and CMake and press the okay button and let the installation complete . After the close the Android Studio and react-native run-android.

0

1 . you should have space in your phone
2 . developer option on
3 . USB debugging option also on
4 . check "adb devices" devices is connected or not

Wang Liang
  • 4,244
  • 6
  • 22
  • 45
0

I have to perform these steps to get it running.

  • First install dos2unix
  • Run dos2unix ./gradlew inside android folder
  • then run ./gradlew clean
  • at last run react-native run-android.
Ali Yar Khan
  • 1,231
  • 2
  • 11
  • 33
0

For me, this had to do with running low on RAM om my Mac.

in gradle.properties put -XX:MaxPermSize=4096

for example like this:

org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m

After that, it runs again.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 19 '22 at 14:59
0

What worked for me is,

Uninstalling the previous installed app version on the emulator device, and running react-native run-android after.

0

Check the error messages carefully. It will give a clue to what went wrong. In my case, after installing expo in my react-native project, the MainApplication.java got corrupted. I made a comparison with the a backup (you can check an earlier working commit), then removed the erroring lines.

After this, it worked.

Okpo
  • 375
  • 4
  • 9
0

as for me working on apple m1 I had to change the path to the following:

export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/Contents/Home"

just a change in the path in case it didn't work with

export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"