Skip to main content

Integration with Existing Apps

React Native is great when you are starting a new mobile app from scratch. However, it also works well for adding a single view or user flow to existing native applications. With a few steps, you can add new React Native based features, screens, views, etc.

The specific steps are different depending on what platform you're targeting.

Key Conceptsโ€‹

The keys to integrating React Native components into your Android application are to:

  1. Set up the correct directory structure.
  2. Install the necessary NPM dependencies.
  3. Adding React Native to your Gradle configuration.
  4. Writing the TypeScript code for your first React Native screen.
  5. Integrate React Native with your Android code using a ReactActivity.
  6. Testing your integration by running the bundler and seeing your app in action.

Using the Community Templateโ€‹

While you follow this guide, we suggest you to use the React Native Community Template as reference. The template contains a minimal Android app and will help you understanding how to integrate React Native into an existing Android app.

Prerequisitesโ€‹

Follow the guide on setting up your development environment and using React Native without a framework to configure your development environment for building React Native apps for Android. This guide also assumes you're familiar with the basics of Android development such as creating Activities and editing the AndroidManifest.xml file.

1. Set up directory structureโ€‹

To ensure a smooth experience, create a new folder for your integrated React Native project, then move your existing Android project to the /android subfolder.

2. Install NPM dependenciesโ€‹

Go to the root directory and run the following command:

shell
curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/0.87-stable/template/package.json

This will copy the package.json file from the Community template to your project.

Next, install the NPM packages by running:

npm install

Installation process has created a new node_modules folder. This folder stores all the JavaScript dependencies required to build your project.

Add node_modules/ to your .gitignore file (here the Community default one).

3. Adding React Native to your appโ€‹

Configuring Gradleโ€‹

React Native uses the React Native Gradle Plugin to configure your dependencies and project setup.

First, let's edit your settings.gradle file by adding those lines (as suggested from the Community template):

Groovy
// Configures the React Native Gradle Settings plugin used for autolinking
pluginManagement { includeBuild("../node_modules/@react-native/gradle-plugin") }
plugins { id("com.facebook.react.settings") }
extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.autolinkLibrariesFromCommand() }
// If using .gradle.kts files:
// extensions.configure<com.facebook.react.ReactSettingsExtension> { autolinkLibrariesFromCommand() }
includeBuild("../node_modules/@react-native/gradle-plugin")

// Include your existing Gradle modules here.
// include(":app")

Then you need to open your top level build.gradle and include this line (as suggested from the Community template):

Diff
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:7.3.1")
+ classpath("com.facebook.react:react-native-gradle-plugin")
}
}

This makes sure the React Native Gradle Plugin (RNGP) is available inside your project. Finally, add those lines inside your Applications's build.gradle file (it's a different build.gradle file usually inside your app folder - you can use the Community template file as reference):

Diff
apply plugin: "com.android.application"
+apply plugin: "com.facebook.react"

repositories {
mavenCentral()
}

dependencies {
// Other dependencies here
+ // Note: we intentionally don't specify the version number here as RNGP will take care of it.
+ // If you don't use the RNGP, you'll have to specify version manually.
+ implementation("com.facebook.react:react-android")
+ implementation("com.facebook.react:hermes-android")
}

+react {
+ // Needed to enable Autolinking - https://github.com/react-native-community/cli/blob/master/docs/autolinking.md
+ autolinkLibrariesWithApp()
+}

Finally, open your application gradle.properties files and add the following line (here the Community template file as reference):

Diff
+reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
+newArchEnabled=true
+hermesEnabled=true

Configuring your manifestโ€‹

First, make sure you have the Internet permission in your AndroidManifest.xml:

Diff
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

+ <uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MainApplication">
</application>
</manifest>

Then you need to enable cleartext traffic in your debug AndroidManifest.xml:

Diff
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
+ android:usesCleartextTraffic="true"
+ tools:targetApi="28"
/>
</manifest>

As usual, here the AndroidManifest.xml file from the Community template to use as a reference: main and debug.

This is needed as your application will communicate with your local bundler, Metro, via HTTP.

Make sure you add this only to your debug manifest.

4. Writing the TypeScript Codeโ€‹

Now we will actually modify the native Android application to integrate React Native.

The first bit of code we will write is the actual React Native code for the new screen that will be integrated into our application.

Create a index.js fileโ€‹

First, create an empty index.js file in the root of your React Native project.

index.js is the starting point for React Native applications, and it is always required. It can be a small file that imports other file that are part of your React Native component or application, or it can contain all the code that is needed for it.

Our index.js should look as follows (here the Community template file as reference):

JavaScript
import {AppRegistry} from 'react-native';
import App from './App';

AppRegistry.registerComponent('HelloWorld', () => App);

Create a App.tsx fileโ€‹

Let's create an App.tsx file. This is a TypeScript file that can have JSX expressions. It contains the root React Native component that we will integrate into our Android application (link):

React TSX
import {type JSX} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';
import {
Colors,
DebugInstructions,
Header,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

function App(): JSX.Element {
const isDarkMode = useColorScheme() === 'dark';

const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};

return (
<SafeAreaView style={backgroundStyle}>
<StatusBar