@@ -113,33 +113,99 @@ def jscFlavor = 'org.webkit:android-jsc:+'
113113/**
114114 * Whether to enable the Hermes VM.
115115 *
116- * This should be set on project.ext.react and mirrored here. If it is not set
116+ * This should be set on project.ext.react and that value will be read here. If it is not set
117117 * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
118118 * and the benefits of using Hermes will therefore be sharply reduced.
119119 */
120120def enableHermes = project. ext. react. get(" enableHermes" , false );
121121
122+ /**
123+ * Architectures to build native code for.
124+ */
125+ def reactNativeArchitectures () {
126+ def value = project. getProperties(). get(" reactNativeArchitectures" )
127+ return value ? value. split(" ," ) : [" armeabi-v7a" , " x86" , " x86_64" , " arm64-v8a" ]
128+ }
129+
122130android {
131+ ndkVersion rootProject. ext. ndkVersion
123132 compileSdkVersion rootProject. ext. compileSdkVersion
124133
125- compileOptions {
126- sourceCompatibility JavaVersion . VERSION_1_8
127- targetCompatibility JavaVersion . VERSION_1_8
128- }
129-
130134 defaultConfig {
131135 applicationId " com.example.reactnativeidscansdk"
132136 minSdkVersion rootProject. ext. minSdkVersion
133137 targetSdkVersion rootProject. ext. targetSdkVersion
134138 versionCode 1
135139 versionName " 1.0"
140+ buildConfigField " boolean" , " IS_NEW_ARCHITECTURE_ENABLED" , isNewArchitectureEnabled(). toString()
141+
142+ if (isNewArchitectureEnabled()) {
143+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
144+ externalNativeBuild {
145+ ndkBuild {
146+ arguments " APP_PLATFORM=android-21" ,
147+ " APP_STL=c++_shared" ,
148+ " NDK_TOOLCHAIN_VERSION=clang" ,
149+ " GENERATED_SRC_DIR=$buildDir /generated/source" ,
150+ " PROJECT_BUILD_DIR=$buildDir " ,
151+ " REACT_ANDROID_DIR=$rootDir /../node_modules/react-native/ReactAndroid" ,
152+ " REACT_ANDROID_BUILD_DIR=$rootDir /../node_modules/react-native/ReactAndroid/build"
153+ cFlags " -Wall" , " -Werror" , " -fexceptions" , " -frtti" , " -DWITH_INSPECTOR=1"
154+ cppFlags " -std=c++17"
155+ // Make sure this target name is the same you specify inside the
156+ // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
157+ targets " rndiffapp_appmodules"
158+ }
159+ }
160+ }
161+ }
162+
163+ if (isNewArchitectureEnabled()) {
164+ // We configure the NDK build only if you decide to opt-in for the New Architecture.
165+ externalNativeBuild {
166+ ndkBuild {
167+ path " $projectDir /src/main/jni/Android.mk"
168+ }
169+ }
170+ def reactAndroidProjectDir = project(' :ReactAndroid' ). projectDir
171+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
172+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
173+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
174+ into(" $buildDir /react-ndk/exported" )
175+ }
176+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
177+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
178+ from(" $reactAndroidProjectDir /src/main/jni/prebuilt/lib" )
179+ into(" $buildDir /react-ndk/exported" )
180+ }
181+ afterEvaluate {
182+ // If you wish to add a custom TurboModule or component locally,
183+ // you should uncomment this line.
184+ // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
185+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
186+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
187+ // Due to a bug inside AGP, we have to explicitly set a dependency
188+ // between configureNdkBuild* tasks and the preBuild tasks.
189+ // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
190+ configureNdkBuildRelease. dependsOn(preReleaseBuild)
191+ configureNdkBuildDebug. dependsOn(preDebugBuild)
192+ reactNativeArchitectures(). each { architecture ->
193+ tasks. findByName(" configureNdkBuildDebug[${ architecture} ]" )?. configure {
194+ dependsOn(" preDebugBuild" )
195+ }
196+ tasks. findByName(" configureNdkBuildRelease[${ architecture} ]" )?. configure {
197+ dependsOn(" preReleaseBuild" )
198+ }
199+ }
200+ }
136201 }
202+
137203 splits {
138204 abi {
139205 reset()
140206 enable enableSeparateBuildPerCPUArchitecture
141207 universalApk false // If true, also generate a universal APK
142- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
208+ include ( * reactNativeArchitectures())
143209 }
144210 }
145211 signingConfigs {
@@ -167,11 +233,12 @@ android {
167233 variant. outputs. each { output ->
168234 // For each separate APK per architecture, set a unique version code as described here:
169235 // https://developer.android.com/studio/build/configure-apk-splits.html
236+ // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
170237 def versionCodes = [" armeabi-v7a" : 1 , " x86" : 2 , " arm64-v8a" : 3 , " x86_64" : 4 ]
171238 def abi = output. getFilter(OutputFile . ABI )
172239 if (abi != null ) { // null for the universal-debug, universal-release variants
173240 output. versionCodeOverride =
174- versionCodes . get(abi) * 1048576 + defaultConfig . versionCode
241+ defaultConfig . versionCode * 1000 + versionCodes . get(abi)
175242 }
176243
177244 }
@@ -186,7 +253,7 @@ dependencies {
186253
187254 implementation " androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
188255 debugImplementation(" com.facebook.flipper:flipper:${ FLIPPER_VERSION} " ) {
189- exclude group :' com.facebook.fbjni'
256+ exclude group :' com.facebook.fbjni'
190257 }
191258 debugImplementation(" com.facebook.flipper:flipper-network-plugin:${ FLIPPER_VERSION} " ) {
192259 exclude group :' com.facebook.flipper'
@@ -207,11 +274,31 @@ dependencies {
207274 implementation project(' :reactnativeidscansdk' )
208275}
209276
277+ if (isNewArchitectureEnabled()) {
278+ // If new architecture is enabled, we let you build RN from source
279+ // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
280+ // This will be applied to all the imported transtitive dependency.
281+ configurations. all {
282+ resolutionStrategy. dependencySubstitution {
283+ substitute(module(" com.facebook.react:react-native" ))
284+ .using(project(" :ReactAndroid" )). because(" On New Architecture we're building React Native from source" )
285+ }
286+ }
287+ }
288+
210289// Run this once to be able to run the application with BUCK
211290// puts all compile dependencies into folder libs for BUCK to use
212291task copyDownloadableDepsToLibs (type : Copy ) {
213- from configurations. compile
292+ from configurations. implementation
214293 into ' libs'
215294}
216295
217296apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
297+
298+ def isNewArchitectureEnabled () {
299+ // To opt-in for the New Architecture, you can either:
300+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
301+ // - Invoke gradle with `-newArchEnabled=true`
302+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
303+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
304+ }
0 commit comments