Skip to content

Custom KGP task and migration to AGP api for geting kgp version#182788

Merged
auto-submit[bot] merged 32 commits into
flutter:masterfrom
reidbaker:i180137-custom-kgp-task
Jun 12, 2026
Merged

Custom KGP task and migration to AGP api for geting kgp version#182788
auto-submit[bot] merged 32 commits into
flutter:masterfrom
reidbaker:i180137-custom-kgp-task

Conversation

@reidbaker

@reidbaker reidbaker commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

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 #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

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions Bot added platform-android Android applications specifically tool Affects the "flutter" command-line tool. See also t: labels. team-android Owned by Android platform team labels Feb 23, 2026
@reidbaker reidbaker closed this Feb 27, 2026
@reidbaker reidbaker reopened this Feb 27, 2026
@fluttergithubbot

Copy link
Copy Markdown
Contributor

An existing Git SHA, e253f595b75a42e42715190c84126761fbfda120, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

@reidbaker reidbaker marked this pull request as ready for review February 27, 2026 18:53
@reidbaker reidbaker requested a review from a team as a code owner February 27, 2026 18:53

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/flutter_tools/gradle/src/main/kotlin/tasks/PrintKgpTask.kt Outdated
Comment thread packages/flutter_tools/gradle/src/main/kotlin/VersionFetcher.kt
Comment thread packages/flutter_tools/gradle/src/main/kotlin/FlutterPluginUtils.kt Outdated
Comment thread packages/flutter_tools/gradle/src/main/kotlin/VersionFetcher.kt
…task

# Conflicts:
#	packages/flutter_tools/gradle/src/main/kotlin/FlutterPluginUtils.kt
#	packages/flutter_tools/gradle/src/test/kotlin/FlutterPluginTest.kt
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Jun 5, 2026
@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 5, 2026
@reidbaker reidbaker added the CICD Run CI/CD label Jun 5, 2026

@gmackall gmackall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@reidbaker

Copy link
Copy Markdown
Contributor Author

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).
https://docs.gradle.org/current/userguide/configuration_cache_requirements.html
Let me see if there is another way to do what you are asking.

@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 8, 2026
@reidbaker reidbaker requested a review from gmackall June 8, 2026 19:08
@gmackall gmackall added the CICD Run CI/CD label Jun 8, 2026
@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 8, 2026
@reidbaker reidbaker added the CICD Run CI/CD label Jun 8, 2026
gmackall
gmackall previously approved these changes Jun 8, 2026
@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 9, 2026
@reidbaker reidbaker added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Jun 10, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue Jun 12, 2026
Merged via the queue into flutter:master with commit dd2d0ff Jun 12, 2026
168 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 12, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Jun 12, 2026
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
via-guy pushed a commit to via-guy/flutter that referenced this pull request Jun 26, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD platform-android Android applications specifically team-android Owned by Android platform team tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants