Skip to content

fix(android): support AGP 9 built-in Kotlin with correct mode detection - #164

Open
MilanJa wants to merge 2 commits into
fluttercandies:mainfrom
MilanJa:fix/agp9-built-in-kotlin-v2
Open

MilanJa wants to merge 2 commits into
fluttercandies:mainfrom
MilanJa:fix/agp9-built-in-kotlin-v2

Conversation

@MilanJa

@MilanJa MilanJa commented Nov 12, 2026

Copy link
Copy Markdown

What

Supersedes #161 (by @453zhangqiang-star, whose commit is kept as the first commit here with authorship intact) and applies the changes requested by @CaiJingLong in that review. Refiled against main as offered in the #161 discussion, since the fix PR on the author's fork (453zhangqiang-star#1) has had no response.

image_editor_common/android/build.gradle now supports AGP 9 built-in Kotlin while keeping the Kotlin Gradle Plugin (KGP) path for older AGP versions, so the plugin's minimum Flutter version is unchanged.

Why

AGP 9 enables built-in Kotlin by default and fails the build when a library subproject applies org.jetbrains.kotlin.android. Flutter warns about plugins that still do this and will stop building them in a future release:

How

  • agpMajor from com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.
  • One boolean drives everything:
    def builtInKotlinProperty = project.findProperty('android.builtInKotlin')
    def builtInKotlinEnabled = agpMajor >= 9 &&
        (builtInKotlinProperty == null || builtInKotlinProperty.toString().toBoolean())
    The property is checked for presence, not truthiness, so an explicit android.builtInKotlin=false is honoured (Groovy's ?: in fix(android): support AGP 9 built-in Kotlin #161 treated false as absent). Built-in Kotlin only exists on AGP 9+, so the property is ignored on AGP 8.
  • if (!builtInKotlinEnabled) pluginManager.apply('kotlin-android') (the pluginManager form keeps Flutter's static KGP detection from flagging the plugin), kotlinOptions { jvmTarget } only on the KGP path, kotlin { compilerOptions { jvmTarget } } only on the built-in path.
  • No Dart, native, or pubspec changes.

Review points from #161, and how each is addressed

Requested Done
Preserve an explicit android.builtInKotlin=false presence check instead of Elvis fallback
AGP 8 with property true must not pick built-in branches agpMajor >= 9 && gate
One AGP-aware boolean for plugin application, kotlinOptions, compilerOptions all three branches read builtInKotlinEnabled
Cover AGP 8 and 9 × unset / true / false truth table below
Keep the older-KGP path usable, keep the minimum Flutter version pubspec untouched; AGP 8 always takes the KGP path

Truth table (Gradle 9.3.1, Groovy 4.0.29; same logic, AGP major injected)

AGP android.builtInKotlin builtIn apply KGP kotlinOptions compilerOptions
8 unset false yes yes no
8 true false yes yes no
8 false false yes yes no
9 unset true no no yes
9 true true no no yes
9 false false yes yes no

Real builds (Flutter 3.47.9)

Path Project Result
AGP 9.1.1, Gradle 9.3.1, builtInKotlin=true image_editor/example (AGP/Gradle bumped locally, kotlin-android removed from the app module, app namespace made unique, see note) BUILD SUCCESSFUL, :image_editor_common:compileDebugKotlin ran, no KGP warnings
AGP 9.1.1, Gradle 9.3.1, builtInKotlin=false a production Flutter app with ~40 plugins, this branch via dependency_overrides APK built; image_editor_common no longer listed in Flutter's "plugins that apply KGP" warning (other plugins still are)
AGP 8.11.1, KGP 2.2.20, Gradle 8.14.3 (example app as-is) image_editor/example APK built, no KGP warnings

Note on the example app: it is not changed in this PR. When you move it to AGP 9 you will also need to give the app module its own namespace: it currently reuses com.fluttercandies.image_editor, which is the library's namespace, and AGP 9's manifest merger rejects duplicate namespaces (Namespace 'com.fluttercandies.image_editor' is used in multiple modules and/or libraries). Happy to include that here or in a follow-up, whichever you prefer.

🤖 Generated with Claude Code

zhangqaing and others added 2 commits November 12, 2026 14:15
… mode detection consistent

Groovy's Elvis operator treats `false` the same as `null`, so with AGP 9 and
`android.builtInKotlin=false` the previous expression still selected built-in
mode, skipped KGP and kotlinOptions, and configured kotlin.compilerOptions.
On AGP 8 with the property set to `true` the plugin-application and
compiler-configuration branches also disagreed.

Check the property for presence instead of truthiness, gate it on AGP 9+
(built-in Kotlin does not exist on AGP 8), and drive plugin application,
kotlinOptions and compilerOptions from the single resulting boolean.

Truth table (AGP x property -> builtIn / applyKGP):
  8 x unset -> false / true      9 x unset -> true  / false
  8 x true  -> false / true      9 x true  -> true  / false
  8 x false -> false / true      9 x false -> false / true

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.

2 participants