KGP warning on every AGP 9 build: literal `apply plugin: 'kotlin-android'` in a branch that never runs

Open Beginner friendly
#731 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
dart, flutter

Research direction

Start with android/build.gradle lines 29–41 and the Flutter warning regex in flutter_tools/gradle/src/main/kotlin/FlutterPluginUtils.kt cited in the report. Check how the conditional apply plugin lines are detected as text, then run the stated Flutter release build with AGP 9 and built-in Kotlin enabled. Done means the build succeeds without the false KGP warning while preserving the AGP 8 path.

Written by the indexing model from the issue text.

Description

Android bug
Version

5.61.0

Steps to Reproduce
  1. Flutter app on AGP 9.x with android.builtInKotlin=true in android/gradle.properties.
  2. Add posthog_flutter: ^5.61.0.
  3. flutter build appbundle --release.

Environment: Flutter 3.50.0 stable (ab2912035e), Dart 3.14.0, AGP 9.3.1, Gradle 9.5.0, Kotlin 2.4.20, JDK 21,
android.builtInKotlin=true, android.newDsl=false.

Expected Result

No KGP warning. posthog_flutter 5.61.0 is already built-in-Kotlin compatible — android/build.gradle:29-41 reads
android.builtInKotlin and does not apply KGP when it is on, so there is nothing for Flutter to warn about.

Actual Result

The build succeeds, but prints on every run:

WARNING: Your app uses the following plugins that apply Kotlin Gradle Plugin (KGP): posthog_flutter
Future versions of Flutter will fail to build if your app uses plugins that apply KGP.

This is a false positive. Flutter detects KGP by regex over the build-script text, not by runtime plugin state
(flutter_tools/gradle/src/main/kotlin/FlutterPluginUtils.kt, kgpRegexGroovy):

^[ \t]*apply[ \t]+plugin[ \t]*:[ \t]*(['"])(?:kotlin-android|org\.jetbrains\.kotlin\.android)\1

android/build.gradle:29-41 contains the literal on two lines, and both match even though neither executes in this
configuration:

def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0] as int
if (agpMajor < 9) {
    apply plugin: 'kotlin-android'          // line 31 — not reached on AGP 9
} else {
    def builtInKotlin = providers.gradleProperty('android.builtInKotlin')
        .map { it.toBoolean() }
        .getOrElse(true)
    if (!builtInKotlin) {
        apply plugin: 'kotlin-android'      // line 39 — false when built-in Kotlin is on
    }
}

getSubprojectPluginState() reads the file and regex-matches it; the comment there says text matching was chosen
deliberately over querying plugin state, to avoid Gradle lifecycle/ordering issues (gradle/gradle#36953). So no
conditional structure avoids the match — only not writing the literal.

That the plugin really isn't applying KGP is provable: AGP 9 hard-fails when a library applies kotlin-android while
built-in Kotlin is on —

Failed to apply plugin 'org.jetbrains.kotlin.android'
The 'org.jetbrains.kotlin.android' plugin is no longer required for Kotlin support since AGP 9.0.

I hit exactly that error from a different plugin in the same project and the build stopped outright. With
posthog_flutter it builds fine, so line 39 is provably not executing.

This matters beyond the noise: Flutter says future versions will fail builds using plugins that apply KGP. If that
check becomes an error while still being textual, posthog_flutter will break those builds despite being compatible.

Suggested fix — keep the AGP 8 path but avoid the literal string:

if (agpMajor < 9) {
    project.pluginManager.apply('kotlin-android')
}

That does not match the regex. Dropping the AGP < 9 branch once AGP 9 is the floor would also work. Note the
!builtInKotlin branch on line 39 is unreachable in practice: when built-in Kotlin is off, Flutter's own Gradle
plugin applies kotlin-android to plugin subprojects that lack it, so posthog doesn't need to.

Dominant language
Dart
Stars
99
Forks
94
Avg merge
2d 4h
Merged PRs (30d)
65

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PostHog/posthog-flutter

All issues in PostHog/posthog-flutter

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.