Skip to content

fix(android): support AGP 9 built-in Kotlin - #161

Open
453zhangqiang-star wants to merge 1 commit into
fluttercandies:mainfrom
453zhangqiang-star:fix/agp9-built-in-kotlin
Open

453zhangqiang-star wants to merge 1 commit into
fluttercandies:mainfrom
453zhangqiang-star:fix/agp9-built-in-kotlin

Conversation

@453zhangqiang-star

Copy link
Copy Markdown

What

Migrate the Android build so it supports AGP 9 built-in Kotlin, while keeping the legacy
Kotlin Gradle Plugin (KGP) path working for older AGP versions.

Why

AGP 9 enables built-in Kotlin and no longer allows library subprojects to apply the
Kotlin Gradle Plugin. Flutter documents this migration and warns about plugins that
still apply KGP; future Flutter versions will fail to build them:

This pattern has already been merged upstream in other plugins, e.g. flutter_timezone 5.1.1,
wakelock_plus 1.7.0+, and sign_in_with_apple 8.2.0.

How

  • Apply KGP only when built-in Kotlin is not in effect (agpMajor < 9 || android.builtInKotlin != true).
  • Use pluginManager.apply(...) instead of the apply plugin: syntax, so Flutter's static
    KGP detection (which pattern-matches build file text) does not flag the plugin.
  • Keep kotlinOptions for the legacy path and use kotlin.compilerOptions when built-in Kotlin
    is active.
  • Change applies to image_editor_common/android/build.gradle.

No Dart or native API changes.

Testing

The change is build-configuration only and mirrors the exact structure already merged in
flutter_timezone 5.1.1, wakelock_plus 1.7.0+ and sign_in_with_apple 8.2.0.

I have not run this repo's example app build yet. If you'd like, I can run
flutter build apk for the example on Flutter 3.47.x / AGP 9.x before you review —
or feel free to trigger CI. Happy to adjust the structure if you prefer a different approach.

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

Changes requested: correct the built-in Kotlin mode detection before relying on the legacy fallback.

[P2] Preserve an explicit android.builtInKotlin=false — image_editor_common/android/build.gradle:30-33. Groovy's Elvis operator falls back for boolean false as well as null. With AGP 9 and the property set to false, the current expression still returns true, skips this script's KGP application, skips kotlinOptions, and selects the built-in-mode compiler configuration. This contradicts the documented opt-out and the fallback promised by this PR. Distinguish an absent property from an explicit false, and derive one AGP-aware mode value that consistently controls both plugin application and compiler configuration.

Reproduced with Groovy 3.0.22: AGP=9/property=false gives builtIn=true/applyKGP=false; AGP=8/property=true gives builtIn=true/applyKGP=true, also selecting inconsistent branches. Please cover AGP 8 legacy and AGP 9 with the flag unset, true, and false. Preserve the library's existing minimum Flutter version by keeping the older-KGP configuration path usable.

Caveat: Flutter 3.44/3.47 can auto-apply KGP to migrated plugins, which can mask the false-flag defect in a full Flutter build; I have reproduced the incorrect branch selection, not claimed an APK build failure. Validation included the full Gradle diff, the six-case Groovy truth table, and Flutter/Android migration documentation. No complete Android build was run, and this head has no CI results. Reviewed 390e87a.

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

Rechecked unchanged head 390e87a. The previous blocking mode-detection finding remains, so changes are still requested.

image_editor_common/android/build.gradle:30: Groovy's Elvis fallback treats explicit false as absent. With AGP 9 and android.builtInKotlin=false, builtInKotlinEnabled still becomes true. With AGP 8 and the property true, the plugin-application and compiler-configuration branches also disagree.

Distinguish an absent property from explicit false, then use one AGP-aware boolean consistently for plugin application, kotlinOptions, and compilerOptions. Cover AGP 8 and 9 with unset/true/false properties. The prior six-case reproduction remains applicable because this head and expression are unchanged. This review does not claim a new APK failure: Flutter's automatic KGP application can mask the branch-selection error. I have left the Gradle implementation for the author to correct.

@MilanJa

MilanJa commented Nov 3, 2026

Copy link
Copy Markdown

I've put up the requested mode-detection fix as a PR against this branch: 453zhangqiang-star#1. It checks android.builtInKotlin for presence instead of truthiness (so an explicit false is honoured), gates built-in mode on AGP 9+, and drives KGP application, kotlinOptions and compilerOptions from the one boolean. Includes the six-case truth table and a legacy-path build on AGP 8.12 / KGP 2.4.21. Merging it into fix/agp9-built-in-kotlin will update this PR.

@MilanJa

MilanJa commented Nov 3, 2026

Copy link
Copy Markdown

@CaiJingLong one question on process: if there's no response from the author within a week on merging the fix (453zhangqiang-star#1), would you like me to refile the updated change as a new PR directly against main so it doesn't stall? Happy to do that and credit this PR.

@CaiJingLong 关于流程的一个问题:如果一周内作者没有回复、没有合并修复(453zhangqiang-star#1),您是否希望我直接基于 main 重新提交一个包含更新内容的新 PR,以免卡住?我可以这样做,并注明来源于本 PR。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants