React-Native run-android on specific device - react-native

Is it possible to use the run-android command for one specific device only?
For example, if I have three devices (or emulators) connected and I want to use run-android on only one of them?
Maybe something like adb install -s DEVICE_NUMBER?

To run react-native application on optional device you can specify some flags in run command. To see available add --help:
react-native run-android --help
Then you can specify your device id throught --deviceId
react-native run-android --deviceId=DEVICE_ID
To see available devices ids'
adb devices

To list AVDs:
$ANDROID_HOME/tools/emulator -list-avds
To run a specific emulator:
$ANDROID_HOME/tools/emulator -avd Pixel_API_28_AOSP
To run React Native in the currently-open emulator:
react-native run-android
To choose between multiple running emulators:
adb devices
react-native run-android --deviceId=DEVICE_ID
(Last point from this answer)

Elaborating on #alexander 's answer, you can use the following workflow:
cd android
./gradlew assembleDebug # assembleRelease for release builds
adb install -s <yourdevice> app/build/outputs/apk/yourapk.apk # You can check the identifier for your device with adb devices
adb reverse tcp:8081 tcp:8081 Will forward the phone's 8081 port to the computer's 8081, where the packager listens. If you are deploying over Wi-Fi, have a look at: https://facebook.github.io/react-native/docs/running-on-device-android.html#configure-your-app-to-connect-to-the-local-dev-server-via-wi-fi
In a different screen, run:
npm start # Will run the packager
If you really need this, you might want to wrap the first snippet into a script that you can parametrize with your phone's identifier.

npx react-native run-android --deviceId='myDeviceId'
This works for me. don't forget '' this one. it accepts string

May be we can not select which android device attached to run.
Read from official react native website:
You must have only one device connected at a time.

You don't need to use run-android command to start it on specific device
Firstly, you have to start the packager:
./packager/packager.sh
Then just build an APK file and run it on target device. APK will connect to the build server, and fetch bundle from it automatically.
But if it didn't happen by some reasons, click on reload button :-)

If you're using React >= 0.68.x. Just type this:
npx react-native run-android --deviceId=VCR4XCORPFTKAQFA
VCR4XCORPFTKAQFA is recovered typing adb devices in your terminal.

To run the app on one specific connected android device:
Disconnect/Unplug all connected android devices.
Connect/Plug the device you want to install the app in
Run adb devices on your terminal to see the list connected devices (you should only see 1 such device as rest were disconnected)
Run npx react-native run-android
This will install the React-native app on the connected device via usb.

Actually, first of all, be sure about adb installation which I think while setting up your RN environment you have it for sure, follow these steps:
Connect your Android external device to your computer with a cable
Run this command adb devices and you will see the result at least:
List of devices attached
9999xxx3434yyy device
FYI: the 9999xxx3434yyy is your device id
Put the given device id in the following command
npx react-native run-android --deviceId="9999xxx3434yyy"
or
yarn run react-native run-android --deviceId="9999xxx3434yyy"
HINT: If the device is a Xiaomi product keep watching the phone screen because you should grand install access.

When 2 or more devices are connected
There seem to be no way to install app on a specific device in this scenario. So follow this
Disconnect all devices except one
run the command, in my case its react-native run-android
Now connect another device
repeat 2nd point
when done with every device, connect all and see device ids adb devices
now map device's port 8081 to computer's 8081
Eg:
adb -s DEV_1_ID reverse tcp:8081 tcp:8081
adb -s DEV_2_ID reverse tcp:8081 tcp:8081
adb -s DEV_3_ID reverse tcp:8081 tcp:8081
...

Related

React-Native Issue while running "npx react-native run-android"

while running npx react-native run-android it shows an error
This issue is happening because you are connected with any Android device or any emulator, to connect with your emulator, you need to add a virtual device, to add a virtual device please follow these links https://developer.android.com/studio/run/managing-avds and https://developers.foxitsoftware.com/kb/article/create-an-emulator-for-testing-in-android-studio/
then run adb devices in your root-project in terminal, you must be able to run the app..

Install application on Android phone

I create an application with react-native-cli, how can I install it to my Android phone?
(Although in development mode)
Platform: osX(10.12+)
target deployment: Android Phone (v6.0+)
There is an easy way with Terminal.
in development mode, we run application in android studio emulator or expo or gnymotion with a simple command:
react-native run-android
this time before running this command, run:
adb devices
this command will return all of the devices(include android studio emulator) with an id and name.
first, active: USB debugging option in your android phone
then connect your phone to the computer with USB cable
now run adb devices on the terminal.
note that react-native run-android command will install your application on one device that exists in adb devices list.
so close all other android emulator and then run again adb devices for checking all devices connected to this computer.
now simply run below command and wait for installing it on the device.
react-native run-android
this will install the application on your connected phone.

Failed completely to run the react native app on real device

I have tried most of solutions on the stackoverflow and github issues related to react native but all in vain... for almost 2 weeks.
Environment
react-native-cli: 2.0.1
react-native: 0.56.0
Window 10 Pro
Using android device 5.1(lollilop)
well the app ran successful in expo client but i need to run the native code.
First error: Unable to load script from assets index.android.bundle on windows
so i was able to run command below and the error disappeared on the phone client
react-native run-android && adb reverse tcp:8081 tcp:8081
But this error has persisted on the package server console
Installing APK 'app-debug.apk' on 'BALR_X7 - 5.1' for app:debug
Installed on 1 device.
BUILD SUCCESSFUL
Running C:\Users\goldsoft25\AppData\Local\Android\Sdk;C:\Users\goldsoft25\AppDat a\Local\Android\Sdk\platform-tools/platform-tools/adb -s 7DM7R4KJ99999999 revers e tcp:8081 tcp:8081
Could not run adb reverse: spawnSync C:\Users\goldsoft25\AppData\Local\Android\S dk;C:\Users\goldsoft25\AppData\Local\Android\Sdk\platform-tools/platform-tools/a db ENOENT
Starting the app on 7DM7R4KJ99999999 (C:\Users\goldsoft25\AppData\Local\Android\ Sdk;C:\Users\goldsoft25\AppData\Local\Android\Sdk\platform-tools/platform-tools/ adb -s 7DM7R4KJ99999999 shell am start -n com.awesomeproject/com.awesomeproject. MainActivity)...
2nd error
development server return error response code --> 500 on the android device
Your help is appreciated
I ensure that i downloaded all the needed API android sdk build tools in my case API 28.0.3 and API 28...
then i run the command below.. pay attention to the location of the android sdk tools
goldsoft25#GOLDSOFTX MINGW64 ~/AppData/Local/Android/Sdk/platform-tools
$ adb devices
then later i run the command below in the same command line as below
adb reverse tcp:8081 tcp:8081
Then later i was able to run react-native run-android and it worked perfectly

React native stuck at loading from localhost : 8081 on physical device using USB debugger

When I run react-native run-android it builds successfully and there after
Mobile screen shows nothing more than loading from local host : 8081
I have tried adb reverse tcp:8081 tcp:8081
Start by closing the App on your phone and clearing phone memory.
Close metro bundler on your PC.
Then clear npm cache as follows: npm cache clean --force
Then clean up gradle as follows:
Change into android directory in your project folder: cd android
To clean clean gradle simply run: ./gradlew clean
cd .. back into your project's root directory.
Build your app again using whichever method suits you. My preferred method for RN apps without expo on a windows machine is as follows:
npx react-native run-android
Try either of these ways to fix it:
Try to Open Developer menu by press Ctrl + M in emulator and choose the Setting port, then input the value localhost:8081.
Try to connect the other wifi, then run 'adb reverse tcp:8081 tcp:8081'
Try to change the URL on chrome to http://localhost:8081/debugger-ui/
Cheer!
Samsung DeX!
Samsung DeX runs in the background to discover device connections, it uses port 8081. In my case, it interfered with Metro and adb reverse. Exiting the system tray app fixed the entire issue.
For some reason it was a watchman issue. I remember installing watchman a few days before and it was causing the issue. I uninstalled watchman and it worked.
Uninstalling watchman made it work for me.

Running React-Native Android App on Ubuntu using Genymotion Emulator

I am building an app using react-native and i was using android default emulator without any problems. The only issue i am having is the emulator is really slow.
I wanted to try genymotion and installed it. How ever when i run react-native run-android it cannot find the Genymotion Emulator. Here is the Error i am seeing in the console.
BUILD SUCCESSFUL
Total time: 8.329 secs
This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.4/userguide/gradle_daemon.html
ADB server didn't ACK
* failed to start daemon *
error:
Starting the app (/home/hduser/Android/Sdk//platform-tools/adb shell am start -n com.legacitinative/.MainActivity...
error: no devices/emulators found
I am not sure why it cannot find the emulator.
Appreciate any help.
Thanks
Sateesh
$ cd /home/hduser/Android/Sdk/platform-tools
$ ./adb reverse tcp:8081 tcp:8081
Try if adb can see your device with
$ ./adb devices