Repository navigation
[AGP 9] Create Android projects with built-in Kotlin and the new DSL - #195088
Draft
reidbaker-agent wants to merge 36 commits into
Draft
reidbaker-agent wants to merge 36 commits into
reidbaker-agent wants to merge 36 commits into
Conversation
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.
Draft
11 tasks
11 tasks
Member
|
@reidbaker-agent should this stack or can it happen before those changes? |
Contributor
Author
|
The built-in Kotlin portion of this PR can happen before #194460 (
Why this branch is currently stacked on #194460
How to land this before #194460If we move
|
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
flutter createmakes Android projects that use built-in Kotlin and the new DSL, the Android Gradle Plugin defaults. Existing projects whose app applieskotlin-androidstill getandroid.builtInKotlin=falseadded.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 breaklabel, and flutter-announce and#announcementsposts.Agent authored details
Changes:
templates/app/android.tmpl/gradle.properties.tmplandtemplates/module/android/gradle/gradle.properties.tmplno longer write theandroid.newDsl=falseandandroid.builtInKotlin=falsepairs.DisableBuiltInKotlinMigrationreturns early unlessAndroidProject.appliesKotlinAndroidPlugin(from [AGP 9.1.0 Migration #10] Enable newDsl for built-in Kotlin projects #194460) is true. Without this guard it would writeandroid.builtInKotlin=falseback into every new project on its first build.usesBuiltInKotlinWithoutKotlinAndroidPlugintreats a missingandroid.builtInKotlinentry as enabled, which matches the Android Gradle Plugin default andFlutterPluginUtils.isBuiltInKotlinEnabled.DisableNewDslMigrationapplies the same check whengradle.propertiesis missing. So new projects also skip the newDsl opt-out.Who is affected:
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=falseandandroid.newDsl=false.android.builtInKotlinentry, 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.propertiesor set with-Pisn't seen. This is documented onusesBuiltInKotlinWithoutKotlinAndroidPlugin.Tests:
android_project_migration_test.dart. The existing tests that expect an opt-out to be written now setkotlinAndroidPlugin = true. Added tests for "app doesn't apply kotlin-android" (no file created, no flag added) for both migrations.a file with only the migrator-written pair is not changedwithremoves the migrator-written pair when builtInKotlin is not setandkeeps 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.dartasserts that new projects contain neither property.usesBuiltInKotlinWithoutKotlinAndroidPlugindescribing the missing-entry case was rewritten.Verification (AGP 9.3.1, KGP 2.4.20):
flutter createthenflutter build apk --debug:gradle.propertiescontains onlyorg.gradle.jvmargsandandroid.useAndroidXbefore and after, and the build succeeds.id("kotlin-android")toapp/build.gradle.ktsand 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-infosanddart formatpassed 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 writesandroid.newDsl=falseexplicitly.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-assistbot 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.