A mobile development framework called React Native allows developers to create cross-platform applications that work and feel completely native on both iOS and Android. You have the option to test your app using React Native on a physical device or an emulator.
Without having to own a real device, you may test your app using an emulator. Numerous virtual devices can be imitated by using the Android Virtual Device. For instance, you can install an emulator based on a device like the Google Pixel and utilise the Android operating system of your choice.
React Native also has you covered if you prefer to test your app on an actual Android smartphone that you own. Connecting your Android device to the PC you use for development and issuing a command can complete the process. On Android, React Native apps must first be set up in order to run.
The following are required in order to run a React Native app on Android:
- Android Studio
- Android SDK
- JDK 8
The official IDE for developing Android applications is called Android Studio. You can download it for nothing from the official website. MacOS, Windows, and Linux all support Android Studio.
Download the Android Studio setup file from this page to install Android Studio. The installation procedure can then be finished by launching the executable and following the on-screen instructions.
Start Android Studio as soon as the installation procedure is complete. Android Studio will ask you to install additional files when you launch it for the first time. The Android SDK, platform tools, and emulator are among the extra files. Ensure that your computer is online, then use the setup wizard to install any required dependencies. Depending on the speed of your internet connection, the process can take some time.
Setting up JDK 8
If you don’t already have JDK 8 installed, you’ll need to do so, as was stated before in the prerequisites. For users of Windows and macOS, the following links will take you to the official download pages and installation guides:
JDK can be installed in a variety of methods, such as on macOS via Homebrew. The above-linked instructions, however, call for utilising an executable installer. JDK 8 can be installed using whatever way you like.
Open a new terminal window or command prompt if you’re on a Windows PC and run the following command:
npx react-native init helloReactNative
After you hit the enter key, wait for the new project setup to complete. Once created, you’ll find a new folder in your current directory. The name of the new folder is the same as the name of your project. Run the following commands to navigate to the project folder:
TIP: Replace helloReactNative with the actual name of your project. That is, in a case where your project has a different name.
In this section, we’ll be running our project on an actual Android device. Before we do that, let’s make sure the device and your computer are able to communicate properly.
USB debugging makes it possible for the Android Debug Bridge (ADB) to push APKs to your phone.
To enable USB debugging, you’ll need to unlock the Developer Options first. To do that, go to Settings on your Android device and navigate to the About Device section. In the About Device section, tap on the Build Number 10 times or until you see the message “You are now a developer.”
With the Developer Options enabled, you should find a Developer Options item on the settings screen or under Advanced/System Settings. From there, turn on USB debugging.
Running the App
Connect your phone to the computer using a USB cable. After that, go back to the terminal (make sure your project directory is set as the current directory), then run the following command:
npx react-native run-android
On a successful build (when no error occurs), the app will be installed on your Android device. The build process might take a few seconds or minutes, depending on the strength of your computer.
TIP: A prompt might appear on your phone requesting permission for ADB the first time you run the above command. Please accept the prompt to grant ADB access.