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
@@ -122,9 +123,12 @@ def jscFlavor = 'org.webkit:android-jsc:+'
122123def enableHermes = project. ext. react. get(" enableHermes" , false );
123124
124125/**
125- * Architectures to build native code for in debug .
126+ * Architectures to build native code for.
126127 */
127- def nativeArchitectures = project. getProperties(). get(" reactNativeDebugArchitectures" )
128+ def reactNativeArchitectures () {
129+ def value = project. getProperties(). get(" reactNativeArchitectures" )
130+ return value ? value. split(" ," ) : [" armeabi-v7a" , " x86" , " x86_64" , " arm64-v8a" ]
131+ }
128132
129133android {
130134 ndkVersion rootProject. ext. ndkVersion
@@ -137,20 +141,85 @@ android {
137141 targetSdkVersion rootProject. ext. targetSdkVersion
138142 versionCode 1
139143 versionName " 1.0"
140- multiDexEnabled true
144+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
145+
146+ if (isNewArchitectureEnabled()) {
147+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
148+ externalNativeBuild {
149+ ndkBuild {
150+ arguments " APP_PLATFORM=android-21" ,
151+ " APP_STL=c++_shared" ,
152+ " NDK_TOOLCHAIN_VERSION=clang" ,
153+ " GENERATED_SRC_DIR=$buildDir /generated/source" ,
154+ " PROJECT_BUILD_DIR=$buildDir " ,
155+ " REACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
156+ " REACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build"
157+ cFlags " -Wall" , " -Werror" , " -fexceptions" , " -frtti" , " -DWITH_INSPECTOR=1"
158+ cppFlags " -std=c++17"
159+ // Make sure this target name is the same you specify inside the
160+ // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
161+ targets " reactnativestarterapp_appmodules"
162+
163+ // Fix for windows limit on number of character in file paths and in command lines
164+ if (Os . isFamily(Os . FAMILY_WINDOWS )) {
165+ arguments " NDK_APP_SHORT_COMMANDS=true"
166+ }
167+ }
168+ }
169+ if (! enableSeparateBuildPerCPUArchitecture) {
170+ ndk {
171+ abiFilters (* reactNativeArchitectures())
172+ }
173+ }
174+ }
141175 }
142176
143- dexOptions {
144- incremental true
145- javaMaxHeapSize " 4g"
177+ if (isNewArchitectureEnabled()) {
178+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
179+ externalNativeBuild {
180+ ndkBuild {
181+ path " $projectDir /src/main/jni/Android.mk"
182+ }
183+ }
184+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
185+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
186+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
187+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
188+ into(" $buildDir /react-ndk/exported" )
189+ }
190+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
191+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
192+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
193+ into(" $buildDir /react-ndk/exported" )
194+ }
195+ afterEvaluate {
196+ // If you wish to add a custom TurboModule or component locally,
197+ // you should uncomment this line.
198+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
199+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
200+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
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+ }
146215 }
147216
148217 splits {
149218 abi {
150219 reset()
151220 enable enableSeparateBuildPerCPUArchitecture
152221 universalApk true // If true, also generate a universal APK
153- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
222+ include ( * reactNativeArchitectures())
154223 }
155224 }
156225 signingConfigs {
@@ -164,11 +233,6 @@ android {
164233 buildTypes {
165234 debug {
166235 signingConfig signingConfigs. debug
167- if (nativeArchitectures) {
168- ndk {
169- abiFilters nativeArchitectures. split(' ,' )
170- }
171- }
172236 }
173237 release {
174238 // Caution! In production, you need to generate your own keystore file.
@@ -198,8 +262,7 @@ android {
198262
199263dependencies {
200264 implementation fileTree(dir : " libs" , include : [" *.jar" ])
201- implementation project(' :react-native-splash-screen' )
202- // implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
265+
203266 // noinspection GradleDynamicVersion
204267 implementation " com.facebook.react:react-native:+" // From node_modules
205268
@@ -217,6 +280,8 @@ dependencies {
217280 // For WebP support, without animations
218281 implementation ' com.facebook.fresco:webpsupport:2.6.0'
219282
283+ implementation project(' :react-native-splash-screen' )
284+
220285 debugImplementation(" com.facebook.flipper:flipper:${ FLIPPER_VERSION} " ) {
221286 exclude group :' com.facebook.fbjni'
222287 }
@@ -239,6 +304,18 @@ dependencies {
239304 }
240305}
241306
307+ if (isNewArchitectureEnabled()) {
308+ // If new architecture is enabled, we let you build RN from source
309+ // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
310+ // This will be applied to all the imported transtitive dependency.
311+ configurations. all {
312+ resolutionStrategy. dependencySubstitution {
313+ substitute(module(" com.facebook.react:react-native" ))
314+ .using(project(" :ReactAndroid" )). because(" On New Architecture we're building React Native from source" )
315+ }
316+ }
317+ }
318+
242319// Run this once to be able to run the application with BUCK
243320// puts all compile dependencies into folder libs for BUCK to use
244321task copyDownloadableDepsToLibs (type : Copy ) {
@@ -247,3 +324,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
247324}
248325
249326apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
327+
328+ def isNewArchitectureEnabled () {
329+ // To opt-in for the New Architecture, you can either:
330+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
331+ // - Invoke gradle with `-newArchEnabled=true`
332+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
333+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
334+ }
0 commit comments