Skip to content

[AGP 9] Remove Flutter's built-in Kotlin opt-out when the app does not apply kotlin-android - #195091

Draft
reidbaker-agent wants to merge 37 commits into
flutter:masterfrom
reidbaker-agent:agp-builtin-kotlin-migration
Draft

reidbaker-agent wants to merge 37 commits into
flutter:masterfrom
reidbaker-agent:agp-builtin-kotlin-migration

Conversation

@reidbaker-agent

@reidbaker-agent reidbaker-agent commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Existing Android projects drop Flutter's android.builtInKotlin=false opt-out on their next build, unless the app applies kotlin-android. In the same build, #194460 then drops android.newDsl=false. If a plugin still applies kotlin-android, the build fails and names the plugin.

This PR stacks on #195088, which stacks on #194460. To see only this PR's changes: https://github.com/flutter/flutter/pull/195091/files/e479f18dd5c..d5a6b5c8ba4

Part of #189235 and #189236.

Before this leaves draft: the docs.flutter.dev page (#193713), the c: API break label, and flutter-announce and #announcements posts.

Agent authored details

What it does

RemoveBuiltInKotlinOptOutMigration (new) removes a Flutter marker comment followed directly by android.builtInKotlin=false. Both markers count: # This builtInKotlin flag was added by the Flutter template and # This builtInKotlin flag was added automatically by Flutter migrator. It removes nothing when AndroidProject.appliesKotlinAndroidPlugin is true. An opt-out without a marker, or with another value, belongs to the developer and stays. It mirrors #194460's RemoveNewDslOptOutMigration. It runs right after DisableBuiltInKotlinMigration and before DisableNewDslMigration and RemoveNewDslOptOutMigration, so one flutter build removes both opt-outs.

The migrations can't undo each other: since #195088, DisableBuiltInKotlinMigration only adds the opt-out when the app applies kotlin-android, and this migration only removes it when the app doesn't.

Two options for apps created before #185953 (they apply kotlin-android)

  1. As implemented (conditional). Any app created since [AGP 9] Update templates to AGP 9+ and to Built-in Kotlin #185953, and any older app once the developer removes id("kotlin-android") from app/build.gradle(.kts), gets both opt-outs removed automatically. Apps that still apply kotlin-android keep builtInKotlin=false and keep seeing the Flutter Gradle Plugin's migration warning.
  2. Forced (Force devs to opt-in to built-in Kotlin in 3.50 #189235: "We force this and break unmigrated apps"). Delete the appliesKotlinAndroidPlugin checks at disable_built_in_kotlin_migration.dart line 36 (so the migration never adds the opt-out) and remove_built_in_kotlin_opt_out_migration.dart line 50 (so it always removes the marked opt-out). Every app created before [AGP 9] Update templates to AGP 9+ and to Built-in Kotlin #185953 then fails on its next build with the Kotlin Gradle Plugin error The 'org.jetbrains.kotlin.android' plugin is no longer required for Kotlin support since AGP 9.0. With [flutter_tools] Say whether the app or a plugin applies kotlin-android under built-in Kotlin #195081, Flutter shows a two-step app migration message for it. An unmarked android.builtInKotlin=false still works as an escape hatch for developers blocked by a third-party plugin: both migrations only touch lines that follow a Flutter marker.

Who is affected (as implemented)

  • Apps whose dependencies include a plugin that applies kotlin-android. The build fails and names the plugin's build file. The removal warning says how to keep the opt-out: add android.builtInKotlin=false and android.newDsl=false without the Flutter comment. Both are needed, because the Kotlin Gradle Plugin also refuses to apply when newDsl is on.

Verification (AGP 9.3.1, KGP 2.4.20)

  • App applying kotlin-android with migrator-written opt-outs: no change, builds.
  • Same app after removing id("kotlin-android"): both opt-outs removed in one build, both warnings printed, builds.
  • Template-marked opt-outs plus a plugin applying kotlin-android: both opt-outs removed, then the build fails with The 'org.jetbrains.kotlin.android' plugin is no longer required …. Without [flutter_tools] Say whether the app or a plugin applies kotlin-android under built-in Kotlin #195081 the Flutter box shows the generic message.
  • flutter test test/general.shard/android/android_project_migration_test.dart: +75 passed. Added the built-in Kotlin opt-out removal group: both markers, one-run removal of both opt-outs, kotlin-android kept, unmarked kept, edited value kept, separators, CRLF, missing file, idempotent.
  • dart analyze --fatal-infos and dart format passed.

No comments, docs or tests removed.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

The application path of the Flutter Gradle Plugin no longer uses the
deprecated BaseVariant API (applicationVariants / ApkVariantOutput):

- CopyFlutterApksTask copies the variant's SingleArtifact.APK outputs,
  read through BuiltArtifactsLoader, into build/outputs/flutter-apk under
  the unchanged names app[-abi][-flavor]-<mode>.apk. assemble<Variant>
  depends on it. The task has no Project or variant fields and uses an
  injected FileSystemOperations.
- A single naming function in the task produces both the declared
  @OutputFiles and the copied file names. Its inputs are the ABIs of the
  variant's outputs (what AGP builds), the flavor, and the build mode.
  The task fails if AGP built an APK for an ABI that was not declared.
- Per-ABI versionCode for --split-per-abi is a read-then-set on
  VariantOutput.versionCode in onVariants. This is a documented
  exception to the no-configuration-time-get rule, because the lazy
  form is circular. The read relies on AGP's compatibility mode
  (android.compatibility.enableLegacyApi, on by default through 9.x).
- The applicationVariants.configureEach block and its
  @Suppress("DEPRECATION") markers are deleted. The add-to-app module
  path keeps using libraryVariants until the next phase.

Behavior change: Flutter's onVariants callback runs before an app's own
androidComponents.onVariants block, while ApkVariantOutput's
versionCodeOverride was applied after it. Apps that transform
output.versionCode there transform Flutter's offset value (for example
arm64-v8a, build 42, x10000: 422000 -> 20420000). The migration doc and
the breaking-change page draft describe this and give a recipe that
keeps the 422000-style numbers. No divergence warning is emitted,
because it would fire on the onVariants pattern that AGP recommends.

Part of flutter#166550.
…ures

- Take the per-ABI versionCode base from a finalizeDsl snapshot of the
  DSL versionCodes (DslVersionCodes) instead of reading
  output.versionCode, which AGP rejects during configuration when
  android.compatibility.enableLegacyApi=false. Warn and skip when the
  DSL declares no versionCode.
- Add a strict-mode (enableLegacyApi=false) case to
  flutter_build_apk_split_per_abi_test.
- CopyFlutterApksTask: use Gradle's Copy/Sync caching annotation and
  reason; explain when the metadata can be missing and when the ABI
  check can fail; point both errors at the plugin that transforms
  SingleArtifact.APK instead of asking for a Flutter issue.
- Document what an APK transform must keep in website-page-draft.md.
- Link the website-page-draft.md references to tracking issue flutter#193713.
- Remove temporal wording from KDoc and docs.
Add-to-app module (library) variants use the same androidComponents.onVariants
path as application variants: the compile, assets and jniLibs tasks are
registered lazily per library variant, and the assets and native libraries are
generated source directories of the variant. The host app consumes them like
any Android library.

Deletes the host-project lookup, the libraryVariants x applicationVariants
cross-wiring, the merge<HostVariant>Assets.dependsOn edge,
addFlutterDepsForModule, addCopyFlutterAssetsDependency,
shouldConfigureFlutterTask(Project, Task) and
buildModeFor(com.android.builder.model.BuildType).

flutter.hostAppProjectName has no effect and logs a warning.

The jniLibs copy is wired from the compile task provider, so its input
directory is required in both copy tasks.
…er comments

- CopyFlutterApksTask: state why caching is disabled (local copy; a cache
  entry would duplicate the APKs) and build both errors through one
  transformedApkError helper.
- Trim comments and KDoc added by this PR to what the code does not
  already say.
…er-agent/flutter into agp-add-to-app-module

# Conflicts:
#	packages/flutter_tools/gradle/src/main/kotlin/FlutterPlugin.kt
#	packages/flutter_tools/gradle/src/main/kotlin/FlutterPluginUtils.kt
Keep only what the code doesn't say: why library variants are not
gated by the command line, why the copy task inputs are required, and
what the tests guard. Restore PR 7's shouldCompileFlutterForVariant
paragraph verbatim.
Explain why library variants skip the command-line gate, drop the
application/library list from the assets comment, drop the 'public
variant API' clause, and say what library variants lack in
configureApplicationOutputs.
…m_build_test

The devicelab test already builds the same host fixture one task at a
time. Add a debuggable qa build type that falls back to the module's
release variant, and a final section that builds it with
android.newDsl=true and flutter.hostAppProjectName set. It checks the
warning and that the APK has release Flutter artifacts.

Delete android_add_to_app_module_test.dart, which duplicated that
setup.
Add stubTaskRegistration, captureTaskConfiguration and
setCommandLineTasks, and use them in the add-to-app onVariants tests.
Branch on the variant type at the call site so the function receives an
ApplicationVariant and needs no check().
…a section

AGP 9.1 and 9.3 deprecate the option. Its consumers back the variant API
that android.newDsl=true already removes.
The flutter tool runs Gradle with -q, which hides logger.warn. Log the
warning with logger.error, as the plugin's other user-facing warnings
do, and state the consequence (Google Play needs a distinct versionCode
per APK) and the fix.
flutter build aar failed on a module with android.newDsl=true: the
library check read android.libraryVariants, which AGP does not create
with the new DSL ("Can't generate AAR on a non Android library
project."), and the plugin wiring read it again.

- Detect a library project with pluginManager.hasPlugin(
  "com.android.library").
- Declare singleVariant publishing for each variant, and keep a
  declaration that the project's build file already made. AGP's
  duplicate-declaration error is matched by its message, because its
  class is not public AGP API. This replaces the read of AGP's internal
  publishing.singleVariants, which skipped Flutter's declarations for
  every variant once the build file declared any, so a module that
  declared only release failed every flutter build aar.
- Remove the module-to-plugin assembleAar<Variant> dependsOn. The tool
  requests the task by name, so Gradle runs it in every project that
  has it and each plugin publishes its own AAR.
- When an Android plugin has no variant with the requested name
  (different product flavors), fail with an error that names the plugin,
  instead of "Task with name 'assembleAar<Variant>' not found".
- devicelab build_aar_module_test builds with android.newDsl=true and a
  module singleVariant("release") declaration.
- Update the migration doc and the website page draft.
The tool requests assembleAar<Variant> without a project path, so Gradle
skips projects that lack it. Check each requested task against the module
and every Android plugin, so a flavor mismatch in either direction fails
instead of reporting success with an incomplete repo. Cover both errors in
build_aar_module_test and scope the doc claims about the deleted code.
…AR tasks lazily

- checkProjectsHaveRequestedVariants compares the requested task names
  with aarTaskName() of each project's publishing components, so no task
  name is parsed back into a variant name. Its doc says when it returns
  and when it throws.
- aarComponents() is the one component filter for task registration and
  the check.
- addAarTask registers its task lazily. The publication is created
  outside the task's configuration action, where maven-publish may
  register its tasks.
- Link AGP's checkSingleVariantUniqueness source (AGP 9.1.0 tag) and say
  why the duplicate error is identified.
- Keep the task-selector explanation once, in projectsEvaluated.
- Docs describe only the behavior.
- checkProjectsHaveRequestedVariants: a requested task that matches no
  project's variant is left to Gradle's task selector error.
- aarComponents: describe what the filter returns.
- addAarTask: state the effect of the requested-task guard, and drop the
  "all" note from the publication comment (the filter is in aarComponents).
…add-to-app-module

# Conflicts:
#	packages/flutter_tools/gradle/src/main/kotlin/FlutterPlugin.kt
#	packages/flutter_tools/gradle/src/main/kotlin/FlutterPluginUtils.kt
#	packages/flutter_tools/gradle/src/test/kotlin/FlutterPluginTest.kt
Drop the "all" component filter. AGP 7.x and lower created an automatic
"all" component only when the project used no publishing DSL; this script
declares singleVariant for every variant, and AGP 8.0+ has no automatic
components. Describe what AGP creates for singleVariant, and select the
requested components in the caller so addAarTask no longer returns early.
Registering only the requested task drops Gradle's candidate list
('Some candidates are: assembleAarDebug, ...') from the error for a
misspelled --flavor. Register a task for each component, as before, and
keep the requested-component filter for publications only.
…ant API errors

- DisableNewDslMigration skips projects whose gradle.properties sets
  android.builtInKotlin=true and whose app does not apply kotlin-android.
- RemoveNewDslOptOutMigration removes the Flutter-written newDsl marker
  pair from those projects only.
- legacyVariantApiErrorHandler and kotlinAndroidPluginWithNewDslErrorHandler
  match errors reproduced with AGP 9.3.1 and KGP 2.4.20.
- The deferred component template, its in-repo test app and the
  integration test fixture use androidComponents.onVariants instead of
  applicationVariants.
…nt errors, comment on the static source path
…sl-default

# Conflicts:
#	packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart
…sl-default

# Conflicts:
#	docs/platforms/android/Migrating-Flutter-Gradle-Plugin-to-AGP-public-API.md
…on, plugin scripts in the error and docs, warning with escape hatch

- builtInKotlin values keep trailing whitespace, as java.util.Properties
  does, so 'true ' is not true (matches the Flutter Gradle Plugin).
- kotlin-android detection accepts a version suffix and the
  libs.plugins.jetbrains.kotlin.android alias; apply false is not applied.
- The legacy variant API error says the script may belong to a plugin and
  points at Gradle's '* Where:' line.
- The opt-out removal prints a warning that names the bare-flag escape hatch.
- Source comments name the Gradle, AGP and KGP versions each pattern matches.
- The CRLF test asserts the exact output.
The Flutter tool writes deferred component files under
intermediates/flutter/<debug|profile|release>. Keying the directories by
variant name missed them for custom build types and flavored variants.
The app and the dynamic feature module declare a free flavor, so the
variant name (freeRelease) differs from the Flutter build mode (release).
With variant-name keying the component's libapp.so part is missing from
the bundle.
… sources

The directories are filled by the app's compileFlutterBuild<Variant> task,
but nothing ordered the component's merge tasks after it. A task with no
actions declares them as outputs, depends on the app's Flutter task, and
is passed to addGeneratedSourceDirectory, which also removes the
configuration-time .get() on the build directory.
The app and module templates no longer write android.builtInKotlin=false
and android.newDsl=false. DisableBuiltInKotlinMigration adds the
builtInKotlin opt-out only when the app applies kotlin-android, and a
missing android.builtInKotlin entry counts as enabled, matching the
Android Gradle Plugin default.
…kotlin-android

RemoveBuiltInKotlinOptOutMigration removes the android.builtInKotlin=false
pair that the template or DisableBuiltInKotlinMigration wrote, unless the
app applies kotlin-android. It runs before RemoveNewDslOptOutMigration so
one build removes both opt-outs.
@reidbaker reidbaker added the CICD Run CI/CD label Oct 22, 2026
@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. team-android Owned by Android platform team d: docs/ flutter/flutter/docs, for contributors labels Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD d: docs/ flutter/flutter/docs, for contributors 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.

2 participants