@@ -6,17 +6,17 @@ buildscript {
6
6
7
7
plugins {
8
8
id(" com.android.application" )
9
- id(" kotlin- android" )
10
- id(" kotlin- kapt" )
9
+ id(" org.jetbrains. kotlin. android" ) version " ${ kotlinVersion } "
10
+ id(" org.jetbrains. kotlin. kapt" ) version " ${ kotlinVersion } "
11
11
}
12
12
13
13
// `react-native run-android` is hard-coded to look for the output APK at a very
14
14
// specific location. See
15
15
// https://github.com/react-native-community/cli/blob/6cf12b00c02aca6d4bc843446394331d71a9749e/packages/platform-android/src/commands/runAndroid/index.ts#L180
16
16
buildDir = " ${ rootDir} /${ name} /build"
17
17
18
- def reactNativeDir = findNodeModulesPath(rootDir, " react-native" )
19
- def reactNativeVersion = getReactNativeVersionNumber( rootDir)
18
+ def reactNativeDir = findNodeModulesPath(" react-native" , rootDir )
19
+ def reactNativeVersion = getPackageVersionNumber( " react-native " , rootDir)
20
20
21
21
repositories {
22
22
maven {
@@ -51,21 +51,23 @@ apply(from: "${projectDir}/../../test-app.gradle")
51
51
applyTestAppModule(project)
52
52
53
53
project. ext. react = [
54
- appName : getAppName(),
55
- applicationId : getApplicationId(),
56
- enableFabric : isFabricEnabled(rootDir),
57
- enableFlipper : getFlipperVersion(rootDir),
58
- enableHermes : true ,
54
+ abiSplit : false ,
55
+ appName : getAppName(),
56
+ applicationId : getApplicationId(),
57
+ architectures : [" arm64-v8a" , " armeabi-v7a" , " x86" , " x86_64" ],
58
+ enableFabric : isFabricEnabled(project),
59
+ enableFlipper : getFlipperVersion(rootDir),
60
+ enableHermes : true ,
61
+ enableNewArchitecture : isNewArchitectureEnabled(project),
59
62
]
60
63
61
64
project. ext. signingConfigs = getSigningConfigs()
62
65
63
66
android {
64
67
compileSdkVersion project. ext. compileSdkVersion
65
68
66
- // We need only set `ndkVersion` when building react-native from source.
67
- if (hasProperty(" ANDROID_NDK_VERSION" )) {
68
- ndkVersion ANDROID_NDK_VERSION
69
+ if (project. hasProperty(" ndkVersion" )) {
70
+ ndkVersion project. ext. ndkVersion
69
71
}
70
72
71
73
// TODO: Remove this block when minSdkVersion >= 24. See
@@ -110,6 +112,69 @@ android {
110
112
resValue " string" , " app_name" , project. ext. react. appName
111
113
112
114
testInstrumentationRunner " androidx.test.runner.AndroidJUnitRunner"
115
+
116
+ if (project. ext. react. enableNewArchitecture) {
117
+ externalNativeBuild {
118
+ ndkBuild {
119
+ arguments " APP_PLATFORM=android-${ project.ext.minSdkVersion} " ,
120
+ " APP_STL=c++_shared" ,
121
+ " NDK_TOOLCHAIN_VERSION=clang" ,
122
+ " GENERATED_SRC_DIR=${ buildDir} /generated/source" ,
123
+ " NODE_MODULES_DIR=${ reactNativeDir} /.." ,
124
+ " PROJECT_BUILD_DIR=${ buildDir} " ,
125
+ " REACT_ANDROID_DIR=${ reactNativeDir} /ReactAndroid" ,
126
+ " REACT_ANDROID_BUILD_DIR=${ reactNativeDir} /ReactAndroid/build"
127
+ cFlags " -Wall" , " -Werror" , " -frtti" , " -fexceptions" , " -DWITH_INSPECTOR=1"
128
+ cppFlags " -std=c++17"
129
+ targets " reacttestapp_appmodules"
130
+ }
131
+ }
132
+ if (! project. ext. react. abiSplit) {
133
+ ndk {
134
+ abiFilters(* project. ext. react. architectures)
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ if (project. ext. react. enableNewArchitecture) {
141
+ externalNativeBuild {
142
+ ndkBuild {
143
+ path " ${ projectDir} /src/main/jni/Android.mk"
144
+ }
145
+ }
146
+
147
+ def reactAndroidProjectDir = project(" :ReactAndroid" ). projectDir
148
+ def packageReactNdkDebugLibs = tasks. register(" packageReactNdkDebugLibs" , Copy ) {
149
+ dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck" )
150
+ from(" ${ reactAndroidProjectDir} /src/main/jni/prebuilt/lib" )
151
+ into(" ${ buildDir} /react-ndk/exported" )
152
+ }
153
+ def packageReactNdkReleaseLibs = tasks. register(" packageReactNdkReleaseLibs" , Copy ) {
154
+ dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck" )
155
+ from(" ${ reactAndroidProjectDir} /src/main/jni/prebuilt/lib" )
156
+ into(" ${ buildDir} /react-ndk/exported" )
157
+ }
158
+
159
+ afterEvaluate {
160
+ preDebugBuild. dependsOn(packageReactNdkDebugLibs)
161
+ preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
162
+
163
+ // Due to a bug in AGP, we have to explicitly set a dependency
164
+ // between configureNdkBuild* tasks and the preBuild tasks. This can
165
+ // be removed once this issue is resolved:
166
+ // https://issuetracker.google.com/issues/207403732
167
+ configureNdkBuildRelease. dependsOn(preReleaseBuild)
168
+ configureNdkBuildDebug. dependsOn(preDebugBuild)
169
+ project. ext. react. architectures. each { architecture ->
170
+ tasks. findByName(" configureNdkBuildDebug[${ architecture} ]" )?. configure {
171
+ dependsOn(" preDebugBuild" )
172
+ }
173
+ tasks. findByName(" configureNdkBuildRelease[${ architecture} ]" )?. configure {
174
+ dependsOn(" preReleaseBuild" )
175
+ }
176
+ }
177
+ }
113
178
}
114
179
115
180
lintOptions {
@@ -158,6 +223,13 @@ android {
158
223
main. java. srcDirs + = " src/no-fabric/java"
159
224
}
160
225
226
+ // TODO: Remove this block when we drop support for 0.65.
227
+ if (project. ext. react. enableNewArchitecture) {
228
+ main. java. srcDirs + = " src/turbomodule/java"
229
+ } else {
230
+ main. java. srcDirs + = " src/no-turbomodule/java"
231
+ }
232
+
161
233
// TODO: Remove this block when we drop support for 0.67.
162
234
// https://github.com/facebook/react-native/commit/ce74aa4ed335d4c36ce722d47937b582045e05c4
163
235
if (reactNativeVersion < 6800 ) {
@@ -166,6 +238,15 @@ android {
166
238
main. java. srcDirs + = " src/reactinstanceeventlistener-0.68/java"
167
239
}
168
240
}
241
+
242
+ splits {
243
+ abi {
244
+ reset()
245
+ enable(project. ext. react. abiSplit)
246
+ universalApk(false )
247
+ include(* project. ext. react. architectures)
248
+ }
249
+ }
169
250
}
170
251
171
252
dependencies {
@@ -175,8 +256,8 @@ dependencies {
175
256
// TODO: Remove this block when we drop support for 0.68.
176
257
if (reactNativeVersion < 6900 ) {
177
258
def hermesEngineDir =
178
- findNodeModulesPath(file(reactNativeDir), " hermes-engine" )
179
- ?: findNodeModulesPath(file(reactNativeDir), " hermesvm " )
259
+ findNodeModulesPath(" hermes-engine" , file(reactNativeDir) )
260
+ ?: findNodeModulesPath(" hermesvm " , file(reactNativeDir))
180
261
if (hermesEngineDir == null ) {
181
262
throw new GradleException (" Could not find 'hermes-engine'. Please make sure you've added it to 'package.json'." )
182
263
}
@@ -236,3 +317,22 @@ dependencies {
236
317
}
237
318
}
238
319
}
320
+
321
+ if (project. ext. react. enableNewArchitecture) {
322
+ configurations. all {
323
+ resolutionStrategy. dependencySubstitution {
324
+ substitute(module(" com.facebook.react:react-native" ))
325
+ .using(project(" :ReactAndroid" ))
326
+ .because(" On New Architecture, we are building React Native from source" )
327
+ substitute(module(" com.facebook.react:hermes-engine" ))
328
+ .using(project(" :ReactAndroid:hermes-engine" ))
329
+ .because(" On New Architecture, we are building Hermes from source" )
330
+ }
331
+ }
332
+ }
333
+
334
+ // `@react-native-community/cli` currently requires this function to be defined.
335
+ // See https://github.com/react-native-community/cli/blob/a87fb9014635fe84ab19a1a88d6ecbbc530eb4e2/packages/platform-android/native_modules.gradle#L497
336
+ def isNewArchitectureEnabled () {
337
+ return isFabricEnabled(project)
338
+ }
0 commit comments