How to integrate React Native into a modern Android project in 2024

Last Update On: Nov 4, 2024

There are many guides on the internet that explain how to integrate React Native into your Android project, such as the official documentation (https://reactnative.dev/docs/integration-with-existing-apps?language=kotlin).

However, when you actually try to do it, you'll find that many things are different from what the documentation says. Oops it's really frustrating!

Recently, I successfully integrated React Native 0.75 into my Android project, which uses Kotlin + Gradle 8 + AGP 8. This project already had some pages built with Kotlin, and adding React Native introduced more cross-platform possibilities. Let me share the issues I encountered during the process.


Dependencies

There are some plugin-related scripts inside the node_modules directory that you should include:

// file: /build.grade pluginManagement { repositories { google { content { includeGroupByRegex("com\\.android.*") includeGroupByRegex("com\\.google.*") includeGroupByRegex("androidx.*") } } mavenCentral() gradlePluginPortal() } // Note this line: includeBuild('../node_modules/@react-native/gradle-plugin') }

Note that to ensure compatibility with the legacy repositories mode, you should set it to PREFER_PROJECT:

dependencyResolutionManagement { // NOT FAIL_ON_PROJECT_REPOS repositoriesMode.set(RepositoriesMode.PREFER_PROJECT) repositories { google() mavenCentral() } }

Plugins

Add the react native plugin:

// File: /build.gradle plugins { id "com.facebook.react.settings" } // File: /app/build.gradle plugins { alias(libs.plugins.android.application) alias(libs.plugins.jetbrains.kotlin.android) // Note this line: id "com.facebook.react" }

Auto Link

// File: /build.gradle extensions.configure(com.facebook.react.ReactSettingsExtension) { ex -> ex.autolinkLibrariesFromCommand() } // File: /app/build.gradle for the app module: react { autolinkLibrariesWithApp() }

Java Version

For some reason that I'm not currently clear on, the Java version has to be 17, so:

// File: /app/build.gradle compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = '17' }




I believe that with these basic settings, your modern Android project should run perfectly.

Note that these configurations are only suitable for React Native's legacy architecture, which requires setting newArchEnabled=false in gradle.properties. If you have already enabled the new architecture, you may need to add more configurations.


Thanks for reading, stay awsome!