-
-
Notifications
You must be signed in to change notification settings - Fork 359
Move task operations out of onVariants to fix AGP Artifacts API confl… #5690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6ffd937
4272792
24f3f78
b072259
ee94db5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,13 +65,37 @@ tasks.register("cleanupTemporarySentryJsonConfiguration") { | |
| plugins.withId('com.android.application') { | ||
| def androidComponents = extensions.getByName("androidComponents") | ||
|
|
||
| // Collect ApplicationVariant objects in onVariants - do NOT access tasks here. | ||
| // Calling tasks.findAll or tasks.matching{}.each{} inside onVariants forces task realization | ||
| // during AGP's variant configuration phase, which disrupts the Artifacts API | ||
| // transform chain used by other plugins (e.g. those calling | ||
| // variant.artifacts.use(...).toTransform(SingleArtifact.APK)). The result is | ||
| // that those plugins' APK output ends up in build/intermediates/ instead of | ||
| // build/outputs/, causing downstream tooling to fail to locate the final APK. | ||
| def releaseVariants = [] | ||
| androidComponents.onVariants(androidComponents.selector().all()) { v -> | ||
| if (!v.name.toLowerCase().contains("debug")) { | ||
| releaseVariants << v | ||
| } | ||
| } | ||
|
|
||
| // All task-level operations must happen in afterEvaluate, not inside onVariants. | ||
| // By the time afterEvaluate runs, all plugins have registered their onVariants | ||
| // callbacks and the AGP Artifacts API transform chain is fully established. | ||
| project.afterEvaluate { | ||
| if (releaseVariants.isEmpty()) { | ||
| project.logger.warn("[sentry] No release variants collected, onVariants may have run after afterEvaluate. Sourcemap upload tasks will not be registered.") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This warning is inaccurate. It should not be possible for onVariants to have been invoked
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wdyt of simplifying the message to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds great. The one you've got in #5714 looks good too! |
||
| } | ||
| releaseVariants.each { v -> | ||
| // separately we then hook into the bundle task of react native to inject | ||
| // sourcemap generation parameters. In case for whatever reason no release | ||
| // was found for the asset folder we just bail. | ||
| def bundleTasks = tasks.findAll { task -> (task.name.startsWith("createBundle") || task.name.startsWith("bundle")) && task.name.endsWith("JsAndAssets") && !task.name.contains("Debug") && task.enabled } | ||
| bundleTasks.each { bundleTask -> | ||
| tasks.matching { task -> | ||
| (task.name.startsWith("createBundle") || task.name.startsWith("bundle")) && | ||
| task.name.endsWith("JsAndAssets") && | ||
| !task.name.contains("Debug") | ||
| }.each { bundleTask -> | ||
| if (!bundleTask.enabled) return | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inefficient nested loop processes each bundle task multiple timesMedium Severity The nested loop structure iterates over all release variants in the outer loop and all bundle tasks in the inner loop, causing each bundle task to be processed N times (once per variant). Lines 98-112 execute for every (variant, bundleTask) combination, but |
||
| def shouldCleanUp | ||
| def sourcemapOutput | ||
| def bundleOutput | ||
|
|
@@ -95,7 +119,7 @@ plugins.withId('com.android.application') { | |
| // .join('\n') | ||
|
|
||
| def currentVariants = extractCurrentVariants(bundleTask, v) | ||
| if (currentVariants == null) return | ||
| if (currentVariants == null || currentVariants.isEmpty()) return | ||
|
|
||
| def previousCliTask = null | ||
| def applicationVariant = null | ||
|
|
@@ -304,9 +328,10 @@ plugins.withId('com.android.application') { | |
| previousCliTask.configure { finalizedBy cliCleanUpTask } | ||
|
|
||
| def packageTasks = tasks.matching { | ||
| task -> ("package${applicationVariant}".equalsIgnoreCase(task.name) || "package${applicationVariant}Bundle".equalsIgnoreCase(task.name)) && task.enabled | ||
| task -> ("package${applicationVariant}".equalsIgnoreCase(task.name) || "package${applicationVariant}Bundle".equalsIgnoreCase(task.name)) | ||
| } | ||
| packageTasks.configureEach { packageTask -> | ||
| if (!packageTask.enabled) return | ||
| packageTask.dependsOn modulesTask | ||
| packageTask.finalizedBy modulesCleanUpTask | ||
| } | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misleading warning for projects with only debug variants
Low Severity
The warning assumes
releaseVariantsis empty becauseonVariantsran afterafterEvaluate, but it could also be empty because the project legitimately has no release variants (only debug). The warning message "onVariants may have run after afterEvaluate" is misleading in this case and could confuse developers working on debug-only configurations or sample projects. The code cannot distinguish between timing issues and the legitimate absence of release variants.