Skip to content

Commit 6f12674

Browse files
ennielfacebook-github-bot
authored andcommitted
gradle vm cleanup fix (#32257)
Summary: This patch remove unused .so files for reduce android .apk and .aab ## Changelog - [Android] [Fixed] - Exclude unused .so files for reduce android .apk and .aab Pull Request resolved: #32257 Test Plan: I have created a repository for testing: https://github.com/enniel/react-native-invalid-build-example That lines includes patch for fix bug: https://github.com/enniel/react-native-invalid-build-example/blob/f195ecdbeaca88ffb57c344615ede45ea0f6ef57/android/app/react.gradle#L331-L385 ```sh git clone https://github.com/enniel/react-native-invalid-build-example cd react-native-invalid-build-example npm i ``` debug, hermes disabled, without patch ```sh git switch hermes-disabled-without-patch npm run build:android:debug:apk ``` open android/app/build/outputs/apk/debug/app-debug.apk in android studio ![debug-hermes-disabled-without-patch](https://user-images.githubusercontent.com/19760944/134640636-1505cfa4-0be9-4bb8-8130-74af01ebfd94.png) debug, hermes enabled, without patch ```sh git switch hermes-enabled-without-patch npm run build:android:debug:apk ``` open android/app/build/outputs/apk/debug/app-debug.apk in android studio ![debug-hermes-enabled-without-patch](https://user-images.githubusercontent.com/19760944/134640743-f5730fbe-433f-45eb-a87c-e0e2eadb056b.png) debug, hermes disabled, with patch ```sh git switch hermes-disabled-with-patch npm run build:android:debug:apk ``` open android/app/build/outputs/apk/debug/app-debug.apk in android studio ![debug-hermes-disabled-with-patch](https://user-images.githubusercontent.com/19760944/134640895-36f294ae-36fb-40b7-a767-f6b421fdbabd.png) debug, hermes enabled, with patch ```sh git switch hermes-enabled-with-patch npm run build:android:debug:apk ``` open android/app/build/outputs/apk/debug/app-debug.apk in android studio ![debug-hermes-enabled-with-patch](https://user-images.githubusercontent.com/19760944/134641040-f4f08755-5234-4567-849d-0b815d58bc15.png) release, hermes disabled, without patch ```sh git switch hermes-disabled-without-patch npm run build:android:release:apk ``` open android/app/build/outputs/apk/release/app-release.apk in android studio ![release-hermes-disabled-without-patch](https://user-images.githubusercontent.com/19760944/134641255-da3ccc5f-91e2-4b12-b0b2-1590fa13e5ce.png) release, hermes enabled, without patch ```sh git switch hermes-enabled-without-patch npm run build:android:release:apk ``` open android/app/build/outputs/apk/release/app-release.apk in android studio ![release-hermes-enabled-without-patch](https://user-images.githubusercontent.com/19760944/134641396-f2ca3178-4225-4de7-b1f7-b741edba1877.png) release, hermes disabled, with patch ```sh git switch hermes-disabled-with-patch npm run build:android:release:apk ``` open android/app/build/outputs/apk/release/app-release.apk in android studio ![release-hermes-disabled-with-patch](https://user-images.githubusercontent.com/19760944/134641675-b967df44-af1b-4070-9f84-a2f029381a39.png) release, hermes enabled, with patch ```sh git switch hermes-enabled-with-patch npm run build:android:release:apk ``` open android/app/build/outputs/apk/release/app-release.apk in android studio ![release-hermes-enabled-with-patch](https://user-images.githubusercontent.com/19760944/134641737-aa4a435a-1fe2-4107-8bbd-8dadda24aebd.png) Reviewed By: cortinico Differential Revision: D31167423 Pulled By: ShikaSD fbshipit-source-id: 4026df818c57fa699526ca1c31c1d1a68d58baef
1 parent 8d0a2e7 commit 6f12674

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

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

+30-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import com.facebook.react.utils.detectedHermesCommand
1919
import java.io.File
2020
import java.util.*
2121
import org.gradle.api.Project
22+
import org.gradle.api.Task
2223
import org.gradle.api.tasks.Copy
24+
import org.gradle.api.tasks.TaskProvider
2325

2426
private const val REACT_GROUP = "react"
2527

@@ -134,6 +136,8 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
134136
is LibraryVariant -> variant.packageLibraryProvider
135137
else -> tasks.named("package$targetName")
136138
}
139+
val stripDebugSymbolsTask: TaskProvider<Task>? = tasks.named("strip${targetName}DebugSymbols")
140+
val mergeNativeLibsTask: TaskProvider<Task>? = tasks.named("merge${targetName}NativeLibs")
137141

138142
val mergeResourcesTask = variant.mergeResourcesProvider
139143
val mergeAssetsTask = variant.mergeAssetsProvider
@@ -160,7 +164,25 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
160164

161165
packageTask.configure {
162166
if (config.enableVmCleanup.get()) {
163-
it.doFirst { cleanupVMFiles(enableHermes, isRelease, targetPath) }
167+
val libDir = "$buildDir/intermediates/transforms/"
168+
val targetVariant = ".*/transforms/[^/]*/$targetPath/.*".toRegex()
169+
it.doFirst { cleanupVMFiles(libDir, targetVariant, enableHermes, isRelease) }
170+
}
171+
}
172+
173+
stripDebugSymbolsTask?.configure {
174+
if (config.enableVmCleanup.get()) {
175+
val libDir = "$buildDir/intermediates/stripped_native_libs/${targetPath}/out/lib/"
176+
val targetVariant = ".*/stripped_native_libs/$targetPath/out/lib/.*".toRegex()
177+
it.doLast { cleanupVMFiles(libDir, targetVariant, enableHermes, isRelease) }
178+
}
179+
}
180+
181+
mergeNativeLibsTask?.configure {
182+
if (config.enableVmCleanup.get()) {
183+
val libDir = "$buildDir/intermediates/merged_native_libs/${targetPath}/out/lib/"
184+
val targetVariant = ".*/merged_native_libs/$targetPath/out/lib/.*".toRegex()
185+
it.doLast { cleanupVMFiles(libDir, targetVariant, enableHermes, isRelease) }
164186
}
165187
}
166188

@@ -191,14 +213,13 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
191213
preBundleTask.dependsOn(currentAssetsCopyTask)
192214
}
193215

194-
private fun Project.cleanupVMFiles(enableHermes: Boolean, isRelease: Boolean, targetPath: String) {
216+
private fun Project.cleanupVMFiles(libDir: String, targetVariant: Regex, enableHermes: Boolean, isRelease: Boolean) {
195217
// Delete the VM related libraries that this build doesn't need.
196218
// The application can manage this manually by setting 'enableVmCleanup: false'
197219
//
198220
// This should really be done by packaging all Hermes related libs into
199221
// two separate HermesDebug and HermesRelease AARs, but until then we'll
200222
// kludge it by deleting the .so files out of the /transforms/ directory.
201-
val libDir = "$buildDir/intermediates/transforms/"
202223
fileTree(libDir) {
203224
if (enableHermes) {
204225
// For Hermes, delete all the libjsc* files
@@ -208,18 +229,23 @@ private fun Project.cleanupVMFiles(enableHermes: Boolean, isRelease: Boolean, ta
208229
// Reduce size by deleting the debugger/inspector
209230
it.include("**/libhermes-inspector.so")
210231
it.include("**/libhermes-executor-debug.so")
232+
it.include("**/libhermes-executor-common-debug.so")
211233
} else {
212234
// Release libs take precedence and must be removed
213235
// to allow debugging
214236
it.include("**/libhermes-executor-release.so")
237+
it.include("**/libhermes-executor-common-release.so")
215238
}
216239
} else {
217240
// For JSC, delete all the libhermes* files
218241
it.include("**/libhermes*.so")
242+
// Delete the libjscexecutor from release build
243+
if (isRelease) {
244+
it.include("**/libjscexecutor.so")
245+
}
219246
}
220247
}
221248
.visit { visit ->
222-
val targetVariant = ".*/transforms/[^/]*/$targetPath/.*".toRegex()
223249
val path = visit.file.absolutePath.replace(File.separatorChar, '/')
224250
if (path.matches(targetVariant) && visit.file.isFile) {
225251
visit.file.delete()

react.gradle

+27-5
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ afterEvaluate {
329329
// two separate HermesDebug and HermesRelease AARs, but until then we'll
330330
// kludge it by deleting the .so files out of the /transforms/ directory.
331331
def isRelease = targetName.toLowerCase().contains("release")
332-
def libDir = "$buildDir/intermediates/transforms/"
333-
def vmSelectionAction = {
332+
333+
def vmSelectionAction = { libDir ->
334334
fileTree(libDir).matching {
335335
if (enableHermes) {
336336
// For Hermes, delete all the libjsc* files
@@ -340,26 +340,48 @@ afterEvaluate {
340340
// Reduce size by deleting the debugger/inspector
341341
include '**/libhermes-inspector.so'
342342
include '**/libhermes-executor-debug.so'
343+
include '**/libhermes-executor-common-debug.so'
343344
} else {
344345
// Release libs take precedence and must be removed
345346
// to allow debugging
346347
include '**/libhermes-executor-release.so'
348+
include '**/libhermes-executor-common-release.so'
347349
}
348350
} else {
349351
// For JSC, delete all the libhermes* files
350352
include "**/libhermes*.so"
353+
// Delete the libjscexecutor from release build
354+
if (isRelease) {
355+
include "**/libjscexecutor.so"
356+
}
351357
}
352358
}.visit { details ->
353-
def targetVariant = ".*/transforms/[^/]*/${targetPath}/.*"
359+
def targetVariant1 = ".*/transforms/[^/]*/${targetPath}/.*"
360+
def targetVariant2 = ".*/merged_native_libs/${targetPath}/out/lib/.*"
361+
def targetVariant3 = ".*/stripped_native_libs/${targetPath}/out/lib/.*"
354362
def path = details.file.getAbsolutePath().replace(File.separatorChar, '/' as char)
355-
if (path.matches(targetVariant) && details.file.isFile()) {
363+
if ((path.matches(targetVariant1) || path.matches(targetVariant2) || path.matches(targetVariant3)) && details.file.isFile()) {
356364
details.file.delete()
357365
}
358366
}
359367
}
360368

361369
if (enableVmCleanup) {
362-
packageTask.doFirst(vmSelectionAction)
370+
def task = tasks.findByName("package${targetName}")
371+
def transformsLibDir = "$buildDir/intermediates/transforms/"
372+
task.doFirst { vmSelectionAction(transformsLibDir) }
373+
374+
def sTask = tasks.findByName("strip${targetName}DebugSymbols")
375+
if (sTask != null) {
376+
def strippedLibDir = "$buildDir/intermediates/stripped_native_libs/${targetPath}/out/lib/"
377+
sTask.doLast { vmSelectionAction(strippedLibDir) }
378+
}
379+
380+
def mTask = tasks.findByName("merge${targetName}NativeLibs")
381+
if (mTask != null) {
382+
def mergedLibDir = "$buildDir/intermediates/merged_native_libs/${targetPath}/out/lib/"
383+
mTask.doLast { vmSelectionAction(mergedLibDir) }
384+
}
363385
}
364386
}
365387
}

0 commit comments

Comments
 (0)