Custom KGP task and migration to AGP api for geting kgp version#182788
Conversation
…y getting java version
… verifying kotlin version
…ion time and use it for java and print build variants
|
An existing Git SHA, To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with |
There was a problem hiding this comment.
Code Review
This pull request refactors several Gradle tasks (javaVersion, kgpVersion, printBuildVariants) to use a more modern and testable approach with deferred execution. A new generic PrintTaskDeferred task is introduced, and a specific PrintKgpTask is created. The method for fetching the Kotlin Gradle Plugin version is also updated to use an internal AGP API, with a fallback mechanism. The tests have been updated accordingly to reflect these changes.
My review focuses on the potential risks of using internal APIs, a possible breaking change in a task's output, and an opportunity to improve code readability in one of the new helper functions.
…task # Conflicts: # packages/flutter_tools/gradle/src/main/kotlin/FlutterPluginUtils.kt # packages/flutter_tools/gradle/src/test/kotlin/FlutterPluginTest.kt
gmackall
left a comment
There was a problem hiding this comment.
Is there a reason why we are building a different pattern for PrintKgpTask and the Java/build variants case? Abstractly it seems to me like the combo of PrintTaskDeferred's closureInput: T and messageClosure: (T) -> String is essentially just a provider. It seems like we could instead build a general purpose version of the KGP pattern here:
package com.flutter.gradle.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
/** Prints a message computed lazily, before the task action runs. */
abstract class PrintTask : DefaultTask() {
@get:Input
abstract val message: Property<String>
@TaskAction
fun run() = println(message.get())
}And then use it for all three cases:
internal fun addTaskForJavaVersion(project: Project) {
project.tasks.register<PrintTask>("javaVersion") {
description = "Print the current java version used by gradle. see: " +
"https://docs.gradle.org/current/javadoc/org/gradle/api/JavaVersion.html"
message.set(project.provider { VersionFetcher.getJavaVersion().toString() })
}
}
internal fun addTaskForKGPVersion(project: Project) {
project.tasks.register<PrintTask>("kgpVersion") {
description = "Print the current kgp version used by the project."
message.set(
project.provider { "KGP Version: " + (VersionFetcher.getKGPVersion(project)?.toString() ?: "null") }
)
}
}
internal fun addTaskForPrintBuildVariants(project: Project) {
val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java)
val variants = project.objects.listProperty(String::class.java)
androidComponents.onVariants { variant -> variants.add(variant.name) }
project.tasks.register<PrintTask>("printBuildVariants") {
description = "Prints out all build variants for this Android project"
message.set(variants.map { names -> names.joinToString("\n") { name -> "BuildVariant: $name" } })
}
}I think this accomplishes the same goals - from the lazy configuration docs:
The method Property.set(Provider) specifies a Provider for the value for the property, overwriting whatever value may have been present. This allows you to wire together Provider and Property instances before the values are configured.
I think this makes our general purpose task easier to read as well, as the current version has an added level of abstraction that this removes.
IIRC because this pr is quite old. The reason why I had it different was because of the amount of work that was done in the different calls and that gradles cache configuration docs ban using project state (like project.provider). |
flutter/flutter@8bdce07...b7cb925 2026-06-12 engine-flutter-autoroll@skia.org Roll Skia from cadbde1ec4b7 to 8c89bf2b0ee3 (5 revisions) (flutter/flutter#187926) 2026-06-12 engine-flutter-autoroll@skia.org Roll Packages from 1b56cde to b78ad83 (5 revisions) (flutter/flutter#187928) 2026-06-12 engine-flutter-autoroll@skia.org Roll Dart SDK from f3441f2067ae to f6c31f4c3a63 (17 revisions) (flutter/flutter#187924) 2026-06-12 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from 2KosSR4ONUjIB7tP_... to A3eaUn9mQ_EkSNxVI... (flutter/flutter#187923) 2026-06-12 engine-flutter-autoroll@skia.org Roll Skia from a2228b926c68 to cadbde1ec4b7 (9 revisions) (flutter/flutter#187921) 2026-06-12 kustermann@google.com Remove dynamic module loading code in flutter web engine (flutter/flutter#187777) 2026-06-12 matt.kosarek@canonical.com Remove EnableTransparentWindowBackground because it did nothing important and because Windows 10 does not support DWMWA_SYSTEMBACKDROP_TYPE (flutter/flutter#187848) 2026-06-12 engine-flutter-autoroll@skia.org Roll Fuchsia Test Scripts from dQ4PjIJB5kZFU8Y32... to EmfiOMUge_nnNS33B... (flutter/flutter#187912) 2026-06-12 mvincentong@gmail.com Clarify RichText selection docs (flutter/flutter#186844) 2026-06-12 1063596+reidbaker@users.noreply.github.com Custom KGP task and migration to AGP api for geting kgp version (flutter/flutter#182788) 2026-06-12 engine-flutter-autoroll@skia.org Roll Skia from f61acb31edf8 to a2228b926c68 (5 revisions) (flutter/flutter#187896) 2026-06-12 bdero@google.com [Flutter GPU] Expose ASTC HDR texture formats (flutter/flutter#187715) 2026-06-12 awolff@google.com Expand coverage of android_hardware_smoke_test. Add image, text, blend mode, and blur tests. (flutter/flutter#187600) 2026-06-11 bdero@google.com [Flutter GPU] Add blit operations (flutter/flutter#187289) 2026-06-11 bkonyi@google.com [flutter_tools] Fix version cache git fallback performance regression (flutter/flutter#187400) 2026-06-11 nshahan@google.com Rewrite `-d web-server` hot reload/restart tests (flutter/flutter#187453) 2026-06-11 bdero@google.com [Impeller] Allow sampling textures with manually-uploaded mip levels (flutter/flutter#187729) 2026-06-11 154381524+flutteractionsbot@users.noreply.github.com Sync CHANGELOG.md from stable (flutter/flutter#187884) 2026-06-11 47866232+chunhtai@users.noreply.github.com iOS a11y sets header trait based on heading level (flutter/flutter#186916) 2026-06-11 engine-flutter-autoroll@skia.org Roll Skia from 9f02102df298 to f61acb31edf8 (19 revisions) (flutter/flutter#187869) 2026-06-11 engine-flutter-autoroll@skia.org Roll ICU from ee5f27adc28b to d578f2e8b7bd (8 revisions) (flutter/flutter#187829) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC louisehsu@google.com,stuartmorgan@google.com on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
…ter#182788) Create a unified `PrintTask` that uses a standard Gradle `Property<String>` to lazily hold and print a message. Modify `javaVersion`, `kgpVersion`, and `printBuildVariants` to use this new unified task. Modify existing tests in `FlutterPluginTest` and `FlutterPluginUtilsTest` to mock the newly required task registration and provide data. Add new unit tests in `PrintTaskTest.kt` to cover the `PrintTask` behavior under both simple string and lazy mapped provider usages. KgpVersion is migrated to use the *internal* AGP api for getting kgp version. Related to flutter#180137 Format command that can be run from package_tools/gradle on only files that are staged for commit. `ktlint --editorconfig=../../../dev/bots/test/analyze-test-input/.editorconfig --baseline=../../../dev/bots/test/analyze-test-input/ktlint-baseline.xml $(git diff --name-only --cached| grep \.kt | sed 's/packages\/flutter_tools\/gradle\///g') --format` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
Create a unified
PrintTaskthat uses a standard GradleProperty<String>to lazily hold and print a message.Modify
javaVersion,kgpVersion, andprintBuildVariantsto use this new unified task.Modify existing tests in
FlutterPluginTestandFlutterPluginUtilsTestto mock the newly required task registration and provide data.Add new unit tests in
PrintTaskTest.ktto cover thePrintTaskbehavior under both simple string and lazy mapped provider usages.KgpVersion is migrated to use the internal AGP api for getting kgp version.
Related to #180137
Format command that can be run from package_tools/gradle on only files that are staged for commit.
ktlint --editorconfig=../../../dev/bots/test/analyze-test-input/.editorconfig --baseline=../../../dev/bots/test/analyze-test-input/ktlint-baseline.xml $(git diff --name-only --cached| grep \.kt | sed 's/packages\/flutter_tools\/gradle\///g') --formatPre-launch Checklist
///).