Skip to content

[AGP 9] Create Android projects with built-in Kotlin and the new DSL - #195088

Draft
reidbaker-agent wants to merge 36 commits into
flutter:masterfrom
reidbaker-agent:agp-builtin-kotlin-templates
Draft

reidbaker-agent wants to merge 36 commits into
flutter:masterfrom
reidbaker-agent:agp-builtin-kotlin-templates

Conversation

@reidbaker-agent

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

Copy link
Copy Markdown
Contributor

flutter create makes Android projects that use built-in Kotlin and the new DSL, the Android Gradle Plugin defaults. Existing projects whose app applies kotlin-android still get android.builtInKotlin=false added.

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

Fixes #194439. 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

Changes:

  • templates/app/android.tmpl/gradle.properties.tmpl and templates/module/android/gradle/gradle.properties.tmpl no longer write the android.newDsl=false and android.builtInKotlin=false pairs.
  • DisableBuiltInKotlinMigration returns early unless AndroidProject.appliesKotlinAndroidPlugin (from [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460) is true. Without this guard it would write android.builtInKotlin=false back into every new project on its first build.
  • usesBuiltInKotlinWithoutKotlinAndroidPlugin treats a missing android.builtInKotlin entry as enabled, which matches the Android Gradle Plugin default and FlutterPluginUtils.isBuiltInKotlinEnabled. DisableNewDslMigration applies the same check when gradle.properties is missing. So new projects also skip the newDsl opt-out.

Who is affected:

  • New projects that depend on a plugin that still applies kotlin-android. The build fails in the Kotlin Gradle Plugin and names the plugin's build file. [flutter_tools] Say whether the app or a plugin applies kotlin-android under built-in Kotlin #195081 turns that into a Flutter message with the escape hatch: android.builtInKotlin=false and android.newDsl=false.
  • Existing projects: every project the migrators have run on already has an android.builtInKotlin entry, so this PR doesn't change them. C2 handles those.

Known limit: the migrations read only the project's gradle.properties. A value in ~/.gradle/gradle.properties or set with -P isn't seen. This is documented on usesBuiltInKotlinWithoutKotlinAndroidPlugin.

Tests:

  • Updated android_project_migration_test.dart. The existing tests that expect an opt-out to be written now set kotlinAndroidPlugin = true. Added tests for "app doesn't apply kotlin-android" (no file created, no flag added) for both migrations.
  • Replaced a file with only the migrator-written pair is not changed with removes the migrator-written pair when builtInKotlin is not set and keeps the pair when builtInKotlin is not set and the app applies kotlin-android. Reason: a missing entry now counts as built-in Kotlin.
  • create_test.dart asserts that new projects contain neither property.
  • No comments or docs removed. The doc comment on usesBuiltInKotlinWithoutKotlinAndroidPlugin describing the missing-entry case was rewritten.

Verification (AGP 9.3.1, KGP 2.4.20):

  • flutter create then flutter build apk --debug: gradle.properties contains only org.gradle.jvmargs and android.useAndroidX before and after, and the build succeeds.
  • Adding id("kotlin-android") to app/build.gradle.kts and building again: the migrators add both opt-outs, the FGP app warning appears, and the build succeeds.
  • flutter test test/general.shard/android/android_project_migration_test.dart: +66 passed.
  • flutter test test/commands.shard/permeable/create_test.dart: +181 passed.
  • flutter test test/integration.shard/android_plugin_example_app_build_test.dart: +2 passed.
  • dart analyze --fatal-infos and dart format passed on the changed Dart files.

Out of scope: the 33 in-repo apps that opt out (Chunk D) and test/integration.shard/test_data/deferred_components_project.dart, which writes android.newDsl=false explicitly.

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.
@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
@reidbaker

Copy link
Copy Markdown
Member

@reidbaker-agent should this stack or can it happen before those changes?

@reidbaker-agent

Copy link
Copy Markdown
Contributor Author

The built-in Kotlin portion of this PR can happen before #194460 (newDsl), and flipping the order is actually cleaner because the dependency only goes in one direction:

  • builtInKotlin=true works with newDsl=false (verified empirically on AGP 9.3.1 / KGP 2.4.20).
  • newDsl=true does not work with builtInKotlin=false (KGP refuses to apply when android.newDsl=true).

Why this branch is currently stacked on #194460

  1. It removes both android.builtInKotlin=false and android.newDsl=false from gradle.properties.tmpl, which requires [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460's DisableNewDslMigration skip (usesBuiltInKotlinWithoutKotlinAndroidPlugin) so flutter build does not write android.newDsl=false back.
  2. It uses AndroidProject.appliesKotlinAndroidPlugin in packages/flutter_tools/lib/src/project.dart, which was extracted in [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460.

How to land this before #194460

If we move AndroidProject.appliesKotlinAndroidPlugin (in project.dart) into this PR and have this PR drop only android.builtInKotlin=false from gradle.properties.tmpl (leaving android.newDsl=false in gradle.properties.tmpl for #194460 to remove), then:

  1. [AGP 9] Create Android projects with built-in Kotlin and the new DSL #195088 (built-in Kotlin in templates + DisableBuiltInKotlinMigration guard) and [AGP 9] Remove Flutter's built-in Kotlin opt-out when the app does not apply kotlin-android #195091 (RemoveBuiltInKotlinOptOutMigration) can land directly on master before [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460.
  2. [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460 (newDsl) then lands afterward and drops android.newDsl=false from both templates and migrated projects in one step.

@reidbaker-agent

Copy link
Copy Markdown
Contributor Author

Following up on the ordering trade-off: keeping #194460 first and #195088 stacked on #194460 is actually the better landing order:

  1. [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460 can land first without waiting on Built-in Kotlin ecosystem gating: Because [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460 only removes android.newDsl=false for projects that have already opted into android.builtInKotlin=true, it does not break apps using unmigrated kotlin-android plugins and can merge as soon as it is reviewed.
  2. [AGP 9] Create Android projects with built-in Kotlin and the new DSL #195088 and [AGP 9] Remove Flutter's built-in Kotlin opt-out when the app does not apply kotlin-android #195091 are gated on the Built-in Kotlin default flip (Force devs to opt-in to built-in Kotlin in 3.50 #189235 / ☔ [AGP 9] Ensure Internal and External Ecosystem is Migrated to Built-in Kotlin #184836): Turning on built-in Kotlin by default in templates ([AGP 9] Create Android projects with built-in Kotlin and the new DSL #195088) and existing projects ([AGP 9] Remove Flutter's built-in Kotlin opt-out when the app does not apply kotlin-android #195091) will fail builds for projects that depend on unmigrated third-party plugins (unless they add the escape hatch).
  3. Once [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460 merges to master, [AGP 9] Create Android projects with built-in Kotlin and the new DSL #195088 will automatically sit directly on master without rewriting branch history.

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.

[proposal] - flutter create templates ship outdated dependency versions and require manual built-in Kotlin migration

2 participants