Skip to content

Commit

Permalink
Fix: Android react.gradle VmCleanup when packaging as library (#33179)
Browse files Browse the repository at this point in the history
Summary:
When packaging a react app as an android library with the enableVmCleanup flag not set to false, an error occurs since the "package${targetName}" task can not be found. This PR adds a simple null check, similar to what is being done for the sTask and mTask just below it to prevent the error.

## 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] - Fixes android build error when compiling as library

Pull Request resolved: #33179

Test Plan:
Compile project as library (com.android.library), and should not trigger and error with these changes.

Would also like to have this fix cherry-pick'd to release 0.67 after merging.

Reviewed By: ShikaSD

Differential Revision: D34475934

Pulled By: cortinico

fbshipit-source-id: ce6ce43960c4b388c4b1da49a9a6e21fd3bf8e16
  • Loading branch information
nickfujita authored and facebook-github-bot committed Mar 8, 2022
1 parent ece1dd4 commit c34ef58
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
// Android configuration
variant.registerGeneratedResFolders(generatedResFolders)

val packageTask =
val packageTask: TaskProvider<out Task>? =
when (variant) {
is ApplicationVariant -> variant.packageApplicationProvider
is LibraryVariant -> variant.packageLibraryProvider
Expand Down Expand Up @@ -154,11 +154,11 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
it.enabled = bundleEnabled
}

packageTask.dependsOn(currentCopyResTask)
packageTask?.dependsOn(currentCopyResTask)
preBundleTask.dependsOn(currentCopyResTask)
}

packageTask.configure {
packageTask?.configure {
if (config.enableVmCleanup.get()) {
val libDir = "$buildDir/intermediates/transforms/"
val targetVariant = ".*/transforms/[^/]*/${variant.name}/.*".toRegex()
Expand Down Expand Up @@ -207,7 +207,7 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
// from Android plugin 4.1+.
// This ensures to copy the bundle file before mergeResources task starts
mergeResourcesTask.dependsOn(currentAssetsCopyTask)
packageTask.dependsOn(currentAssetsCopyTask)
packageTask?.dependsOn(currentAssetsCopyTask)
preBundleTask.dependsOn(currentAssetsCopyTask)
}

Expand Down
6 changes: 4 additions & 2 deletions react.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,10 @@ afterEvaluate {

if (enableVmCleanup) {
def task = tasks.findByName("package${targetName}")
def transformsLibDir = "$buildDir/intermediates/transforms/"
task.doFirst { vmSelectionAction(transformsLibDir) }
if (task != null) {
def transformsLibDir = "$buildDir/intermediates/transforms/"
task.doFirst { vmSelectionAction(transformsLibDir) }
}

def sTask = tasks.findByName("strip${targetName}DebugSymbols")
if (sTask != null) {
Expand Down

0 comments on commit c34ef58

Please sign in to comment.