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
@@ -78,7 +79,7 @@ import com.android.build.OutputFile
7879 */
7980
8081project. ext. react = [
81- enableHermes : false , // clean and rebuild if changing
82+ enableHermes : true , // clean and rebuild if changing
8283]
8384
8485apply from : " ../../node_modules/react-native/react.gradle"
@@ -114,12 +115,20 @@ 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
124+ /**
125+ * Architectures to build native code for.
126+ */
127+ def reactNativeArchitectures () {
128+ def value = project. getProperties(). get(" reactNativeArchitectures" )
129+ return value ? value. split(" ," ) : [" armeabi-v7a" , " x86" , " x86_64" , " arm64-v8a" ]
130+ }
131+
123132android {
124133 ndkVersion rootProject. ext. ndkVersion
125134
@@ -131,13 +140,74 @@ android {
131140 targetSdkVersion rootProject. ext. targetSdkVersion
132141 versionCode 1
133142 versionName " 1.0"
143+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
144+
145+ if (isNewArchitectureEnabled()) {
146+ // We configure the CMake build only if you decide to opt-in for the New Architecture.
147+ externalNativeBuild {
148+ cmake {
149+ arguments " -DPROJECT_BUILD_DIR=$buildDir " ,
150+ " -DREACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
151+ " -DREACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build" ,
152+ " -DNODE_MODULES_DIR=$rootDir /../node_modules" ,
153+ " -DANDROID_STL=c++_shared"
154+ }
155+ }
156+ if (! enableSeparateBuildPerCPUArchitecture) {
157+ ndk {
158+ abiFilters (* reactNativeArchitectures())
159+ }
160+ }
161+ }
162+ }
163+
164+ if (isNewArchitectureEnabled()) {
165+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
166+ externalNativeBuild {
167+ cmake {
168+ path " $projectDir /src/main/jni/CMakeLists.txt"
169+ }
170+ }
171+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
172+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
173+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
174+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
175+ into(" $buildDir /react-ndk/exported" )
176+ }
177+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
178+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
179+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
180+ into(" $buildDir /react-ndk/exported" )
181+ }
182+ afterEvaluate {
183+ // If you wish to add a custom TurboModule or component locally,
184+ // you should uncomment this line.
185+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
186+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
187+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
188+
189+ // Due to a bug inside AGP, we have to explicitly set a dependency
190+ // between configureCMakeDebug* tasks and the preBuild tasks.
191+ // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
192+ configureCMakeRelWithDebInfo. dependsOn(preReleaseBuild)
193+ configureCMakeDebug. dependsOn(preDebugBuild)
194+ reactNativeArchitectures(). each { architecture ->
195+ tasks. findByName(" configureCMakeDebug[${ architecture} ]" )?. configure {
196+ dependsOn(" preDebugBuild" )
197+ }
198+ tasks. findByName(" configureCMakeRelWithDebInfo[${ architecture} ]" )?. configure {
199+ dependsOn(" preReleaseBuild" )
200+ }
201+ }
202+ }
134203 }
204+
135205 splits {
136206 abi {
137207 reset()
138208 enable enableSeparateBuildPerCPUArchitecture
139209 universalApk false // If true, also generate a universal APK
140- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
210+ include ( * reactNativeArchitectures())
141211 }
142212 }
143213 signingConfigs {
@@ -180,13 +250,14 @@ android {
180250
181251dependencies {
182252 implementation fileTree(dir : " libs" , include : [" *.jar" ])
253+
183254 // noinspection GradleDynamicVersion
184255 implementation " com.facebook.react:react-native:+" // From node_modules
185256
186257 implementation " androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
187258
188259 debugImplementation(" com.facebook.flipper:flipper:${ FLIPPER_VERSION} " ) {
189- exclude group :' com.facebook.fbjni'
260+ exclude group :' com.facebook.fbjni'
190261 }
191262
192263 debugImplementation(" com.facebook.flipper:flipper-network-plugin:${ FLIPPER_VERSION} " ) {
@@ -199,14 +270,31 @@ dependencies {
199270 }
200271
201272 if (enableHermes) {
202- def hermesPath = " ../../node_modules/hermes-engine/android/" ;
203- debugImplementation files(hermesPath + " hermes-debug.aar" )
204- releaseImplementation files(hermesPath + " hermes-release.aar" )
273+ // noinspection GradleDynamicVersion
274+ implementation(" com.facebook.react:hermes-engine:+" ) { // From node_modules
275+ exclude group :' com.facebook.fbjni'
276+ }
205277 } else {
206278 implementation jscFlavor
207279 }
208280}
209281
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" ))
290+ .because(" On New Architecture we're building React Native from source" )
291+ substitute(module(" com.facebook.react:hermes-engine" ))
292+ .using(project(" :ReactAndroid:hermes-engine" ))
293+ .because(" On New Architecture we're building Hermes from source" )
294+ }
295+ }
296+ }
297+
210298// Run this once to be able to run the application with BUCK
211299// puts all compile dependencies into folder libs for BUCK to use
212300task copyDownloadableDepsToLibs (type : Copy ) {
@@ -215,3 +303,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
215303}
216304
217305apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
306+
307+ def isNewArchitectureEnabled () {
308+ // To opt-in for the New Architecture, you can either:
309+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
310+ // - Invoke gradle with `-newArchEnabled=true`
311+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
312+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
313+ }
0 commit comments