Skip to content

Commit d6ed1ff

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
Allow configuring ndk build architectures (#31232)
Summary: Building from source in debug takes a very long time because native builds need to run for all supported architectures. It is possible to check which architecture the devices for which we are about to launch the app on are and build only for those. For most cases we can reduce the number of architectures we build for to 1 instead of 4, resulting in a large speedup of the build. This is inspired by iOS which has a "Build for active architecture only" option. Since android doesn't really support this natively we can implement it here and also in react-native by reading the build properties that we pass and alter the abi we build for. With fabric / codegen coming up I suspect that we might want to default to building c++ soon. This should ease the transition as builds won't be orders of magnitude slower. See react-native-community/cli#1388 for more context and how we use this new config to automatically detect running emulator architectures. ## Changelog [Android] [Added] - Allow configuring ndk build architectures Pull Request resolved: #31232 Test Plan: Tested by setting reactNativeDebugArchitectures with different values in gradle.properties. Checked the build logs to see which architectures are being built. Also made sure release builds are not affected by this value. Clean build reactNativeDebugArchitectures not set 824.41s reactNativeDebugArchitectures=x86 299.77s Reviewed By: mdvacca Differential Revision: D29613939 Pulled By: ShikaSD fbshipit-source-id: d20a23d1d9bbf33f5afaaf3475f208a2e48c0e1a
1 parent 8dfc3bc commit d6ed1ff

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

ReactAndroid/build.gradle

+9
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ def reactNativeInspectorProxyPort() {
326326
return value != null ? value : reactNativeDevServerPort()
327327
}
328328

329+
def reactNativeArchitectures() {
330+
def isDebug = gradle.startParameter.taskRequests.any {
331+
it.args.any { it.endsWith("Debug") }
332+
}
333+
def value = project.getProperties().get("reactNativeDebugArchitectures")
334+
return value != null && isDebug ? value : "all"
335+
}
336+
329337
def getNdkBuildFullPath() {
330338
def ndkBuildFullPath = findNdkBuildFullPath()
331339
if (ndkBuildFullPath == null) {
@@ -355,6 +363,7 @@ def buildReactNdkLib = tasks.register("buildReactNdkLib", Exec) {
355363
inputs.dir("src/main/java/com/facebook/react/modules/blob")
356364
outputs.dir("$buildDir/react-ndk/all")
357365
commandLine(getNdkBuildFullPath(),
366+
"APP_ABI=${reactNativeArchitectures()}",
358367
"NDK_DEBUG=" + (nativeBuildType.equalsIgnoreCase("debug") ? "1" : "0"),
359368
"NDK_PROJECT_PATH=null",
360369
"NDK_APPLICATION_MK=$projectDir/src/main/jni/Application.mk",

packages/rn-tester/android/app/build.gradle

+10-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def enableFabric = project.ext.react.enableFabric
128128
*/
129129
def useIntlJsc = false
130130

131+
/**
132+
* Architectures to build native code for in debug.
133+
*/
134+
def nativeArchitectures = project.getProperties().get("reactNativeDebugArchitectures")
135+
131136
android {
132137
compileSdkVersion 29
133138
ndkVersion ANDROID_NDK_VERSION
@@ -179,6 +184,11 @@ android {
179184
debug {
180185
debuggable true
181186
signingConfig signingConfigs.release
187+
if (nativeArchitectures) {
188+
ndk {
189+
abiFilters nativeArchitectures.split(',')
190+
}
191+
}
182192
}
183193
release {
184194
debuggable false
@@ -254,7 +264,6 @@ if (enableCodegen) {
254264
defaultConfig {
255265
externalNativeBuild {
256266
ndkBuild {
257-
abiFilters "armeabi-v7a", "x86", "x86_64", "arm64-v8a"
258267
arguments "APP_PLATFORM=android-21",
259268
"APP_STL=c++_shared",
260269
"NDK_TOOLCHAIN_VERSION=clang",

template/android/app/build.gradle

+10
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ def jscFlavor = 'org.webkit:android-jsc:+'
120120
*/
121121
def enableHermes = project.ext.react.get("enableHermes", false);
122122

123+
/**
124+
* Architectures to build native code for in debug.
125+
*/
126+
def nativeArchitectures = project.getProperties().get("reactNativeDebugArchitectures")
127+
123128
android {
124129
ndkVersion rootProject.ext.ndkVersion
125130

@@ -151,6 +156,11 @@ android {
151156
buildTypes {
152157
debug {
153158
signingConfig signingConfigs.debug
159+
if (nativeArchitectures) {
160+
ndk {
161+
abiFilters nativeArchitectures.split(',')
162+
}
163+
}
154164
}
155165
release {
156166
// Caution! In production, you need to generate your own keystore file.

0 commit comments

Comments
 (0)