erv
erv

Reputation: 603

How to run react-native app on Android emulator (or a physical device) in Android Studio?

I'm trying to run my react native app on either an Android Studio emulator or my physical device. I've followed the instructions at https://reactnative.dev/docs/environment-setup under the React Native CLI Quickstart tab, and initialised the 'AwesomeProject' app from that page, but I'm finding the instructions unclear. For instance it says do the following on the command line: npx react-native start which starts up a process of some sort. Then it says to enter: npx react-native run-android, but the command line is blocked by the previous process. If I try the latter either in another command prompt or without first running the former command, I get an error Unrecognized command "run-android". I should say I have an emulator running from Android Studio before doing all this.

So failing getting it running via the react-native CLI, how do I get it running from within just Android Studio? I've never used Android Studio before, so I have no idea what the process is. Googling hasn't helped either. But as I said, I have managed to get an emulator installed and running.

Any help getting this going either via react-native CLI or from within Android Studio would be greatly appreciated.

edit: I've found how to run my app in Studio, but it pops up an 'Edit configuration' dialogue and says there's no Module selected. But in the module dropdown box there's nothing to select.

Upvotes: 0

Views: 10328

Answers (1)

Raghvender Kataria
Raghvender Kataria

Reputation: 1485

You can try following the below steps:

Make sure that you have 2 environment variables set in your system.

  1. ADB variable which is set to the path of "platform-tools" in your system like below:

Global variable ADB

  1. ANDROID_HOME variable set to SDK path, where android was installed.

ANDROID_HOME variable

Now execute below commands in command prompt and start you emulator or connect your phone which has development settings ON before this:

  1. Open your command prompt in admin mode.
  2. Delete node_modules folder
  3. Execute: npm install
  4. Execute: npm install -g react-native && npm install -g @react-native-community/cli
  5. Execute: npx react-native init
  6. Go to android folder in your react-native project and execute: npx gradlew clean
  7. Come back to main folder and execute: adb reverse tcp:8083 tcp:8083. (You need to check the status of your connected devices by executing adb devices. It will show you the connected devices.)
  8. You also need to make sure that your index.bundle is created. If not than you may create it by executing: npx react-native bundle --platform android --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --dev false --reset-cache --assets-dest android/app/src/main/res/
  9. Finally execute: npx react-native run-android --port=8083

Upvotes: 3

Related Questions