Skip to content

FIX(android): support AGP 9 built-in Kotlin (closes #46) - #47

Open
tetianazakhozhaeva wants to merge 2 commits into
yanncabral:mainfrom
tetianazakhozhaeva:fix/agp9-built-in-kotlin
Open

tetianazakhozhaeva wants to merge 2 commits into
yanncabral:mainfrom
tetianazakhozhaeva:fix/agp9-built-in-kotlin

Conversation

@tetianazakhozhaeva

Copy link
Copy Markdown

Status

READY

Description

Closes #46.

AGP 9 enables built-in Kotlin by default, and the Kotlin Gradle Plugin refuses to be applied when it is on
("The 'org.jetbrains.kotlin.android' plugin is no longer required…"), so apps using this plugin had to keep
android.builtInKotlin=false.

android/build.gradle now:

  • applies kotlin-android only on AGP < 9 or when the app sets android.builtInKotlin=false
    (same pattern as FlutterFire), so older setups are unaffected;
  • replaces android.kotlinOptions with a top-level kotlin { compilerOptions { jvmTarget.set(JVM_11) } },
    which works with both KGP and built-in Kotlin;
  • drops main.java.srcDirs += 'src/main/kotlin' (a default source dir; adding Kotlin dirs to java is
    unsupported with built-in Kotlin).

Tested with a Flutter 3.47.2 app (AGP 9.1.0, KGP 2.4.0, Gradle 9.3.1): builds with
android.builtInKotlin=false (compiled by KGP) and =true (compiled by built-in Kotlin).

Type of Change

  • ✅ Build configuration change

Apply kotlin-android only on AGP < 9 or when the app opts out of built-in
Kotlin (android.builtInKotlin=false).

Copy link
Copy Markdown
Owner

@tetianazakhozhaeva Thanks for contributing this fix! Supporting AGP 9 built-in Kotlin is important: applying kotlin-android unconditionally prevents consumers from enabling it without a workaround. The conditional plugin application and source directory cleanup address a real compatibility issue, and the change stays nicely scoped to Android build configuration.

Before merging, I would like to address two points:

  1. Preserve compatibility with older Kotlin Gradle Plugin versions, or explicitly update the minimum requirements. The new top-level kotlin { compilerOptions { ... } } block runs unconditionally and requires KGP 2.0.0 or newer. However, this library still declares Kotlin 1.8.20 in android/build.gradle and supports Flutter >=3.41.0 in pubspec.yaml. Conditional plugin application alone therefore does not guarantee that older setups remain unaffected. The Flutter migration guide for plugin authors explicitly calls out the KGP 2.0.0 requirement and recommends Flutter >=3.44 for that migration.

    Preferably, keep a compatible compiler configuration for the older KGP path. With built-in Kotlin, an explicit jvmTarget is optional because it inherits android.compileOptions.targetCompatibility (11 here), as documented in the Android migration guide. Alternatively, explicitly raise and align the Kotlin/Flutter/Dart requirements and document the compatibility change in the changelog.

  2. Add Android build validation. The reported AGP 9 tests with built-in Kotlin enabled and disabled are useful, but there is no AGP 8 validation in the PR description. At review time, the CI run was in action_required, and the current workflow builds the iOS example but does not build Android, so a green run of that workflow alone would not verify this change. Please add Android build coverage for:

    • AGP 8 with an older KGP version supported by the library, if preserving the existing requirements;
    • AGP 9 with built-in Kotlin enabled;
    • AGP 9 with built-in Kotlin disabled, using the required opt-out settings.

I support merging this fix once the compatibility policy is reflected in the implementation and these Android builds are validated. This review is based on the diff, issue, workflow, and official documentation; I have not independently run the builds locally because Flutter and a Java runtime are unavailable in the review environment.

Review follow-up on yanncabral#46.

Compatibility: the top-level kotlin { compilerOptions { ... } } block ran
unconditionally and required KGP 2.0+, which would have raised this library's
minimum (it declares Kotlin 1.8.20 and supports Flutter >= 3.41). Drop it and
restore the original kotlinOptions under the same guard as the plugin apply, so
the KGP path is byte-identical to before this PR and no minimum changes. Under
built-in Kotlin jvmTarget is inherited from compileOptions.targetCompatibility
(verified: the compiled class is major version 55, i.e. Java 11).
`agpMajor < 9 ||` is now redundant — builtInKotlin is already false below AGP 9.

Apply KGP through pluginManager.apply rather than `apply plugin:`: the Flutter
tool scans this file for that literal to warn consumers about plugins applying
KGP and cannot see the guard, so an already-migrated plugin was still listed.
Both forms apply the same plugin at the same point; with this one the warning no
longer names open_settings_plus on either KGP path, and its Kotlin still
compiles (classes under tmp/kotlin-classes).

CI: add a validate-android job building the example for the three combinations
this plugin must support (AGP 8 + KGP, AGP 9 + built-in Kotlin, AGP 9 with
android.builtInKotlin=false). Each entry patches the example's AGP/KGP/Gradle
versions and gradle.properties; the built-in Kotlin entry also drops
kotlin-android and kotlinOptions from the example app, which is the migration a
consumer app has to make as well.

The AGP 8 entry uses Gradle 8.14.3 / KGP 2.2.20 rather than the example's
committed 8.13 / 2.2.10: the Flutter Gradle plugin enforces those minimums, so
the example does not build on current stable Flutter as-is.
@tetianazakhozhaeva

Copy link
Copy Markdown
Author

Thanks for the careful review — both points addressed in 7cb887a.

1. KGP compatibility. Dropped the top-level kotlin { compilerOptions { … } } block (it required
KGP 2.0+) and restored the original kotlinOptions under the same guard as the plugin application,
so the KGP path is byte-identical to before this PR and the declared minimums stay as they are. Under
built-in Kotlin jvmTarget is inherited from compileOptions.targetCompatibility, as you noted —
verified, not assumed: the compiled class is major version 55 (Java 11).

I also switched to pluginManager.apply('kotlin-android'). The Flutter tool scans this file for the
apply plugin: literal to warn consumers about plugins that apply KGP and can't see the guard, so an
already-migrated plugin was still being reported. Both forms apply the same plugin at the same point;
with this one the warning no longer names open_settings_plus, and its Kotlin still compiles.

2. Android build validation. Added a validate-android matrix job covering all three
combinations. Local results on Flutter 3.47.2:

ConfigResultPlugin's Kotlin compiled by
AGP 8.13.2, KGP 2.2.20, Gradle 8.14.3passKGP (tmp/kotlin-classes/debug)
AGP 9.1.1, KGP 2.4.20, Gradle 9.3.1, built-in KotlinpassAGP built-in (intermediates/built_in_kotlinc/…)
AGP 9.1.1, KGP 2.4.20, Gradle 9.3.1, android.builtInKotlin=falsepassKGP (tmp/kotlin-classes/debug)

The output directory is what confirms the guard dispatches: tmp/kotlin-classes only when
kotlin-android is applied, built_in_kotlinc otherwise.

Two notes. example/android pins Gradle 8.13 and KGP 2.2.10 in the repo, both below the Flutter
Gradle plugin's enforced minimums (8.14.0 / 2.2.20), so the example doesn't build on current stable
Flutter as-is; the matrix patches versions per entry at CI runtime, so no example file changes in
this PR. A side effect is that KGP 1.8.20 can't be exercised by CI on stable Flutter at all — the
other reason I left the old path untouched instead of relying on tests. And on AGP 9,
android.newDsl=false is required either way, because Flutter's own Gradle plugin still uses the
legacy variant API (flutter/flutter#180137).

This branch has not been deployed

No deployments
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.

AGP 9 built-in Kotlin support

2 participants