Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose which android device (emulator or phone) will react-native run-android run on

I am developing a react-native application and every time I want to update the changes to either an actual phone or an emulator, I execute react-native run-android (Sometimes I use the reload functionality in the emulator).

My question is whether there is the possibility to specify in which device/emulator the command will run, since when I have both connected it will run and update the app in the most recently run/plugged.

So I would imagine something like this:

react-native run-android --device=XXXXXX

I have been doing some research and haven't found an answer so far, so I hope someone has a clue about it. Thanks!

like image 623
vabada Avatar asked Jan 05 '23 11:01

vabada


2 Answers

If you have just one device, either plugged in or running as emulator:

npx react-native run-android // or if you have a npm script, 'npm run android'

The following is useful if you have multiple devices, either plugged in or running as emulators.

Ensure the devices are plugged in, and get their deviceIds with adb devices, then npx react-native run-android --deviceId [DEVICE_ID]

First, plug the device in, or start the emulator with Android Emulator CLI.

  1. emulator -list-avds will list your avds (android virtual devices), e.g. Pixel_2_API_29
  2. emulator -avd Pixel_2_API_29 or emulator @Pixel_2_API_29 will start a specified device.
  3. emulator -help for more info.
  4. Then, open a new terminal/ tab, run either npx react-native run-android. It will start your app on all android devices, both physical and AVDs. If you wanted to specify only 1 device (why not run on all of them!):
  5. adb devices, to get list of device IDs, e.g. 1cfe4i231414523
  6. npx react-native run-android --deviceId 1cfe4i231414523

PS: React native getting started guide specifies that you should uninstall react-native, and instead use the package through npx. To uninstall npm rm -g react-native. Now, everytime you want to run react-native, use npx react-native [command]. What npx does is it looks in the local node_modules and runs it from there, as opposed to a possibly outdated global react-native package.

like image 72
Ben Butterworth Avatar answered Jan 13 '23 10:01

Ben Butterworth


If you go through the docs then you would find that there are two more commands available to start the server and there is no such react-native run-android ---//deviceID command available until now but, you can customize react native code in node_modules to make that command for yourself.

The two commands are:

1)react-native start
2)npm start

Cheers :)

like image 41
Codesingh Avatar answered Jan 13 '23 10:01

Codesingh