Skip to content

Commit 91adb76

Browse files
geraintwhitefacebook-github-bot
authored andcommitted
Add hermesFlagsForVariant and deleteDebugFilesForVariant (#32281)
Summary: Ref #25601 (comment). From #31040. The `hermesFlagsRelease` option only works with the release build type, but not with other build types. This PR allows hermes flags on a per variant basis to be specified using the `hermesFlagsForVariant` lambda. It also allows the hermes debugger cleanup to be run on a per variant basis using the `deleteDebugFilesForVariant` lambda. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [Android] [Fixed] - Fix hermesFlags not working with multiple variants Pull Request resolved: #32281 Test Plan: Set the following options in `android/app/build.gradle` and ensure warnings are hidden when running `./gradlew assembleRelease` and `./gradlew assembleLive`. ``` hermesFlagsForVariant: { def v -> v.name.toLowerCase().contains('release') || v.name.toLowerCase().contains('live') ? ['-w'] : [] }, deleteDebugFilesForVariant: { def v -> v.name.toLowerCase().contains('release') || v.name.toLowerCase().contains('live') }, ``` Reviewed By: cortinico Differential Revision: D31234241 Pulled By: ShikaSD fbshipit-source-id: 2cb3dd63adbcd023061076b5a3b262a87b470518
1 parent 8ba4a2f commit 91adb76

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ abstract class ReactExtension @Inject constructor(project: Project) {
137137
*/
138138
var enableHermesForVariant: (BaseVariant) -> Boolean = { enableHermes.get() }
139139

140+
/**
141+
* Functional interface specify flags for Hermes on specific [BaseVariant] Default: will
142+
* return [hermesFlagsRelease] for Release variants and [hermesFlagsDebug] for Debug variants.
143+
*/
144+
var hermesFlagsForVariant: (BaseVariant) -> List<String> = {
145+
variant -> if (variant.isRelease) hermesFlagsRelease.get() else hermesFlagsDebug.get()
146+
}
147+
148+
/**
149+
* Functional interface to delete debug files only on specific [BaseVariant] Default: will
150+
* return True for Release variants and False for Debug variants.
151+
*/
152+
var deleteDebugFilesForVariant: (BaseVariant) -> Boolean = {
153+
variant -> variant.isRelease
154+
}
155+
140156
/** Flags to pass to Hermes for Debug variants. Default: [] */
141157
val hermesFlagsDebug: ListProperty<String> =
142158
objects.listProperty(String::class.java).convention(emptyList())

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
4949

5050
val execCommand = nodeExecutableAndArgs + cliPath
5151
val enableHermes = config.enableHermesForVariant(variant)
52+
val cleanup = config.deleteDebugFilesForVariant(variant)
5253
val bundleEnabled = variant.checkBundleEnabled(config)
5354

5455
val bundleTask =
@@ -100,8 +101,7 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
100101

101102
it.reactRoot = config.reactRoot.get().asFile
102103
it.hermesCommand = detectedHermesCommand(config)
103-
it.hermesFlags =
104-
if (isRelease) config.hermesFlagsRelease.get() else config.hermesFlagsDebug.get()
104+
it.hermesFlags = config.hermesFlagsForVariant(variant)
105105
it.jsBundleFile = jsBundleFile
106106
it.composeSourceMapsCommand = nodeExecutableAndArgs + config.composeSourceMapsPath.get()
107107
it.jsPackagerSourceMapFile = jsPackagerSourceMapFile
@@ -166,23 +166,23 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
166166
if (config.enableVmCleanup.get()) {
167167
val libDir = "$buildDir/intermediates/transforms/"
168168
val targetVariant = ".*/transforms/[^/]*/$targetPath/.*".toRegex()
169-
it.doFirst { cleanupVMFiles(libDir, targetVariant, enableHermes, isRelease) }
169+
it.doFirst { cleanupVMFiles(libDir, targetVariant, enableHermes, cleanup) }
170170
}
171171
}
172172

173173
stripDebugSymbolsTask?.configure {
174174
if (config.enableVmCleanup.get()) {
175175
val libDir = "$buildDir/intermediates/stripped_native_libs/${targetPath}/out/lib/"
176176
val targetVariant = ".*/stripped_native_libs/$targetPath/out/lib/.*".toRegex()
177-
it.doLast { cleanupVMFiles(libDir, targetVariant, enableHermes, isRelease) }
177+
it.doLast { cleanupVMFiles(libDir, targetVariant, enableHermes, cleanup) }
178178
}
179179
}
180180

181181
mergeNativeLibsTask?.configure {
182182
if (config.enableVmCleanup.get()) {
183183
val libDir = "$buildDir/intermediates/merged_native_libs/${targetPath}/out/lib/"
184184
val targetVariant = ".*/merged_native_libs/$targetPath/out/lib/.*".toRegex()
185-
it.doLast { cleanupVMFiles(libDir, targetVariant, enableHermes, isRelease) }
185+
it.doLast { cleanupVMFiles(libDir, targetVariant, enableHermes, cleanup) }
186186
}
187187
}
188188

@@ -217,7 +217,7 @@ private fun Project.cleanupVMFiles(
217217
libDir: String,
218218
targetVariant: Regex,
219219
enableHermes: Boolean,
220-
isRelease: Boolean
220+
cleanup: Boolean
221221
) {
222222
// Delete the VM related libraries that this build doesn't need.
223223
// The application can manage this manually by setting 'enableVmCleanup: false'
@@ -230,7 +230,7 @@ private fun Project.cleanupVMFiles(
230230
// For Hermes, delete all the libjsc* files
231231
it.include("**/libjsc*.so")
232232

233-
if (isRelease) {
233+
if (cleanup) {
234234
// Reduce size by deleting the debugger/inspector
235235
it.include("**/libhermes-inspector.so")
236236
it.include("**/libhermes-executor-debug.so")
@@ -245,7 +245,7 @@ private fun Project.cleanupVMFiles(
245245
// For JSC, delete all the libhermes* files
246246
it.include("**/libhermes*.so")
247247
// Delete the libjscexecutor from release build
248-
if (isRelease) {
248+
if (cleanup) {
249249
it.include("**/libjscexecutor.so")
250250
}
251251
}
@@ -270,5 +270,5 @@ private fun BaseVariant.checkBundleEnabled(config: ReactExtension): Boolean {
270270
return isRelease
271271
}
272272

273-
private val BaseVariant.isRelease: Boolean
273+
internal val BaseVariant.isRelease: Boolean
274274
get() = name.toLowerCase(Locale.ROOT).contains("release")

react.gradle

+26-13
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ def enableHermesForVariant = config.enableHermesForVariant ?: {
8383
def variant -> config.enableHermes ?: false
8484
}
8585

86+
// Set hermesFlagsForVariant to a function to configure per variant,
87+
// or set `hermesFlagsRelease` and `hermesFlagsDebug` to an array
88+
def hermesFlagsForVariant = config.hermesFlagsForVariant ?: {
89+
def variant ->
90+
def hermesFlags;
91+
if (variant.name.toLowerCase().contains("release")) {
92+
// Can't use ?: since that will also substitute valid empty lists
93+
hermesFlags = config.hermesFlagsRelease
94+
if (hermesFlags == null) hermesFlags = ["-O", "-output-source-map"]
95+
} else {
96+
hermesFlags = config.hermesFlagsDebug
97+
if (hermesFlags == null) hermesFlags = []
98+
}
99+
return hermesFlags
100+
}
101+
102+
// Set deleteDebugFilesForVariant to a function to configure per variant,
103+
// defaults to True for Release variants and False for debug variants
104+
def deleteDebugFilesForVariant = config.deleteDebugFilesForVariant ?: {
105+
def variant -> variant.name.toLowerCase().contains("release")
106+
}
107+
86108
android {
87109
buildTypes.all {
88110
resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort()
@@ -177,18 +199,9 @@ afterEvaluate {
177199

178200
if (enableHermes) {
179201
doLast {
180-
def hermesFlags;
202+
def hermesFlags = hermesFlagsForVariant(variant)
181203
def hbcTempFile = file("${jsBundleFile}.hbc")
182204
exec {
183-
if (targetName.toLowerCase().contains("release")) {
184-
// Can't use ?: since that will also substitute valid empty lists
185-
hermesFlags = config.hermesFlagsRelease
186-
if (hermesFlags == null) hermesFlags = ["-O", "-output-source-map"]
187-
} else {
188-
hermesFlags = config.hermesFlagsDebug
189-
if (hermesFlags == null) hermesFlags = []
190-
}
191-
192205
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
193206
commandLine("cmd", "/c", getHermesCommand(), "-emit-binary", "-out", hbcTempFile, jsBundleFile, *hermesFlags)
194207
} else {
@@ -328,15 +341,15 @@ afterEvaluate {
328341
// This should really be done by packaging all Hermes related libs into
329342
// two separate HermesDebug and HermesRelease AARs, but until then we'll
330343
// kludge it by deleting the .so files out of the /transforms/ directory.
331-
def isRelease = targetName.toLowerCase().contains("release")
344+
def cleanup = deleteDebugFilesForVariant(variant)
332345

333346
def vmSelectionAction = { libDir ->
334347
fileTree(libDir).matching {
335348
if (enableHermes) {
336349
// For Hermes, delete all the libjsc* files
337350
include "**/libjsc*.so"
338351

339-
if (isRelease) {
352+
if (cleanup) {
340353
// Reduce size by deleting the debugger/inspector
341354
include '**/libhermes-inspector.so'
342355
include '**/libhermes-executor-debug.so'
@@ -351,7 +364,7 @@ afterEvaluate {
351364
// For JSC, delete all the libhermes* files
352365
include "**/libhermes*.so"
353366
// Delete the libjscexecutor from release build
354-
if (isRelease) {
367+
if (cleanup) {
355368
include "**/libjscexecutor.so"
356369
}
357370
}

0 commit comments

Comments
 (0)