@@ -121,9 +121,12 @@ def jscFlavor = 'org.webkit:android-jsc:+'
121121def enableHermes = project. ext. react. get(" enableHermes" , false );
122122
123123/**
124- * Architectures to build native code for in debug .
124+ * Architectures to build native code for.
125125 */
126- 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+ }
127130
128131android {
129132 ndkVersion rootProject. ext. ndkVersion
@@ -136,13 +139,80 @@ android {
136139 targetSdkVersion rootProject. ext. targetSdkVersion
137140 versionCode 30
138141 versionName " 0.0.30"
142+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
143+ if (isNewArchitectureEnabled()) {
144+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
145+ externalNativeBuild {
146+ ndkBuild {
147+ arguments " APP_PLATFORM=android-21" ,
148+ " APP_STL=c++_shared" ,
149+ " NDK_TOOLCHAIN_VERSION=clang" ,
150+ " GENERATED_SRC_DIR=$buildDir /generated/source" ,
151+ " PROJECT_BUILD_DIR=$buildDir " ,
152+ " REACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
153+ " REACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build" ,
154+ " NODE_MODULES_DIR=$rootDir /../node_modules"
155+ cFlags " -Wall" , " -Werror" , " -fexceptions" , " -frtti" , " -DWITH_INSPECTOR=1"
156+ cppFlags " -std=c++17"
157+ // Make sure this target name is the same you specify inside the
158+ // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
159+ targets " rndiffapp_appmodules"
160+ }
161+ }
162+ if (! enableSeparateBuildPerCPUArchitecture) {
163+ ndk {
164+ abiFilters (* reactNativeArchitectures())
165+ }
166+ }
167+ }
168+ }
169+
170+ if (isNewArchitectureEnabled()) {
171+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
172+ externalNativeBuild {
173+ ndkBuild {
174+ path " $projectDir /src/main/jni/Android.mk"
175+ }
176+ }
177+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
178+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
179+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
180+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
181+ into(" $buildDir /react-ndk/exported" )
182+ }
183+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
184+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
185+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
186+ into(" $buildDir /react-ndk/exported" )
187+ }
188+ afterEvaluate {
189+ // If you wish to add a custom TurboModule or component locally,
190+ // you should uncomment this line.
191+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
192+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
193+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
194+ // Due to a bug inside AGP, we have to explicitly set a dependency
195+ // between configureNdkBuild* tasks and the preBuild tasks.
196+ // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
197+ configureNdkBuildRelease. dependsOn(preReleaseBuild)
198+ configureNdkBuildDebug. dependsOn(preDebugBuild)
199+ reactNativeArchitectures(). each { architecture ->
200+ tasks. findByName(" configureNdkBuildDebug[${ architecture} ]" )?. configure {
201+ dependsOn(" preDebugBuild" )
202+ }
203+ tasks. findByName(" configureNdkBuildRelease[${ architecture} ]" )?. configure {
204+ dependsOn(" preReleaseBuild" )
205+ }
206+ }
207+ }
139208 }
209+
140210 splits {
141211 abi {
142212 reset()
143213 enable enableSeparateBuildPerCPUArchitecture
144214 universalApk false // If true, also generate a universal APK
145- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
215+ include ( * reactNativeArchitectures())
146216 }
147217 }
148218 signingConfigs {
@@ -164,11 +234,6 @@ android {
164234 buildTypes {
165235 debug {
166236 signingConfig signingConfigs. debug
167- if (nativeArchitectures) {
168- ndk {
169- abiFilters nativeArchitectures. split(' ,' )
170- }
171- }
172237 }
173238 release {
174239 // Caution! In production, you need to generate your own keystore file.
@@ -217,14 +282,31 @@ dependencies {
217282 }
218283
219284 if (enableHermes) {
220- def hermesPath = " ../../node_modules/hermes-engine/android/" ;
221- debugImplementation files(hermesPath + " hermes-debug.aar" )
222- releaseImplementation files(hermesPath + " hermes-release.aar" )
285+ // noinspection GradleDynamicVersion
286+ implementation(" com.facebook.react:hermes-engine:+" ) { // From node_modules
287+ exclude group :' com.facebook.fbjni'
288+ }
223289 } else {
224290 implementation jscFlavor
225291 }
226292}
227293
294+ if (isNewArchitectureEnabled()) {
295+ // If new architecture is enabled, we let you build RN from source
296+ // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
297+ // This will be applied to all the imported transtitive dependency.
298+ configurations. all {
299+ resolutionStrategy. dependencySubstitution {
300+ substitute(module(" com.facebook.react:react-native" ))
301+ .using(project(" :ReactAndroid" ))
302+ .because(" On New Architecture we're building React Native from source" )
303+ substitute(module(" com.facebook.react:hermes-engine" ))
304+ .using(project(" :ReactAndroid:hermes-engine" ))
305+ .because(" On New Architecture we're building Hermes from source" )
306+ }
307+ }
308+ }
309+
228310// Run this once to be able to run the application with BUCK
229311// puts all compile dependencies into folder libs for BUCK to use
230312task copyDownloadableDepsToLibs (type : Copy ) {
@@ -234,4 +316,12 @@ task copyDownloadableDepsToLibs(type: Copy) {
234316
235317apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
236318
319+ def isNewArchitectureEnabled () {
320+ // To opt-in for the New Architecture, you can either:
321+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
322+ // - Invoke gradle with `-newArchEnabled=true`
323+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
324+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
325+ }
326+
237327apply from : " ../../node_modules/react-native-vector-icons/fonts.gradle"
0 commit comments