Get Started With React Native 3
Get Started With React Native 3
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:
Prerequisites
Follow the guide on setting up your development environment to configure your
development environment for building React Native apps for Android.
To ensure a smooth experience, create a new folder for your integrated React Native
project, then copy your existing Android project to an /android subfolder.
Go to the root directory for your project and create a new package.json file with the
following contents:
https://reactnative.dev/docs/integration-with-existing-apps 1/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
{
"name": "MyReactNativeApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start"
}
}
Next, install the react and react-native packages. Open a terminal or command
prompt, then navigate to the directory with your package.json file and run:
npm Yarn
This will print a message similar to the following (scroll up in the installation command
output to see it):
warning " react-native@0.70.5 " has unmet peer dependency " react@18.1.0 "
npm Yarn
Installation process has created a new /node_modules folder. This folder stores all the
JavaScript dependencies required to build your project.
includeBuild('../node_modules/@react-native/gradle-plugin')
Then you need to open your top level build.gradle and include this line:
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 is available inside your project. Finally,
add those lines inside your app's build.gradle file (it's a different build.gradle file
inside your app folder):
repositories {
mavenCentral()
}
dependencies {
// Other dependencies here
+ implementation "com.facebook.react:react-android"
+ implementation "com.facebook.react:hermes-android"
}
Those depedencies are available on mavenCentral() so make sure you have it defined
in your repositories{} block.
INFO
https://reactnative.dev/docs/integration-with-existing-apps 3/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
Next add the following entry at the very bottom of the app/build.gradle :
Configuring permissions
Next, make sure you have the Internet permission in your AndroidManifest.xml :
This is only used in dev mode when reloading JavaScript from the development server,
so you can strip this in release builds if you need to.
Starting with Android 9 (API level 28), cleartext traffic is disabled by default; this
prevents your application from connecting to the Metro bundler. The changes below
allow cleartext traffic in debug builds.
https://reactnative.dev/docs/integration-with-existing-apps 4/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
To learn more about Network Security Config and the cleartext traffic policy see this
link.
Code integration
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 "High
Score" screen that will be integrated into our application.
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 require s other file that are part of your React Native
component or application, or it can contain all the code that is needed for it. In our
case, we will put everything in index.js .
In your index.js , create your component. In our sample here, we will add a <Text>
component within a styled <View> :
https://reactnative.dev/docs/integration-with-existing-apps 5/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
hello: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
AppRegistry.registerComponent(
'MyReactNativeApp',
() => HelloWorld,
);
If your app is targeting the Android API level 23 or greater, make sure you have the
permission android.permission.SYSTEM_ALERT_WINDOW enabled for the development
build. You can check this with Settings.canDrawOverlays(this); . This is required in
dev builds because React Native development errors must be displayed above all the
other windows. Due to the new permissions system introduced in the API level 23
(Android M), the user needs to approve it. This can be achieved by adding the following
code to your Activity's in onCreate() method.
...
Finally, the onActivityResult() method (as shown in the code below) has to be
overridden to handle the permission Accepted or Denied cases for consistent UX. Also,
https://reactnative.dev/docs/integration-with-existing-apps 6/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted
}
}
}
mReactInstanceManager.onActivityResult( this, requestCode, resultCode,
data );
}
Let's add some native code in order to start the React Native runtime and tell it to
render our JS component. To do this, we're going to create an Activity that creates a
ReactRootView , starts a React application inside it and sets it as the main content
view.
If you are targeting Android version <5, use the AppCompatActivity class from the
com.android.support:appcompat package instead of Activity .
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SoLoader.init(this, false);
`app/build.gradle` too.
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setCurrentActivity(this)
.setBundleAssetName("index.android.bundle")
.setJSMainModulePath("index")
.addPackages(packages)
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
mReactRootView.startReactApplication(mReactInstanceManager,
"MyReactNativeApp", null);
setContentView(mReactRootView);
}
@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}
}
If you are using a starter kit for React Native, replace the "HelloWorld" string with the
one in your index.js file (it’s the first argument to the
AppRegistry.registerComponent() method).
If you are using Android Studio, use Alt + Enter to add all missing imports in your
MyReactActivity class. Be careful to use your package’s BuildConfig and not the one
from the facebook package.
<activity
android:name=".MyReactActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
</activity>
https://reactnative.dev/docs/integration-with-existing-apps 8/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
@Override
protected void onPause() {
super.onPause();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostPause(this);
}
}
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostResume(this, this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostDestroy(this);
}
if (mReactRootView != null) {
mReactRootView.unmountReactApplication();
}
}
@Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
https://reactnative.dev/docs/integration-with-existing-apps 9/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}
This allows JavaScript to control what happens when the user presses the hardware
back button (e.g. to implement navigation). When JavaScript doesn't handle the back
button press, your invokeDefaultOnBackPressed method will be called. By default this
finishes your Activity .
Finally, we need to hook up the dev menu. By default, this is activated by (rage) shaking
the device, but this is not very useful in emulators. So we make it show when you press
the hardware menu button (use Ctrl + M if you're using Android Studio emulator):
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
mReactInstanceManager.showDevOptionsDialog();
return true;
}
return super.onKeyUp(keyCode, event);
}
You have now done all the basic steps to integrate React Native with your current
application. Now we will start the Metro bundler to build the index.bundle package
and the server running on localhost to serve it.
To run your app, you need to first start the development server. To do this, run the
following command in the root directory of your React Native project:
npm Yarn
npm start
https://reactnative.dev/docs/integration-with-existing-apps 10/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
Once you reach your React-powered activity inside the app, it should load the
JavaScript code from the development server and display:
You can use Android Studio to create your release builds too! It’s as quick as creating
release builds of your previously-existing native Android app.
If you use the React Native Gradle Plugin as described above, everything should work
when running app from Android Studio.
https://reactnative.dev/docs/integration-with-existing-apps 11/12
25/06/2024, 15:49 Integration with Existing Apps · React Native
If you're not using the React Native Gradle Plugin, there’s one additional step which
you’ll have to do before every release build. You need to execute the following to create
a React Native bundle, which will be included with your native Android app:
Don’t forget to replace the paths with correct ones and create the assets folder if it
doesn’t exist.
Now, create a release build of your native app from within Android Studio as usual and
you should be good to go!
Now what?
At this point you can continue developing your app as usual. Refer to our debugging
and deployment docs to learn more about working with React Native.
https://reactnative.dev/docs/integration-with-existing-apps 12/12