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
@@ -15,7 +16,9 @@ import com.android.build.OutputFile
15
16
* // the name of the generated asset file containing your JS bundle
16
17
* bundleAssetName: "index.android.bundle",
17
18
*
18
- * // the entry file for bundle generation
19
+ * // the entry file for bundle generation. If none specified and
20
+ * // "index.android.js" exists, it will be used. Otherwise "index.js" is
21
+ * // default. Can be overridden with ENTRY_FILE environment variable.
19
22
* entryFile: "index.android.js",
20
23
*
21
24
* // https://reactnative.dev/docs/performance#enable-the-ram-format
@@ -37,7 +40,7 @@ import com.android.build.OutputFile
37
40
* // bundleInBeta: true,
38
41
*
39
42
* // whether to disable dev mode in custom build variants (by default only disabled in release)
40
- * // for PusherWebsocketReactNativeExample : to disable dev mode in the staging build type (if configured)
43
+ * // for example : to disable dev mode in the staging build type (if configured)
41
44
* devDisabledInStaging: true,
42
45
* // The configuration property can be in the following formats
43
46
* // 'devDisabledIn${productFlavor}${buildType}'
@@ -64,7 +67,7 @@ import com.android.build.OutputFile
64
67
* // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
65
68
* // date; if you have any other folders that you want to ignore for performance reasons (gradle
66
69
* // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
67
- * // for PusherWebsocketReactNativeExample , you might want to remove it from here.
70
+ * // for example , you might want to remove it from here.
68
71
* inputExcludes: ["android/**", "ios/**"],
69
72
*
70
73
* // override which node gets called and with what additional arguments
@@ -76,8 +79,7 @@ import com.android.build.OutputFile
76
79
*/
77
80
78
81
project. ext. react = [
79
- enableHermes : false , // clean and rebuild if changing
80
- entryFile : " index.tsx" ,
82
+ enableHermes : true , // clean and rebuild if changing
81
83
]
82
84
83
85
apply from : " ../../node_modules/react-native/react.gradle"
@@ -100,7 +102,7 @@ def enableProguardInReleaseBuilds = false
100
102
/**
101
103
* The preferred build flavor of JavaScriptCore.
102
104
*
103
- * For PusherWebsocketReactNativeExample , to use the international variant, you can use:
105
+ * For example , to use the international variant, you can use:
104
106
* `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
105
107
*
106
108
* The international variant includes ICU i18n library and necessary data
@@ -113,35 +115,102 @@ def jscFlavor = 'org.webkit:android-jsc:+'
113
115
/**
114
116
* Whether to enable the Hermes VM.
115
117
*
116
- * 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
117
119
* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
118
120
* and the benefits of using Hermes will therefore be sharply reduced.
119
121
*/
120
122
def enableHermes = project. ext. react. get(" enableHermes" , false );
121
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
+
122
132
android {
123
- compileSdkVersion rootProject. ext. compileSdkVersion
133
+ ndkVersion rootProject. ext. ndkVersion
124
134
125
- compileOptions {
126
- sourceCompatibility JavaVersion . VERSION_1_8
127
- targetCompatibility JavaVersion . VERSION_1_8
128
- }
135
+ compileSdkVersion rootProject. ext. compileSdkVersion
129
136
130
137
defaultConfig {
131
138
applicationId " com.example.pusherwebsocketreactnative"
132
139
minSdkVersion rootProject. ext. minSdkVersion
133
140
targetSdkVersion rootProject. ext. targetSdkVersion
134
141
versionCode 1
135
142
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
+ }
136
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
+ }
203
+ }
204
+
137
205
splits {
138
206
abi {
139
207
reset()
140
208
enable enableSeparateBuildPerCPUArchitecture
141
209
universalApk false // If true, also generate a universal APK
142
- include " armeabi-v7a " , " x86 " , " arm64-v8a " , " x86_64 "
210
+ include ( * reactNativeArchitectures())
143
211
}
144
212
}
213
+ namespace ' com.example.pusherwebsocketreactnative'
145
214
signingConfigs {
146
215
debug {
147
216
storeFile file(' debug.keystore' )
@@ -150,7 +219,6 @@ android {
150
219
keyPassword ' android'
151
220
}
152
221
}
153
- namespace ' com.example.pusherwebsocketreactnative'
154
222
buildTypes {
155
223
debug {
156
224
signingConfig signingConfigs. debug
@@ -163,16 +231,18 @@ android {
163
231
proguardFiles getDefaultProguardFile(" proguard-android.txt" ), " proguard-rules.pro"
164
232
}
165
233
}
234
+
166
235
// applicationVariants are e.g. debug, release
167
236
applicationVariants. all { variant ->
168
237
variant. outputs. each { output ->
169
238
// For each separate APK per architecture, set a unique version code as described here:
170
239
// https://developer.android.com/studio/build/configure-apk-splits.html
240
+ // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
171
241
def versionCodes = [" armeabi-v7a" : 1 , " x86" : 2 , " arm64-v8a" : 3 , " x86_64" : 4 ]
172
242
def abi = output. getFilter(OutputFile . ABI )
173
243
if (abi != null ) { // null for the universal-debug, universal-release variants
174
244
output. versionCodeOverride =
175
- versionCodes . get(abi) * 1048576 + defaultConfig . versionCode
245
+ defaultConfig . versionCode * 1000 + versionCodes . get(abi)
176
246
}
177
247
178
248
}
@@ -181,33 +251,52 @@ android {
181
251
182
252
dependencies {
183
253
implementation fileTree(dir : " libs" , include : [" *.jar" ])
254
+
184
255
// noinspection GradleDynamicVersion
185
256
implementation " com.facebook.react:react-native:+" // From node_modules
186
257
187
-
188
258
implementation ' androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
259
+
189
260
debugImplementation(" com.facebook.flipper:flipper:${ FLIPPER_VERSION} " ) {
190
- exclude group :' com.facebook.fbjni'
261
+ exclude group :' com.facebook.fbjni'
191
262
}
263
+
192
264
debugImplementation(" com.facebook.flipper:flipper-network-plugin:${ FLIPPER_VERSION} " ) {
193
265
exclude group :' com.facebook.flipper'
194
266
exclude group :' com.squareup.okhttp3' , module :' okhttp'
195
267
}
268
+
196
269
debugImplementation(" com.facebook.flipper:flipper-fresco-plugin:${ FLIPPER_VERSION} " ) {
197
270
exclude group :' com.facebook.flipper'
198
271
}
199
272
200
273
if (enableHermes) {
201
- def hermesPath = " ../../node_modules/hermes-engine/android/" ;
202
- debugImplementation files(hermesPath + " hermes-debug.aar" )
203
- releaseImplementation files(hermesPath + " hermes-release.aar" )
274
+ // noinspection GradleDynamicVersion
275
+ implementation(" com.facebook.react:hermes-engine:+" ) { // From node_modules
276
+ exclude group :' com.facebook.fbjni'
277
+ }
204
278
} else {
205
279
implementation jscFlavor
206
280
}
207
-
208
281
implementation project(' :pusher-websocket-react-native' )
209
282
}
210
283
284
+ if (isNewArchitectureEnabled()) {
285
+ // If new architecture is enabled, we let you build RN from source
286
+ // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
287
+ // This will be applied to all the imported transtitive dependency.
288
+ configurations. all {
289
+ resolutionStrategy. dependencySubstitution {
290
+ substitute(module(" com.facebook.react:react-native" ))
291
+ .using(project(" :ReactAndroid" ))
292
+ .because(" On New Architecture we're building React Native from source" )
293
+ substitute(module(" com.facebook.react:hermes-engine" ))
294
+ .using(project(" :ReactAndroid:hermes-engine" ))
295
+ .because(" On New Architecture we're building Hermes from source" )
296
+ }
297
+ }
298
+ }
299
+
211
300
// Run this once to be able to run the application with BUCK
212
301
// puts all compile dependencies into folder libs for BUCK to use
213
302
task copyDownloadableDepsToLibs (type : Copy ) {
@@ -216,3 +305,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
216
305
}
217
306
218
307
apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle" ); applyNativeModulesAppBuildGradle(project)
308
+
309
+ def isNewArchitectureEnabled () {
310
+ // To opt-in for the New Architecture, you can either:
311
+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
312
+ // - Invoke gradle with `-newArchEnabled=true`
313
+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
314
+ return project. hasProperty(" newArchEnabled" ) && project. newArchEnabled == " true"
315
+ }
0 commit comments