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
@@ -114,16 +115,19 @@ def jscFlavor = 'org.webkit:android-jsc:+'
114115/**
115116 * Whether to enable the Hermes VM.
116117 *
117- * This should be set on project.ext.react and mirrored here. If it is not set
118+ * This should be set on project.ext.react and that value will be read here. If it is not set
118119 * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
119120 * and the benefits of using Hermes will therefore be sharply reduced.
120121 */
121122def enableHermes = project. ext. react. get(" enableHermes" , false );
122123
123124/**
124- * Architectures to build native code for in debug .
125+ * Architectures to build native code for.
125126 */
126- def nativeArchitectures = project. getProperties(). get(" reactNativeDebugArchitectures" )
127+ def reactNativeArchitectures () {
128+ def value = project. getProperties(). get(" reactNativeArchitectures" )
129+ return value ? value. split(" ," ) : [" armeabi-v7a" , " x86" , " x86_64" , " arm64-v8a" ]
130+ }
127131
128132android {
129133 ndkVersion rootProject. ext. ndkVersion
@@ -136,13 +140,86 @@ android {
136140 targetSdkVersion rootProject. ext. targetSdkVersion
137141 versionCode 1
138142 versionName " 1.0"
143+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
144+
145+ if (isNewArchitectureEnabled()) {
146+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
147+ externalNativeBuild {
148+ ndkBuild {
149+ arguments " APP_PLATFORM=android-21" ,
150+ " APP_STL=c++_shared" ,
151+ " NDK_TOOLCHAIN_VERSION=clang" ,
152+ " GENERATED_SRC_DIR=$buildDir /generated/source" ,
153+ " PROJECT_BUILD_DIR=$buildDir " ,
154+ " REACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
155+ " REACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build"
156+ cFlags " -Wall" , " -Werror" , " -fexceptions" , " -frtti" , " -DWITH_INSPECTOR=1"
157+ cppFlags " -std=c++17"
158+ // Make sure this target name is the same you specify inside the
159+ // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
160+ targets " example_appmodules"
161+
162+ // Fix for windows limit on number of character in file paths and in command lines
163+ if (Os . isFamily(Os . FAMILY_WINDOWS )) {
164+ arguments " NDK_APP_SHORT_COMMANDS=true"
165+ }
166+ }
167+ }
168+ if (! enableSeparateBuildPerCPUArchitecture) {
169+ ndk {
170+ abiFilters (* reactNativeArchitectures())
171+ }
172+ }
173+ }
139174 }
175+
176+ if (isNewArchitectureEnabled()) {
177+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
178+ externalNativeBuild {
179+ ndkBuild {
180+ path " $projectDir /src/main/jni/Android.mk"
181+ }
182+ }
183+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
184+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
185+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
186+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
187+ into(" $buildDir /react-ndk/exported" )
188+ }
189+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
190+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
191+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
192+ into(" $buildDir /react-ndk/exported" )
193+ }
194+ afterEvaluate {
195+ // If you wish to add a custom TurboModule or component locally,
196+ // you should uncomment this line.
197+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
198+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
199+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
200+
201+ // Due to a bug inside AGP, we have to explicitly set a dependency
202+ // between configureNdkBuild* tasks and the preBuild tasks.
203+ // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
204+ configureNdkBuildRelease. dependsOn(preReleaseBuild)
205+ configureNdkBuildDebug. dependsOn(preDebugBuild)
206+ reactNativeArchitectures(). each { architecture ->
207+ tasks. findByName(" configureNdkBuildDebug[${ architecture} ]" )?. configure {
208+ dependsOn(" preDebugBuild" )
209+ }
210+ tasks. findByName(" configureNdkBuildRelease[${ architecture} ]" )?. configure {
211+ dependsOn(" preReleaseBuild" )
212+ }
213+ }
214+ }
215+ }
216+
140217 splits {
141218 abi {
142219 reset()
143220 enable enableSeparateBuildPerCPUArchitecture
144221 universalApk false // If true, also generate a universal APK
145- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
222+ include ( * reactNativeArchitectures())
146223 }
147224 }
148225 signingConfigs {
@@ -156,11 +233,6 @@ android {
156233 buildTypes {
157234 debug {
158235 signingConfig signingConfigs. debug
159- if (nativeArchitectures) {
160- ndk {
161- abiFilters nativeArchitectures. split(' ,' )
162- }
163- }
164236 }
165237 release {
166238 // Caution! In production, you need to generate your own keystore file.
@@ -190,6 +262,7 @@ android {
190262
191263dependencies {
192264 implementation fileTree(dir : " libs" , include : [" *.jar" ])
265+
193266 // noinspection GradleDynamicVersion
194267 implementation " com.facebook.react:react-native:+" // From node_modules
195268
@@ -217,6 +290,18 @@ dependencies {
217290 }
218291}
219292
293+ if (isNewArchitectureEnabled()) {
294+ // If new architecture is enabled, we let you build RN from source
295+ // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
296+ // This will be applied to all the imported transtitive dependency.
297+ configurations. all {
298+ resolutionStrategy. dependencySubstitution {
299+ substitute(module(" com.facebook.react:react-native" ))
300+ .using(project(" :ReactAndroid" )). because(" On New Architecture we're building React Native from source" )
301+ }
302+ }
303+ }
304+
220305// Run this once to be able to run the application with BUCK
221306// puts all compile dependencies into folder libs for BUCK to use
222307task copyDownloadableDepsToLibs (type : Copy ) {
@@ -225,3 +310,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
225310}
226311
227312apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
313+
314+ def isNewArchitectureEnabled () {
315+ // To opt-in for the New Architecture, you can either:
316+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
317+ // - Invoke gradle with `-newArchEnabled=true`
318+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
319+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
320+ }
0 commit comments