11apply plugin : " com.android.application"
22
33import com.android.build.OutputFile
4+ import org.apache.tools.ant.taskdefs.condition.Os
45
56project. ext. react = [
67 entryFile : " index.js" ,
@@ -14,9 +15,12 @@ def jscFlavor = 'org.webkit:android-jsc:+'
1415def enableHermes = project. ext. react. get(" enableHermes" , false )
1516
1617/**
17- * Architectures to build native code for in debug .
18+ * Architectures to build native code for.
1819 */
19- def nativeArchitectures = project. getProperties(). get(" reactNativeDebugArchitectures" )
20+ def reactNativeArchitectures () {
21+ def value = project. getProperties(). get(" reactNativeArchitectures" )
22+ return value ? value. split(" ," ) : [" armeabi-v7a" , " x86" , " x86_64" , " arm64-v8a" ]
23+ }
2024
2125android {
2226 compileSdkVersion rootProject. ext. compileSdkVersion
@@ -29,13 +33,85 @@ android {
2933 versionCode 1
3034 versionName " 1.0"
3135 multiDexEnabled true
36+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
37+
38+ if (isNewArchitectureEnabled()) {
39+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
40+ externalNativeBuild {
41+ ndkBuild {
42+ arguments " APP_PLATFORM=android-21" ,
43+ " APP_STL=c++_shared" ,
44+ " NDK_TOOLCHAIN_VERSION=clang" ,
45+ " GENERATED_SRC_DIR=$buildDir /generated/source" ,
46+ " PROJECT_BUILD_DIR=$buildDir " ,
47+ " REACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
48+ " REACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build"
49+ cFlags " -Wall" , " -Werror" , " -fexceptions" , " -frtti" , " -DWITH_INSPECTOR=1"
50+ cppFlags " -std=c++17"
51+ // Make sure this target name is the same you specify inside the
52+ // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
53+ targets " rnuilib_appmodules"
54+ // Fix for windows limit on number of character in file paths and in command lines
55+ if (Os . isFamily(Os . FAMILY_WINDOWS )) {
56+ arguments " NDK_APP_SHORT_COMMANDS=true"
57+ }
58+ }
59+ }
60+ if (! enableSeparateBuildPerCPUArchitecture) {
61+ ndk {
62+ abiFilters (* reactNativeArchitectures())
63+ }
64+ }
65+ }
66+ }
67+
68+ if (isNewArchitectureEnabled()) {
69+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
70+ externalNativeBuild {
71+ ndkBuild {
72+ path " $projectDir /src/main/jni/Android.mk"
73+ }
74+ }
75+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
76+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
77+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
78+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
79+ into(" $buildDir /react-ndk/exported" )
80+ }
81+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
82+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
83+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
84+ into(" $buildDir /react-ndk/exported" )
85+ }
86+ afterEvaluate {
87+ // If you wish to add a custom TurboModule or component locally,
88+ // you should uncomment this line.
89+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
90+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
91+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
92+
93+ // Due to a bug inside AGP, we have to explicitly set a dependency
94+ // between configureNdkBuild* tasks and the preBuild tasks.
95+ // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
96+ configureNdkBuildRelease. dependsOn(preReleaseBuild)
97+ configureNdkBuildDebug. dependsOn(preDebugBuild)
98+ reactNativeArchitectures(). each { architecture ->
99+ tasks. findByName(" configureNdkBuildDebug[${ architecture} ]" )?. configure {
100+ dependsOn(" preDebugBuild" )
101+ }
102+ tasks. findByName(" configureNdkBuildRelease[${ architecture} ]" )?. configure {
103+ dependsOn(" preReleaseBuild" )
104+ }
105+ }
106+ }
32107 }
108+
33109 splits {
34110 abi {
35111 reset()
36112 enable enableSeparateBuildPerCPUArchitecture
37113 universalApk false // If true, also generate a universal APK
38- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
114+ include ( * reactNativeArchitectures())
39115 }
40116 }
41117 signingConfigs {
@@ -49,11 +125,6 @@ android {
49125 buildTypes {
50126 debug {
51127 signingConfig signingConfigs. debug
52- if (nativeArchitectures) {
53- ndk {
54- abiFilters nativeArchitectures. split(' ,' )
55- }
56- }
57128 }
58129 release {
59130 // Caution! In production, you need to generate your own keystore file.
@@ -113,6 +184,18 @@ dependencies {
113184 }
114185}
115186
187+ if (isNewArchitectureEnabled()) {
188+ // If new architecture is enabled, we let you build RN from source
189+ // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
190+ // This will be applied to all the imported transtitive dependency.
191+ configurations. all {
192+ resolutionStrategy. dependencySubstitution {
193+ substitute(module(" com.facebook.react:react-native" ))
194+ .using(project(" :ReactAndroid" )). because(" On New Architecture we're building React Native from source" )
195+ }
196+ }
197+ }
198+
116199// Run this once to be able to run the application with BUCK
117200// puts all compile dependencies into folder libs for BUCK to use
118201task copyDownloadableDepsToLibs (type : Copy ) {
@@ -121,3 +204,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
121204}
122205
123206apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
207+
208+ def isNewArchitectureEnabled () {
209+ // To opt-in for the New Architecture, you can either:
210+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
211+ // - Invoke gradle with `-newArchEnabled=true`
212+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
213+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
214+ }
0 commit comments