1
1
apply plugin : " com.android.application"
2
2
3
3
import com.android.build.OutputFile
4
+ import org.apache.tools.ant.taskdefs.condition.Os
4
5
5
6
/**
6
7
* The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
@@ -20,7 +21,7 @@ import com.android.build.OutputFile
20
21
* // default. Can be overridden with ENTRY_FILE environment variable.
21
22
* entryFile: "index.android.js",
22
23
*
23
- * // https://facebook.github.io/react-native /docs/performance#enable-the-ram-format
24
+ * // https://reactnative.dev /docs/performance#enable-the-ram-format
24
25
* bundleCommand: "ram-bundle",
25
26
*
26
27
* // whether to bundle JS and assets in debug mode
@@ -114,33 +115,111 @@ def jscFlavor = 'org.webkit:android-jsc:+'
114
115
/**
115
116
* Whether to enable the Hermes VM.
116
117
*
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
118
119
* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
119
120
* and the benefits of using Hermes will therefore be sharply reduced.
120
121
*/
121
122
def enableHermes = project. ext. react. get(" enableHermes" , false );
122
123
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
+
123
132
android {
124
- compileSdkVersion rootProject. ext. compileSdkVersion
133
+ ndkVersion rootProject. ext. ndkVersion
125
134
126
- compileOptions {
127
- sourceCompatibility JavaVersion . VERSION_1_8
128
- targetCompatibility JavaVersion . VERSION_1_8
129
- }
135
+ compileSdkVersion rootProject. ext. compileSdkVersion
130
136
131
137
defaultConfig {
132
138
applicationId " com.reactnativeintersectionobserver"
133
139
minSdkVersion rootProject. ext. minSdkVersion
134
140
targetSdkVersion rootProject. ext. targetSdkVersion
135
141
versionCode 1
136
142
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 " reactnativeintersectionobserver_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
+ }
137
174
}
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
+
138
217
splits {
139
218
abi {
140
219
reset()
141
220
enable enableSeparateBuildPerCPUArchitecture
142
221
universalApk false // If true, also generate a universal APK
143
- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
222
+ include ( * reactNativeArchitectures())
144
223
}
145
224
}
146
225
signingConfigs {
@@ -157,30 +236,24 @@ android {
157
236
}
158
237
release {
159
238
// Caution! In production, you need to generate your own keystore file.
160
- // see https://facebook.github.io/react-native /docs/signed-apk-android.
239
+ // see https://reactnative.dev /docs/signed-apk-android.
161
240
signingConfig signingConfigs. debug
162
241
minifyEnabled enableProguardInReleaseBuilds
163
242
proguardFiles getDefaultProguardFile(" proguard-android.txt" ), " proguard-rules.pro"
164
243
}
165
244
}
166
245
167
- packagingOptions {
168
- pickFirst " lib/armeabi-v7a/libc++_shared.so"
169
- pickFirst " lib/arm64-v8a/libc++_shared.so"
170
- pickFirst " lib/x86/libc++_shared.so"
171
- pickFirst " lib/x86_64/libc++_shared.so"
172
- }
173
-
174
246
// applicationVariants are e.g. debug, release
175
247
applicationVariants. all { variant ->
176
248
variant. outputs. each { output ->
177
249
// For each separate APK per architecture, set a unique version code as described here:
178
250
// https://developer.android.com/studio/build/configure-apk-splits.html
251
+ // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
179
252
def versionCodes = [" armeabi-v7a" : 1 , " x86" : 2 , " arm64-v8a" : 3 , " x86_64" : 4 ]
180
253
def abi = output. getFilter(OutputFile . ABI )
181
254
if (abi != null ) { // null for the universal-debug, universal-release variants
182
255
output. versionCodeOverride =
183
- versionCodes . get(abi) * 1048576 + defaultConfig . versionCode
256
+ defaultConfig . versionCode * 1000 + versionCodes . get(abi)
184
257
}
185
258
186
259
}
@@ -189,17 +262,19 @@ android {
189
262
190
263
dependencies {
191
264
implementation fileTree(dir : " libs" , include : [" *.jar" ])
265
+
192
266
// noinspection GradleDynamicVersion
193
267
implementation " com.facebook.react:react-native:+" // From node_modules
194
268
195
269
implementation " androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
196
270
197
271
debugImplementation(" com.facebook.flipper:flipper:${ FLIPPER_VERSION} " ) {
198
- exclude group :' com.facebook.fbjni'
272
+ exclude group :' com.facebook.fbjni'
199
273
}
200
274
201
275
debugImplementation(" com.facebook.flipper:flipper-network-plugin:${ FLIPPER_VERSION} " ) {
202
276
exclude group :' com.facebook.flipper'
277
+ exclude group :' com.squareup.okhttp3' , module :' okhttp'
203
278
}
204
279
205
280
debugImplementation(" com.facebook.flipper:flipper-fresco-plugin:${ FLIPPER_VERSION} " ) {
@@ -215,11 +290,31 @@ dependencies {
215
290
}
216
291
}
217
292
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
+
218
305
// Run this once to be able to run the application with BUCK
219
306
// puts all compile dependencies into folder libs for BUCK to use
220
307
task copyDownloadableDepsToLibs (type : Copy ) {
221
- from configurations. compile
308
+ from configurations. implementation
222
309
into ' libs'
223
310
}
224
311
225
312
apply 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