Getting started with Native App Development with React Native
A quick tutorial on setting up and building first React Native App project on Ubuntu
If you are a JavaScript developer and want to develop native apps for mobile and other native devices like wearables, TV, etc., you have React Native that can help you build iOS and Android apps using code that you can write once. If you are already familiar with mobile development, you may want to use React Native CLI. It requires Xcode or Android Studio to get started. I had already installed Android Studio on an Ubuntu machine. [tutorial]. So, I wanted to set-up Reach Native on that machine.
Next, you need Node.js 12+ and JDK 8+ installed.
Android Studio installs the latest Android SDK by default. Building a React Native app with native code, however, requires the Android 10(Q) SDK in particular. Additional Android SDKs can be installed through the SDK Manager in Android Studio.
To do that, open Android Studio, click on Settings, under file, and go to Appearance & Behavior → System Settings → Android SDK. Click on the "Configure" button and select "SDK Manager".
Select the "SDK Platforms" tab from within the SDK Manager, then check the box next to "Show Package Details" in the bottom right corner. Look for and expand the Android 10(Q) entry, then make sure the following sub items are checked:
Android SDK Platform 29
Intel x86 Atom_64 System Image or Google APIs Intel x86 Atom System Image Next, select the "SDK Tools" tab and check the box next to "Show Package Details" here as well. Look for and expand the "Android SDK Build-Tools" entry, then make sure that 29.0.2 is selected.
Finally, click "Apply" to download and install the Android SDK and related build tools.
Configure the ANDROID_HOME environment variable
Generally, you will use Android Studio to run the Android SDK CLI tool, but sometimes you may need to invoke those Android SDK commands in the terminal. For example, the React Native tools require some environment variables to be set up to build apps with native code. So please follow the below steps to add the android SDK command execute path in Linux PATH system environment variable value then you can invoke them in the command line.
Add the following lines to your $HOME/.bash_profile or $HOME/.bashrc (if you are using zsh, then ~/.zprofile or ~/.zshrc) config file:
export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
React Native Command Line Interface
React Native has a built-in command line interface. Rather than install and manage a specific version of the CLI globally, we recommend you access the current version at runtime using npx
, which ships with Node.js. With npx react-native <command>
, the current stable version of the CLI will be downloaded and executed at the time the command is run.
Creating a new application
React Native has a built-in command line interface, which you can use to generate a new project. You can access it without installing anything globally using npx
, which ships with Node.js. Let's create a new React Native project called "AwesomeProject":
npx react-native init AwesomeProject
Optionally, if you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init AwesomeProject --version X.XX.X
You can also start a project with a custom React Native template, like TypeScript, with --template argument:
npx react-native init AwesomeTSProject --template react-native-template-typescript
Preparing your Android Device
You will need an Android device to run your React Native Android app. This can be either a physical Android device, or more commonly, you can use an Android Virtual Device which allows you to emulate an Android device on your computer.
Either way, you will need to prepare the device to run Android apps for development. If you use an Android Virtual Device, you can do so by opening ./AwesomeProject/android, you can see the list of available Android Virtual Devices (AVDs) by opening the "AVD Manager" from within Android Studio. If you have recently installed Android Studio, you will likely need to create a new AVD. Select "Create Virtual Device...", then pick any Phone from the list and click "Next", then select the Q API Level 29 image. Click "Next", then "Finish" to create your AVD. At this point you should be able to click on the green triangle button next to your AVD to launch it, then proceed to the next step.
Running your React Native application
Step 1: Start Metro
First, you will need to start Metro, the JavaScript bundler that ships with React Native. Metro is a package that "takes in an entry file and various options, and returns a single JavaScript file that includes all your code and its dependencies."—Metro Docs. To start Metro, from inside your React Native project folder, run:
npx react-native start
This starts the Metro Bundler.
npm install --save-dev metro metro-core
If you use the Yarn package manager, you can use yarn instead of npx when running React Native commands inside an existing project.
If you're familiar with web development, Metro is a lot like webpack—for React Native apps. Unlike Kotlin or Java, JavaScript isn't compiled—and neither is React Native. Bundling isn't the same as compiling, but it can help improve startup performance and translate some platform-specific JavaScript into more widely supported JavaScript.
Step 2: Start your application
Let Metro Bundler run in its own terminal. Open a new terminal inside your React Native project folder. Run the following:
npx react-native run-android
If everything is set up correctly, you should see your new app running in your Android emulator shortly.
Running the command npx react-native run-android
is one way to run your app; you can also run it directly from within Android Studio.
If you can't get this to work, see the Troubleshooting page.
Modifying your app
Now that you have successfully run the app, let's modify it. For that, open App.js in your text editor of choice and edit some lines. Press the R key twice or select Reload from the Developer Menu (Ctrl + M) to see your changes!
That's it! Congratulations! You've successfully run and modified your first React Native app.