@@ -126,6 +126,14 @@ def enableHermes = project.ext.react.get("enableHermes", false);
126126 */
127127def nativeArchitectures = project. getProperties(). get(" reactNativeDebugArchitectures" )
128128
129+ /**
130+ * Architectures to build native code for.
131+ */
132+ def reactNativeArchitectures () {
133+ def value = project. getProperties(). get(" reactNativeArchitectures" )
134+ return value ? value. split(" ," ) : [" armeabi-v7a" , " x86" , " x86_64" , " arm64-v8a" ]
135+ }
136+
129137android {
130138 testOptions {
131139 unitTests. all {
@@ -144,7 +152,77 @@ android {
144152 versionName " 1.0"
145153 testBuildType System . getProperty(' testBuildType' , ' debug' )
146154 testInstrumentationRunner ' androidx.test.runner.AndroidJUnitRunner'
155+
156+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
157+
158+ if (isNewArchitectureEnabled()) {
159+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
160+ externalNativeBuild {
161+ ndkBuild {
162+ arguments " APP_PLATFORM=android-21" ,
163+ " APP_STL=c++_shared" ,
164+ " NDK_TOOLCHAIN_VERSION=clang" ,
165+ " GENERATED_SRC_DIR=$buildDir /generated/source" ,
166+ " PROJECT_BUILD_DIR=$buildDir " ,
167+ " REACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
168+ " REACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build" ,
169+ " NODE_MODULES_DIR=$rootDir /../node_modules"
170+ cFlags " -Wall" , " -Werror" , " -fexceptions" , " -frtti" , " -DWITH_INSPECTOR=1"
171+ cppFlags " -std=c++17"
172+ // Make sure this target name is the same you specify inside the
173+ // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
174+ targets " flatlistpro_appmodules"
175+ }
176+ }
177+ if (! enableSeparateBuildPerCPUArchitecture) {
178+ ndk {
179+ abiFilters (* reactNativeArchitectures())
180+ }
181+ }
182+ }
183+ }
184+
185+ if (isNewArchitectureEnabled()) {
186+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
187+ externalNativeBuild {
188+ ndkBuild {
189+ path " $projectDir /src/main/jni/Android.mk"
190+ }
191+ }
192+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
193+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
194+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
195+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
196+ into(" $buildDir /react-ndk/exported" )
197+ }
198+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
199+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
200+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
201+ into(" $buildDir /react-ndk/exported" )
202+ }
203+ afterEvaluate {
204+ // If you wish to add a custom TurboModule or component locally,
205+ // you should uncomment this line.
206+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
207+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
208+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
209+
210+ // Due to a bug inside AGP, we have to explicitly set a dependency
211+ // between configureNdkBuild* tasks and the preBuild tasks.
212+ // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
213+ configureNdkBuildRelease. dependsOn(preReleaseBuild)
214+ configureNdkBuildDebug. dependsOn(preDebugBuild)
215+ reactNativeArchitectures(). each { architecture ->
216+ tasks. findByName(" configureNdkBuildDebug[${ architecture} ]" )?. configure {
217+ dependsOn(" preDebugBuild" )
218+ }
219+ tasks. findByName(" configureNdkBuildRelease[${ architecture} ]" )?. configure {
220+ dependsOn(" preReleaseBuild" )
221+ }
222+ }
223+ }
147224 }
225+
148226 splits {
149227 abi {
150228 reset()
@@ -238,3 +316,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
238316}
239317
240318apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
319+
320+ def isNewArchitectureEnabled () {
321+ // To opt-in for the New Architecture, you can either:
322+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
323+ // - Invoke gradle with `-newArchEnabled=true`
324+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
325+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
326+ }
0 commit comments