@@ -121,9 +121,12 @@ def jscFlavor = 'org.webkit:android-jsc:+'
121121def enableHermes = project. ext. react. get(" enableHermes" , false );
122122
123123/**
124- * Architectures to build native code for in debug .
124+ * Architectures to build native code for.
125125 */
126- def nativeArchitectures = project. getProperties(). get(" reactNativeDebugArchitectures" )
126+ def reactNativeArchitectures () {
127+ def value = project. getProperties(). get(" reactNativeArchitectures" )
128+ return value ? value. split(" ," ) : [" armeabi-v7a" , " x86" , " x86_64" , " arm64-v8a" ]
129+ }
127130
128131android {
129132 ndkVersion rootProject. ext. ndkVersion
@@ -136,13 +139,76 @@ android {
136139 targetSdkVersion rootProject. ext. targetSdkVersion
137140 versionCode 1
138141 versionName " 1.0"
142+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
143+
144+ if (isNewArchitectureEnabled()) {
145+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
146+ externalNativeBuild {
147+ ndkBuild {
148+ arguments " APP_PLATFORM=android-21" ,
149+ " APP_STL=c++_shared" ,
150+ " NDK_TOOLCHAIN_VERSION=clang" ,
151+ " GENERATED_SRC_DIR=$buildDir /generated/source" ,
152+ " PROJECT_BUILD_DIR=$buildDir " ,
153+ " REACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
154+ " REACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build"
155+ cFlags " -Wall" , " -Werror" , " -fexceptions" , " -frtti" , " -DWITH_INSPECTOR=1"
156+ cppFlags " -std=c++17"
157+ // Make sure this target name is the same you specify inside the
158+ // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
159+ targets " helloworld_appmodules"
160+ }
161+ }
162+ }
163+ }
164+
165+ if (isNewArchitectureEnabled()) {
166+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
167+ externalNativeBuild {
168+ ndkBuild {
169+ path " $projectDir /src/main/jni/Android.mk"
170+ }
171+ }
172+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
173+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
174+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
175+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
176+ into(" $buildDir /react-ndk/exported" )
177+ }
178+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
179+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
180+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
181+ into(" $buildDir /react-ndk/exported" )
182+ }
183+ afterEvaluate {
184+ // If you wish to add a custom TurboModule or component locally,
185+ // you should uncomment this line.
186+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
187+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
188+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
189+
190+ // Due to a bug inside AGP, we have to explicitly set a dependency
191+ // between configureNdkBuild* tasks and the preBuild tasks.
192+ // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
193+ configureNdkBuildRelease. dependsOn(preReleaseBuild)
194+ configureNdkBuildDebug. dependsOn(preDebugBuild)
195+ reactNativeArchitectures(). each { architecture ->
196+ tasks. findByName(" configureNdkBuildDebug[${ architecture} ]" )?. configure {
197+ dependsOn(" preDebugBuild" )
198+ }
199+ tasks. findByName(" configureNdkBuildRelease[${ architecture} ]" )?. configure {
200+ dependsOn(" preReleaseBuild" )
201+ }
202+ }
203+ }
139204 }
205+
140206 splits {
141207 abi {
142208 reset()
143209 enable enableSeparateBuildPerCPUArchitecture
144210 universalApk false // If true, also generate a universal APK
145- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
211+ include ( * reactNativeArchitectures())
146212 }
147213 }
148214 signingConfigs {
@@ -156,11 +222,6 @@ android {
156222 buildTypes {
157223 debug {
158224 signingConfig signingConfigs. debug
159- if (nativeArchitectures) {
160- ndk {
161- abiFilters nativeArchitectures. split(' ,' )
162- }
163- }
164225 }
165226 release {
166227 // Caution! In production, you need to generate your own keystore file.
@@ -190,6 +251,7 @@ android {
190251
191252dependencies {
192253 implementation fileTree(dir : " libs" , include : [" *.jar" ])
254+
193255 // noinspection GradleDynamicVersion
194256 implementation " com.facebook.react:react-native:+" // From node_modules
195257
@@ -217,6 +279,18 @@ dependencies {
217279 }
218280}
219281
282+ if (isNewArchitectureEnabled()) {
283+ // If new architecture is enabled, we let you build RN from source
284+ // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
285+ // This will be applied to all the imported transtitive dependency.
286+ configurations. all {
287+ resolutionStrategy. dependencySubstitution {
288+ substitute(module(" com.facebook.react:react-native" ))
289+ .using(project(" :ReactAndroid" )). because(" On New Architecture we're building React Native from source" )
290+ }
291+ }
292+ }
293+
220294// Run this once to be able to run the application with BUCK
221295// puts all compile dependencies into folder libs for BUCK to use
222296task copyDownloadableDepsToLibs (type : Copy ) {
@@ -225,3 +299,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
225299}
226300
227301apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
302+
303+ def isNewArchitectureEnabled () {
304+ // To opt-in for the New Architecture, you can either:
305+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
306+ // - Invoke gradle with `-newArchEnabled=true`
307+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
308+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
309+ }
0 commit comments