Install application on Android phone - react-native

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.

Related

React Native Android App run on emulator doesn't connect to metro bundler when installed using android-studio

My React-Native(0.68.1) project runs properly on my emulator when using npx react-native run-android command and everything works fine with the metro bundler instance created by this command.
Only when I run project from android studio (2021.3.1 Patch 1) , app is build successfully and installed on emulator, but cannot connect to metro bundler (which I separately run using npx react-native start command).
I wanted to try and update the debug server & port as mentioned in other similar questions , but I cannot access the debug app menu even after dismissing the error (menu doesn't show when ipress the shortcut)
Issue also occurs on build installed on emulator via npx react-native run-android when I close the app and metro bundler created by the command and start a separate metro bundler. In this case but I am able to enter settings menu and enter "debug server and port " to 10.0.2.2:8081 and app connects to metro.
I am using mac mini m1.
To fix this issue
first of all, start your emulator and run adb reverse tcp:8081 tcp:8081 this command on the terminal and then run npx react-native start and then run your project in the android studio and it will automatically link your app to that bundle

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..

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

Could not connect to development server on android emulator and on real device

When I run "react-native run-android",it gives me error:"could not connect to development server...".-- The red screen
OS: Windows 7
node version:8.2.1
npm version:4.1.2
react-native version:0.47.1
react-native-cli version:2.0.1
android device and emulator version: 5.1.1
i followed following command:
react-native init ProjectName
cd ProjectName
react-native run-android
And this makes the package server run automatically.
But I am not able to access the package server from browser on the machine and the mobile.
My android device connected to computer has debugging enabled i checked it using adb devices command.
Usb debug is on.
I tried running the project on real device and on emulator. On Both I got error: "Unable to load script from assets 'index.android.bundle' Make sure your bundle is packaged correctly or you're running a package server'.
reloading i get the error: "Could not connect to development server."
Then i set the host and port number on development setting on both and got the error "Could not connect to development server."
enter image description here
So how to fix the red error screen issue?Any suggestion is appreciated!
Try to open this link in browser. It will automatically bundle the assets.
http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false
You development server should be running while running this command.

React-Native run-android on specific device

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
...