11apply plugin : " com.android.application"
22
33import com.android.build.OutputFile
4+ import org.apache.tools.ant.taskdefs.condition.Os
45
56/**
67 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
@@ -120,9 +121,12 @@ def jscFlavor = 'org.webkit:android-jsc:+'
120121def enableHermes = project. ext. react. get(" enableHermes" , false );
121122
122123/**
123- * Architectures to build native code for in debug .
124+ * Architectures to build native code for.
124125 */
125- 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+ }
126130
127131android {
128132 ndkVersion rootProject. ext. ndkVersion
@@ -140,6 +144,79 @@ android {
140144 targetSdkVersion rootProject. ext. targetSdkVersion
141145 versionCode 1
142146 versionName " 1.0"
147+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
148+
149+ if (isNewArchitectureEnabled()) {
150+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
151+ externalNativeBuild {
152+ ndkBuild {
153+ arguments " APP_PLATFORM=android-21" ,
154+ " APP_STL=c++_shared" ,
155+ " NDK_TOOLCHAIN_VERSION=clang" ,
156+ " GENERATED_SRC_DIR=$buildDir /generated/source" ,
157+ " PROJECT_BUILD_DIR=$buildDir " ,
158+ " REACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
159+ " REACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build"
160+ cFlags " -Wall" , " -Werror" , " -fexceptions" , " -frtti" , " -DWITH_INSPECTOR=1"
161+ cppFlags " -std=c++17"
162+ // Make sure this target name is the same you specify inside the
163+ // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
164+ targets " blob_courier_example_appmodules"
165+
166+ // Fix for windows limit on number of character in file paths and in command lines
167+ if (Os . isFamily(Os . FAMILY_WINDOWS )) {
168+ arguments " NDK_OUT=${ rootProject.projectDir.getParent()} \\ .cxx" ,
169+ " NDK_APP_SHORT_COMMANDS=true"
170+ }
171+ }
172+ }
173+ if (! enableSeparateBuildPerCPUArchitecture) {
174+ ndk {
175+ abiFilters (* reactNativeArchitectures())
176+ }
177+ }
178+ }
179+ }
180+
181+ if (isNewArchitectureEnabled()) {
182+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
183+ externalNativeBuild {
184+ ndkBuild {
185+ path " $projectDir /src/main/jni/Android.mk"
186+ }
187+ }
188+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
189+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
190+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
191+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
192+ into(" $buildDir /react-ndk/exported" )
193+ }
194+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
195+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
196+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
197+ into(" $buildDir /react-ndk/exported" )
198+ }
199+ afterEvaluate {
200+ // If you wish to add a custom TurboModule or component locally,
201+ // you should uncomment this line.
202+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
203+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
204+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
205+
206+ // Due to a bug inside AGP, we have to explicitly set a dependency
207+ // between configureNdkBuild* tasks and the preBuild tasks.
208+ // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
209+ configureNdkBuildRelease. dependsOn(preReleaseBuild)
210+ configureNdkBuildDebug. dependsOn(preDebugBuild)
211+ reactNativeArchitectures(). each { architecture ->
212+ tasks. findByName(" configureNdkBuildDebug[${ architecture} ]" )?. configure {
213+ dependsOn(" preDebugBuild" )
214+ }
215+ tasks. findByName(" configureNdkBuildRelease[${ architecture} ]" )?. configure {
216+ dependsOn(" preReleaseBuild" )
217+ }
218+ }
219+ }
143220 }
144221
145222 lintOptions {
@@ -152,7 +229,7 @@ android {
152229 reset()
153230 enable enableSeparateBuildPerCPUArchitecture
154231 universalApk false // If true, also generate a universal APK
155- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
232+ include ( * reactNativeArchitectures())
156233 }
157234 }
158235 signingConfigs {
@@ -166,11 +243,6 @@ android {
166243 buildTypes {
167244 debug {
168245 signingConfig signingConfigs. debug
169- if (nativeArchitectures) {
170- ndk {
171- abiFilters nativeArchitectures. split(' ,' )
172- }
173- }
174246 }
175247 release {
176248 // Caution! In production, you need to generate your own keystore file.
@@ -202,6 +274,7 @@ dependencyLocking {
202274
203275dependencies {
204276 implementation fileTree(dir : " libs" , include : [" *.jar" ])
277+
205278 // noinspection GradleDynamicVersion
206279 implementation " com.facebook.react:react-native:+" // From node_modules
207280
@@ -228,6 +301,18 @@ dependencies {
228301 implementation project(' :blob_courier' )
229302}
230303
304+ if (isNewArchitectureEnabled()) {
305+ // If new architecture is enabled, we let you build RN from source
306+ // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
307+ // This will be applied to all the imported transtitive dependency.
308+ configurations. all {
309+ resolutionStrategy. dependencySubstitution {
310+ substitute(module(" com.facebook.react:react-native" ))
311+ .using(project(" :ReactAndroid" )). because(" On New Architecture we're building React Native from source" )
312+ }
313+ }
314+ }
315+
231316// Run this once to be able to run the application with BUCK
232317// puts all compile dependencies into folder libs for BUCK to use
233318task copyDownloadableDepsToLibs (type : Copy ) {
@@ -236,3 +321,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
236321}
237322
238323apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
324+
325+ def isNewArchitectureEnabled () {
326+ // To opt-in for the New Architecture, you can either:
327+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
328+ // - Invoke gradle with `-newArchEnabled=true`
329+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
330+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
331+ }
0 commit comments