Step 1: Install create-react-native-app

Introduction to create-react-native-app

create-react-native-app is a powerful tool that simplifies the process of starting a new React Native application. It’s designed to help developers create a React Native app with no build configuration, enabling them to focus on writing code rather than setting up the development environment.

This tool provides a streamlined way to initialize a React Native project, including everything you need to develop, build, and test your app across multiple platforms.

Installation Process

To install create-react-native-app, you’ll need to have Node.js installed on your machine. Node.js comes with npm (Node Package Manager), which you’ll use to install the tool.

Here’s a step-by-step guide to get you started:

1. Check Node.js and npm Installation

First, ensure that you have Node.js and npm installed by running the following commands in your terminal or command prompt:

node –version
npm –version

If these commands return version numbers, it means Node.js and npm are installed. If not, download and install Node.js from the official website, which will also install npm.

2. Install create-react-native-app

With Node.js and npm ready, install create-react-native-app globally on your machine using npm with the command:

npm install -g create-react-native-app

The -g flag installs the package globally, making it available from any directory on your system.

3. Verify Installation

After installation, you can verify that create-react-native-app is installed correctly by checking its version:

create-react-native-app –version

Successful installation will display the version number of create-react-native-app.

Following these steps, you’ll have create-react-native-app installed and ready to use. This tool simplifies the initial setup and lets you jump straight into building your React Native application, making it an excellent starting point for beginners learning React Native.

In the next step, we’ll use create-react-native-app to create a new React Native project and explore the generated project structure.

Step 2: Create Project

After successfully installing create-react-native-app, you’re now ready to initialize your first React Native project. This step will walk you through creating a new project and performing some basic configurations to get started.

Project Initialization

1. Creating a New React Native App

To create a new app, open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command:

npx create-react-native-app MyReactNativeApp

You can replace MyReactNativeApp with your desired project name. This command fetches the latest version of create-react-native-app and creates a new React Native project with the specified name.

2. Navigating into Your Project Directory

Once the project has been created, navigate into your project directory:

cd MyReactNativeApp

3. Starting the Development Server

To start the development server and run your app, execute:

npm start

Alternatively, you can use expo start if the Expo CLI is installed. This command launches the Metro bundler, which compiles your JavaScript code into a bundle that the app can execute.

Basic Configuration

Expo Developer Tools

Expo Developer Tools will open in your web browser when you start the project with npm start or expo start. This graphical interface allows you to run your app on a physical device or an emulator, view logs, and access various developer tools.

Running on a Device or Emulator

Download the Expo app from the Google Play Store or iOS App Store to run your app on a physical device. Scan the QR code displayed in the terminal or Expo Developer Tools using the Expo app to open your project.

For emulators, you’ll need to have Android Studio (for Android) or Xcode (for iOS) installed and configured. Expo Developer Tools provides direct options to run on an iOS simulator or Android emulator.

Editing Your App

Your project’s entry point is App.js. Open this file in your preferred code editor and start editing. Thanks to the live reloading feature, any changes you make will automatically be compiled and reflected in your running app.

App Configuration (app.json)

The app.json file in your project root contains configuration settings for your app, like its name, version, and more. You can customize these settings as needed for your project.

Step 3: NodeJS, Python, JDK8

To develop with React Native, specific software dependencies are crucial for setting up the environment, compiling code, and running your application. Node.js, Python, and JDK (Java Development Kit) play significant roles in this process.

Let’s explore the importance of each and how to install them.

Role of Each Software

  • js:Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It’s essential for running the React Native command line interface (CLI) and most development tools and packages you’ll use. Node.js comes with npm (Node Package Manager), which is used to install libraries and manage project dependencies.
  • Python:Python is a programming language that certain development tools and scripts in the React Native ecosystem might require. It’s particularly important for developers targeting the Android platform, as some build scripts and tools used in the process are written in Python.
  • JDK (Java Development Kit): The JDK is a software development environment used for developing Java applications and applets. It is essential for Android development because it includes the Java Runtime Environment (JRE), compilers, and tools to compile and package Java applications. React Native uses the JDK to compile and run Android applications.

Installation Guide

Node.js and npm

  1. Download Node.js Installer: Go to the official Node.js website and download the installer for your operating system. Downloading the LTS (Long-Term Support) version is recommended for better stability.
  2. Install Node.js: Run the downloaded installer and follow the installation prompts. Make sure to include the option to install npm and add Node.js to your PATH.
  3. Verify Installation: Open a terminal or command prompt and run node –version and npm –version to ensure both were installed correctly.

Python

  1. Download Python: Visit the official Python website and download the latest version of Python 3 for your operating system.
  2. Install Python: Run the installer. Make sure to select the option to add Python to your PATH during installation.
  3. Verify Installation: To confirm that Python was installed correctly and is accessible from your terminal or command prompt, open a new terminal window, type python –version, and press enter. This command checks which version of Python is installed and if it’s correctly set up in your system’s PATH. You should see the Python version number if the installation was successful.

JDK (Java Development Kit)

  1. Download JDK: Download the latest version of the JDK from the Oracle website or adopt an open-source version like OpenJDK.
  2. Run the Downloaded Installer: After downloading the JDK installer from the official website, run it and follow the installation prompts on your screen. This process installs the JDK on your computer, which is necessary for developing Android applications with React Native.
  3. Adding the JDK to Your System’s PATH: The PATH environment variable is a list of directories where your operating system looks for executable files (like the Java compiler and other command-line tools). Adding the JDK to your PATH makes these tools accessible from your terminal or command prompt, allowing you to compile and run Java applications without needing to navigate to the JDK’s bin directory every time.
  4. How to Add JDK to PATH: On Windows, search for “Environment Variables” in your system settings, then find the PATH variable under “System Variables” and add the path to the JDK’s bin directory (e.g., C:\Program Files\Java\jdk-14\bin).

    On macOS and Linux, you can add a line to your shell profile file (such as .bash_profile, .bashrc, or .zprofile in your home directory) that exports the JDK’s bin directory to the PATH variable, like export PATH=/usr/lib/jvm/java-14/bin:$PATH.
  5. Verify Installation:
    • On Windows, search for “Command Prompt” or “cmd” in the Start menu.
    • On macOS or Linux, open the “Terminal” application.
    • For Windows, type java -version and press Enter. This command checks the version of the Java runtime environment (JRE) installed on your system.
    • For macOS or Linux, type the Javac -version and press Enter. This command checks the version of the Java compiler, which is part of the JDK.

Expected Outcome: Both commands should display the version number of Java and the Java compiler installed on your system.

For example, you might see output like java version “14.0.1” and javac 14.0.1. This output confirms that Java and the JDK tools are correctly installed and accessible.

Step 4: Install React Native CLI

The React Native Command Line Interface (CLI) is a valuable tool for developing React Native applications. It provides a set of commands for creating, building, and managing React Native projects.

Unlike create-react-native-app, which aims to simplify the initial setup for beginners, the React Native CLI offers more flexibility and control, making it essential for developers looking to customize their build configurations or integrate native code.

Importance of React Native CLI

  • Project Initialization: The CLI allows for the creation of new React Native projects with a simple command, laying down the foundation for your application.
  • Running on Devices: It provides commands to run your app on iOS simulators, Android emulators, and physical devices, simplifying the testing process across different environments.
  • Linking Libraries: For projects that require native dependencies, the CLI can automate the linking process, ensuring that your JavaScript code can interact with native modules correctly.
  • Debugging and Logging: The CLI offers tools for debugging your application and viewing logs, which are essential for troubleshooting and ensuring your app runs smoothly.

Installation Steps

1. Open Terminal or Command Prompt: Begin by opening your terminal (macOS/Linux) or command prompt (Windows).

2. Install the CLI Globally: To install the React Native CLI, run the following command:

npm install -g react-native-cli

Tutorial 2 React Native - Environment Setup

Installing it globally (-g) makes the CLI available from any directory on your system, allowing you to manage React Native projects wherever they are located.

3. Verify Installation: Once the installation is complete, you can verify it by checking the CLI version:

react-native –version

This command should return the version of the React Native CLI installed on your system, confirming that the installation was successful.

4. Creating a New React Native Project: With the React Native CLI installed, you can now initialize a new project by running:

react-native initMyNewReactNativeProject

Replace MyNewReactNativeProject with your desired project name. This command creates a new React Native project directory with all the necessary files and dependencies to get started.

5. Navigating to Your Project Directory: Move into your newly created project directory:

cd MyNewReactNativeProject

6. Running Your Project: To run your project on iOS, use:

react-native run-ios

For Android, make sure you have an Android emulator running or a device connected, then use:

react-native run-android

Step 5: Start React Native

After setting up your React Native environment and creating a new project, the next step is to start the React Native development server and run your app. This process involves initiating the development server, which bundles your JavaScript code and serves it to your app.

1. Open Your Project Directory:

After setting up your React Native environment and creating a new project, the next step is to start the React Native development server and run your app. This process involves initiating the development server, which bundles your JavaScript code and serves it to your app.

2. Start the Development Server:

To start the React Native development server, run the following command:

npm start

Alternatively, if you’re using Yarn, you can start the server with:

yarn start

This command starts the Metro bundler, a JavaScript bundler optimized for React Native. Metro compiles your JavaScript code and assets, making them ready for use in your app.

3. Run Your App:

iOS Simulator:

To run your app on the iOS Simulator, you can use the command (only available on macOS):

npx react-native run-ios

Android Emulator:

To run your app on an Android Emulator, first ensure that the emulator is running. Then, execute:

npx react-native run-android

These commands will build and launch your application on the respective emulator or simulator. If you have a physical device connected, run-android can also install the app on your device.

Troubleshooting Common Issues

1. App Does Not Reload or Update

  • Ensure the development server is running and connected to the same network as your testing device or emulator.
  • Try reloading the app manually. For iOS, press Cmd + R in the simulator. For Android, press R twice on the keyboard or use the Reload option in the developer menu.

2. Metro Bundler Errors

  • Clear the Metro bundler cache with npx react-native start –reset-cache.
  • Ensure there are no syntax errors or unresolved dependencies in your code.

3. iOS Simulator or Android Emulator Not Starting

  • For iOS, ensure Xcode is installed and updated. For Android, check that the Android SDK is correctly installed and that an AVD (Android Virtual Device) is properly configured and started.

4. Connection Issues on Physical Devices

  • Ensure your device is connected to the same Wi-Fi network as your computer.
  • For Android, check that USB Debugging is enabled and the device is authorized for debugging.

Step 6: Eject the Project

  • Ejecting a React Native project is a significant step that involves transitioning from the managed workflow provided by tools like Expo to a more customizable environment where you have direct control over the native code (iOS and Android).

Understanding Ejecting

  • Managed vs. Bare Workflow: Initially, React Native projects, especially those started with Expo, operate in a managed workflow where the complexity of native code is abstracted away. Ejecting moves your project to a bare workflow, giving you full access to the underlying native files.
  • Why Eject? Developers might choose to eject their project to implement functionality that requires custom native code, integrate with third-party native libraries not supported by Expo, or have more control over their app’s build and configuration process.
  • Considerations: Before ejecting, it’s essential to understand that this action is irreversible. Once you eject, you cannot go back to the managed workflow without significant effort. Therefore, it’s recommended only when necessary and when you’re comfortable handling native development environments.

Ejection Process

1. Prepare Your Project

  • Make sure your project’s JavaScript code is error-free and stable.
  • Back up your project to avoid data loss.

2. Ejecting from Expo

If you’re using Expo and ready to eject, navigate to your project directory in the terminal and run:

expo eject

This command will prompt you to choose between two app types: a “bare” minimum native app or a “bare + ExpoKit” app. Selecting “bare” is the most common choice, giving you a minimal setup to work with.

3. Install Native Dependencies

After ejecting, you’ll need to manually install dependencies for iOS and Android. Navigate to your project’s iOS and Android directories and follow platform-specific instructions.

For iOS:

    • Navigate to the ios directory.
    • Run pod install to install CocoaPods dependencies.
    • Open the .xcworkspace file in Xcode to continue development.

For Android:

    • Make sure Android SDK and build tools are correctly set up.
    • Open the android folder as an Android Studio project to resolve any dependencies or version issues.

4. Running Your Ejected Project:

  • To run your iOS app, use Xcode or the command npx react-native run-ios from the project directory.
  • For Android, open your project in Android Studio or use npx react-native run-android.

5. Moving Forward:

  • After ejecting, you’ll manage native dependencies and SDK updates and build processes directly.
  • Familiarize yourself with native development documentation for iOS and Android to navigate this new workflow effectively.

Step 7: Installing Android Studio

Installing Android Studio is a crucial step for React Native developers, especially for those working on Android applications. Android Studio is the official Integrated Development Environment (IDE) for Android development and provides a comprehensive set of tools for building, testing, and debugging Android apps.

Why Android Studio?

  • Official Android SDK: Android Studio includes the Android SDK (Software Development Kit), which is essential for building any Android app, including those developed with React Native.
  • Emulator: It provides a powerful emulator to test your applications on various Android versions and device types without needing physical devices.
  • Native Code Integration: For projects that require custom native Android code or third-party native modules, Android Studio is indispensable for editing, compiling, and debugging.
  • Performance Monitoring: It offers extensive tools for monitoring app performance, such as memory usage and CPU load, helping developers optimize their applications.
  • UI Design: Though React Native uses JavaScript for UI development, Android Studio can be helpful for creating and managing XML files for native Android components if needed.

Installation Procedure

1. Download Android Studio:

2. Run the Installer:

  • Windows: Launch the downloaded installer and follow the installation instructions. This may include downloading additional SDK components as suggested by the installer. Ensure that you select the option to install the Android Virtual Device (AVD) during the setup process.
  • macOS: Open the downloaded DMG file, drag and drop Android Studio into the Applications folder, and follow the setup wizard. The wizard will prompt you to download the necessary SDK components.
  • Linux: Unpack the downloaded Tar file into your desired location and execute the studio.sh script from the terminal to start the setup wizard. Follow the wizard’s instructions to install the necessary SDK components.

3. Configure the Android SDK:

  • After installation, open Android Studio. The setup wizard will guide you through downloading the Android SDK components. If you’re new to Android development, it’s advisable to accept the default SDK settings recommended by the setup wizard.

4. Set Up an Android Emulator:

  • In Android Studio, access the AVD Manager by navigating to Tools > AVD Manager.
  • Click on “Create Virtual Device…” and select a device to emulate. Choose a system image (for example, a specific Android version). Download the system image if it’s not already available.
  • After configuring the device specifications, finish the setup. You can now run this emulator directly from Android Studio or from the command line using React Native CLI commands.

5. Configure Environment Variables:

  • ANDROID_HOME: This variable points to the location of your Android SDK. The SDK location varies by operating system:
    • macOS:~/Library/Android/sdk
    • Windows:%LOCALAPPDATA%\Android\sdk
  • Linux: ~/Android/Sdk
  • Path: Add the paths to the platform-tools and tools directories within the Android SDK to your system’s PATH variable. This enables commands like adb to be run from the terminal.

6. Verify the Installation:

  • Open a new terminal window (or Command Prompt on Windows) and type adb version to check that the Android Debug Bridge (ADB) is installed correctly. You should see the version information for ADB, confirming that Android Studio and its components are set up properly on your system.

Step 8: Configuring AVD Manager

The Android Virtual Device (AVD) Manager in Android Studio allows developers to set up and manage virtual Android devices for testing applications. These virtual devices emulate different configurations of Android hardware, making it easier to test how an app behaves across a wide range of devices without needing physical hardware for each one.

Setting Up Android Virtual Devices

AVD Manager provides a user-friendly interface to create and configure virtual devices. Each AVD is an emulator configuration that simulates a physical Android device. You can customize your AVD to emulate different types of devices, screen sizes, hardware properties, and Android versions to ensure your React Native app works well across the Android ecosystem.

Configuration Steps

1. Open AVD Manager:

  • Launch Android Studio.
  • Navigate to Tools > AVD Manager. The AVD Manager interface will open, listing any existing virtual devices and the option to create new ones.

2. Create a New Virtual Device:

  • Click on “Create Virtual Device” at the bottom of the AVD Manager.
  • Choose a device definition from the list. This can be anything from a Pixel phone to a Nexus tablet, depending on what type of device you want to emulate. Click “Next” after selecting.

3. Select a System Image:

  • Choose a system image for your AVD. This determines the Android version the emulator will run. You might need to download the system image by clicking the “Download” link next to the desired version (for example, Android Q).
  • After downloading, select the system image and click “Next.”

4. Configure the AVD:

  • Give your AVD a name.
  • Configure hardware and emulation options as needed. Common adjustments include:
    • RAM and VM Heap: Adjust these settings based on the requirements of the app you’re developing and the capabilities of your development machine.
    • Graphics: Choose between software or hardware graphics acceleration based on your system’s capabilities and the requirements of your app.
  • You can also modify the orientation, scale, and other hardware properties to mimic different devices.

5. Launch the Emulator:

  • After configuring your AVD, click “Finish” to create it. Your new virtual device will appear in the AVD Manager list.
  • Click the green play button under the “Actions” column to start the emulator. The first startup may take some time as the emulator initializes.

6. Running Your React Native App:

  • With the emulator running, you can now deploy your React Native app to it. Use the React Native CLI command from your project directory:

npx react-native run-android

This command compiles the Android app and installs it on the running emulator.

By setting up and configuring AVDs through the AVD Manager in Android Studio, you create a flexible testing environment that can simulate a wide range of devices.

Step 8: Configuring AVD Manager

Once you’ve set up your React Native environment and configured your Android Virtual Devices (AVDs) or connected a physical device, the next step is launching your app to see it in action.

Running your React Native app on an Android device, whether virtual or physical, is a critical development phase, allowing you to test and iterate on your application.

Launching on an Android Device

For a Virtual Device:

1. Start Your AVD:

  • Open AVD Manager in Android Studio and start your preferred emulator from the list of configured virtual devices.

2. Run the App:

  • Open a terminal or command prompt.
  • Navigate to your React Native project directory.
  • Run the command:

npx react-native run-android

This command builds your app and installs it on the running emulator.

For a Physical Device:

1. Enable Developer Options and USB Debugging:

  • On your Android device, go to Settings > About phone and tap on the Build number 7 times to enable Developer options.
  • Go back to the main Settings menu, find Developer options, and enable USB Debugging.

2. Connect Your Device:

  • Connect your device to your computer via a USB cable.
  • If prompted on your device, allow USB debugging.

3. Run the App:

  • Ensure your device is detected by running adb devices from a terminal or command prompt. You should see your device listed.
  • In your project directory, run:

npx react-native run-android

The command compiles the app and installs it on your connected device.

Debugging Tips

1. App Not Installing

  • Make sure USB Debugging is enabled on your device.
  • Check that your device is authorized on your computer if prompted when connecting via USB.
  • Verify the device appears in adb devices.

2. Metro Bundler Errors

  • If the Metro bundler shows errors, try resetting the cache with npx react-native start –reset-cache.

3. App Crashes on Launch

  • Check the logcat output in Android Studio for error messages that can help identify the issue. Logcat is a command-line tool that dumps a log of system messages, including stack traces, when the device throws errors and messages written from your application with the Log class.
      • To use logcat within Android Studio:
      • Open Android Studio and navigate to the bottom toolbar.
      • Click on the “Logcat” tab.
      • Choose your device and app from the dropdown menus.
      • Look for error messages that correspond to your app crash. These messages can help identify what went wrong.
  • For React Native versions 0.60 and above, auto-linking should handle most cases. For earlier versions or troubleshooting, manual linking may be necessary. Refer to the documentation of the specific library for instructions on linking native dependencies.

4. Slow Build Times

  • Incremental builds can speed up the development process. After the first full build, subsequent builds should be faster. Consider enabling Gradle Daemon to speed up the build process.The Gradle Daemon is a background process designed to speed up the build process by avoiding constant reinitialization and caching build information. It’s enabled by default in recent versions of Gradle.

To ensure it’s enabled or to manually enable it:

    • Navigate to the gradle.properties file in your Android project.
    • Add or ensure this line is present: org.gradle.daemon=true.
    • Restart your build process.
  • For more details on the Gradle Daemon and how to configure it for optimal performance, refer to the Gradle User Manual.

5. Debugging JavaScript Code

  • To debug JavaScript code running on your React Native app:
    • Shake your device to open the developer menu, or use the command adb shell input keyevent 82 from your terminal if your device is connected via USB.
    • Select “Debug JS Remotely.”
    • This will open a new tab in your default browser with Chrome’s Developer Tools loaded.
    • You can place breakpoints, inspect variables, and view console logs within the browser.

Using console.log:         

  • The console.log method outputs debug information to the Metro bundler’s terminal or to Chrome’s Developer Console when remote debugging is enabled. It’s a simple but effective way to trace your code execution and inspect variable states.

Debugging JavaScript Code:

  • Enable Remote JS Debugging in your app’s developer menu to debug JavaScript code using Chrome’s Developer Tools.
  • Use console.log statements to print debug information, visible in Metro bundler’s terminal or Chrome’s Developer Console when remote debugging.

Step 10: local.properties

The local.properties file in a React Native Android project specifies local configuration settings for the Android SDK and build tools. Understanding how to properly configure this file can help resolve build errors and streamline the development process.

Understanding local.properties

The local.properties file is a key-value pair file located in the android/ directory of your React Native project. This file is not checked into version control because it contains paths specific to your local development environment.

It primarily specifies the location of the Android SDK and, if applicable, the Android NDK on your development machine. It ensures that the Gradle build tool can correctly invoke the Android SDK tools and build your application.

Configuration Details

Setting Android SDK Location

The most common use of local.properties is to set the SDK location. This might be necessary if your environment variables are not set up correctly or if you want to override the SDK path for a specific project.

1. Locate Your Android SDK:

  • If you installed Android Studio, the SDK is usually located in:
      • Windows:C:\Users\[YOUR_USERNAME]\AppData\Local\Android\Sdk
      • macOS:~/Library/Android/sdk
      • Linux:~/Android/Sdk
  • If you’re unsure, you can find the SDK location in Android Studio under Preferences > Appearance & Behavior > System Settings > Android SDK.

2. Edit or Create local.properties:

  • Navigate to the android/ directory in your React Native project.
  • If properties does not exist, create it using a text editor.
  • Add the following line, replacing [PATH_TO_SDK] with the actual path to your Android SDK:

sdk.dir=[PATH_TO_SDK]

Example for macOS:

sdk.dir=/Users/[YOUR_USERNAME]/Library/Android/sdk

Setting Android NDK Location

  • If your project uses native code or third-party libraries that require the Android NDK, you might also need to specify the NDK location in local.properties.
  • Locate your NDK installation, which is often within the Android SDK directory under ndk-bundle.
  • Add the NDK path to local.properties:

ndk.dir=[PATH_TO_NDK]

Tips

  • Use absolute paths and avoid using environment variables or relative paths in properties.
  • After modifying properties, you may need to sync your project with Gradle files in Android Studio or restart the React Native packager for changes to take effect.

By correctly setting up the local.properties file, you ensure that your React Native project can smoothly find and use the Android SDK and NDK tools required for building your app. This setup minimizes potential build issues and simplifies the development process, especially when working across multiple machines or collaborating with other developers.