Skip to content

Commit ed92bd6

Browse files
authored
[0.81] Backport: Create a debugOptimized buildType for Android (#53568)
* Migrate RNTester to use `{usesCleartextTraffic}` Manifest Placeholder (#52620) Summary: This creates a `debugOptimized` build type for React Native Android, meaning that we can run C++ optimization on the debug build, while still having the debugger enabled. This is aimed at improving the developer experience for folks developing on low-end devices or emulators. Users that intend to debug can still use the `debug` variant where the full debug symbols are shipped. ## Changelog: [ANDROID] [ADDED] - Create a debugOptimized buildType for Android Pull Request resolved: #52620 Test Plan: Tested locally with RNTester by doing: ``` ./gradlew installDebugOptimized ``` This is the output of the 3 generated .aar. The size difference is a proof that we're correctly stripping out the C++ debug symbols: <img width="193" height="54" alt="Screenshot 2025-07-15 at 17 49 50" src="https://github.com/user-attachments/assets/584a0e8d-2d17-40d4-ac29-da09049d6554" /> <img width="235" height="51" alt="Screenshot 2025-07-15 at 17 49 39" src="https://github.com/user-attachments/assets/eda8f9e7-3509-4334-8c16-990e55caa04d" /> <img width="184" height="52" alt="Screenshot 2025-07-15 at 17 49 32" src="https://github.com/user-attachments/assets/a5c94385-bc00-4484-b43e-088ee039827f" /> Rollback Plan: Reviewed By: cipolleschi Differential Revision: D78351347 Pulled By: cortinico fbshipit-source-id: 568a484ba8d2ee6e089cabc95451938e853fbc54 * Create a debugOptimized buildType for Android (#52648) Summary: Pull Request resolved: #52648 This creates a `debugOptimized` build type for React Native Android, meaning that we can run C++ optimization on the debug build, while still having the debugger enabled. This is aimed at improving the developer experience for folks developing on low-end devices or emulators. Users that intend to debug can still use the `debug` variant where the full debug symbols are shipped. Changelog: [ANDROID] [ADDED] - Create a debugOptimized buildType for Android Reviewed By: cipolleschi Differential Revision: D78425138 fbshipit-source-id: c1e9ea3608e7df10fb871a5584352f0747cf560b
1 parent 366f2ad commit ed92bd6

File tree

16 files changed

+74
-23
lines changed

16 files changed

+74
-23
lines changed

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactExtension.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ abstract class ReactExtension @Inject constructor(val project: Project) {
100100
* Allows to specify the debuggable variants (by default just 'debug'). Variants in this list will
101101
* not be bundled (the bundle file will not be created and won't be copied over).
102102
*
103-
* Default: ['debug']
103+
* Default: ['debug', 'debugOptimized']
104104
*/
105105
val debuggableVariants: ListProperty<String> =
106-
objects.listProperty(String::class.java).convention(listOf("debug"))
106+
objects.listProperty(String::class.java).convention(listOf("debug", "debugOptimized"))
107107

108108
/** Hermes Config */
109109

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.facebook.react.tasks.GenerateEntryPointTask
1818
import com.facebook.react.tasks.GeneratePackageListTask
1919
import com.facebook.react.utils.AgpConfiguratorUtils.configureBuildConfigFieldsForApp
2020
import com.facebook.react.utils.AgpConfiguratorUtils.configureBuildConfigFieldsForLibraries
21+
import com.facebook.react.utils.AgpConfiguratorUtils.configureBuildTypesForApp
2122
import com.facebook.react.utils.AgpConfiguratorUtils.configureDevServerLocation
2223
import com.facebook.react.utils.AgpConfiguratorUtils.configureNamespaceForLibraries
2324
import com.facebook.react.utils.BackwardCompatUtils.configureBackwardCompatibilityReactMap
@@ -84,6 +85,7 @@ class ReactPlugin : Plugin<Project> {
8485
configureAutolinking(project, extension)
8586
configureCodegen(project, extension, rootExtension, isLibrary = false)
8687
configureResources(project, extension)
88+
configureBuildTypesForApp(project)
8789
}
8890

8991
// Library Only Configuration

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/AgpConfiguratorUtils.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import java.net.Inet4Address
1919
import java.net.NetworkInterface
2020
import javax.xml.parsers.DocumentBuilder
2121
import javax.xml.parsers.DocumentBuilderFactory
22+
import kotlin.plus
2223
import org.gradle.api.Action
2324
import org.gradle.api.Project
2425
import org.gradle.api.plugins.AppliedPlugin
@@ -27,6 +28,36 @@ import org.w3c.dom.Element
2728
@Suppress("UnstableApiUsage")
2829
internal object AgpConfiguratorUtils {
2930

31+
fun configureBuildTypesForApp(project: Project) {
32+
val action =
33+
Action<AppliedPlugin> {
34+
project.extensions
35+
.getByType(ApplicationAndroidComponentsExtension::class.java)
36+
.finalizeDsl { ext ->
37+
ext.buildTypes {
38+
val debug =
39+
getByName("debug").apply {
40+
manifestPlaceholders["usesCleartextTraffic"] = "true"
41+
}
42+
getByName("release").apply {
43+
manifestPlaceholders["usesCleartextTraffic"] = "false"
44+
}
45+
maybeCreate("debugOptimized").apply {
46+
manifestPlaceholders["usesCleartextTraffic"] = "true"
47+
initWith(debug)
48+
externalNativeBuild {
49+
cmake {
50+
arguments("-DCMAKE_BUILD_TYPE=Release")
51+
matchingFallbacks += listOf("release")
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
project.pluginManager.withPlugin("com.android.application", action)
59+
}
60+
3061
fun configureBuildConfigFieldsForApp(project: Project, extension: ReactExtension) {
3162
val action =
3263
Action<AppliedPlugin> {

packages/react-native/ReactAndroid/build.gradle.kts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,14 +598,23 @@ android {
598598
publishing {
599599
multipleVariants {
600600
withSourcesJar()
601-
includeBuildTypeValues("debug", "release")
601+
includeBuildTypeValues("debug", "release", "debugOptimized")
602602
}
603603
}
604604

605605
testOptions {
606606
unitTests { isIncludeAndroidResources = true }
607607
targetSdk = libs.versions.targetSdk.get().toInt()
608608
}
609+
610+
buildTypes {
611+
create("debugOptimized") {
612+
initWith(getByName("debug"))
613+
externalNativeBuild {
614+
cmake { arguments("-DCMAKE_BUILD_TYPE=Release", "-DREACT_NATIVE_DEBUG_OPTIMIZED=True") }
615+
}
616+
}
617+
}
609618
}
610619

611620
tasks.withType<KotlinCompile>().configureEach {

packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ android {
306306
}
307307
}
308308
}
309+
buildTypes {
310+
create("debugOptimized") {
311+
initWith(getByName("debug"))
312+
externalNativeBuild { cmake { arguments("-DCMAKE_BUILD_TYPE=Release") } }
313+
}
314+
}
309315
}
310316

311317
sourceSets.getByName("main") {

packages/react-native/ReactAndroid/src/main/jni/react/hermes/reactexecutor/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ target_link_libraries(
2525
reactnative
2626
)
2727
target_compile_reactnative_options(hermes_executor PRIVATE)
28-
target_compile_options(hermes_executor PRIVATE $<$<CONFIG:Debug>:-DHERMES_ENABLE_DEBUGGER=1>)
28+
if(${CMAKE_BUILD_TYPE} MATCHES Debug OR REACT_NATIVE_DEBUG_OPTIMIZED)
29+
target_compile_options(hermes_executor PRIVATE -DHERMES_ENABLE_DEBUGGER=1)
30+
endif()

packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ target_link_libraries(hermesinstancejni
2727
)
2828

2929
target_compile_reactnative_options(hermesinstancejni PRIVATE)
30-
target_compile_options(hermesinstancejni PRIVATE $<$<CONFIG:Debug>:-DHERMES_ENABLE_DEBUGGER=1>)
30+
if(${CMAKE_BUILD_TYPE} MATCHES Debug OR REACT_NATIVE_DEBUG_OPTIMIZED)
31+
target_compile_options(hermesinstancejni PRIVATE -DHERMES_ENABLE_DEBUGGER=1)
32+
endif ()

packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ add_library(rninstance
1717
)
1818

1919
target_compile_reactnative_options(rninstance PRIVATE)
20-
target_compile_options(rninstance PRIVATE $<$<CONFIG:Debug>:-DHERMES_ENABLE_DEBUGGER=1>)
20+
if(${CMAKE_BUILD_TYPE} MATCHES Debug OR REACT_NATIVE_DEBUG_OPTIMIZED)
21+
target_compile_options(rninstance PRIVATE -DHERMES_ENABLE_DEBUGGER=1)
22+
endif ()
2123

2224
target_merge_so(rninstance)
2325
target_include_directories(rninstance PUBLIC .)

packages/react-native/ReactCommon/hermes/executor/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ target_link_libraries(hermes_executor_common
2626
)
2727

2828
target_compile_reactnative_options(hermes_executor_common PRIVATE)
29-
if(${CMAKE_BUILD_TYPE} MATCHES Debug)
29+
if(${CMAKE_BUILD_TYPE} MATCHES Debug OR REACT_NATIVE_DEBUG_OPTIMIZED)
3030
target_compile_options(
3131
hermes_executor_common
3232
PRIVATE

packages/react-native/ReactCommon/hermes/inspector-modern/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ add_library(hermes_inspector_modern
1717

1818
target_compile_reactnative_options(hermes_inspector_modern PRIVATE)
1919

20-
if(${CMAKE_BUILD_TYPE} MATCHES Debug)
20+
if(${CMAKE_BUILD_TYPE} MATCHES Debug OR REACT_NATIVE_DEBUG_OPTIMIZED)
2121
target_compile_options(
2222
hermes_inspector_modern
2323
PRIVATE

0 commit comments

Comments
 (0)