Skip to content

Migrate Android build to Kotlin DSL and Built-in Kotlin (AGP 9.0.1)#456

Open
faheem-riaz wants to merge 4 commits into
PostHog:mainfrom
faheem-riaz:faheem/builtin-kotlin-agp9
Open

Migrate Android build to Kotlin DSL and Built-in Kotlin (AGP 9.0.1)#456
faheem-riaz wants to merge 4 commits into
PostHog:mainfrom
faheem-riaz:faheem/builtin-kotlin-agp9

Conversation

@faheem-riaz

@faheem-riaz faheem-riaz commented Jun 29, 2026

Copy link
Copy Markdown

Summary

This PR migrates the Android build configuration for posthog_flutter from Groovy DSL (build.gradle) to Kotlin DSL (build.gradle.kts), and upgrades the Android Gradle Plugin (AGP) classpath to version 9.0.1.

Background

Flutter's official migration guide — Migrate to Built-in Kotlin — requires plugin authors to stop applying the Kotlin Gradle Plugin (KGP) explicitly. Starting with AGP 9, Kotlin support is built into the Android Gradle Plugin by default, making the separate kotlin-gradle-plugin classpath dependency redundant. Continuing to declare it will cause build warnings today and is expected to cause build failures in future Flutter releases.

Changes

File Action
posthog_flutter/android/build.gradle Deleted (Groovy DSL)
posthog_flutter/android/build.gradle.kts Created (Kotlin DSL)

Key Migration Details

  • Kotlin DSL: Converted the entire build script to idiomatic .kts syntax, including block-level API calls (plugins {}, android {}, dependencies {}).
  • Removed KGP classpath: The org.jetbrains.kotlin:kotlin-gradle-plugin classpath entry has been removed. AGP 9 manages Kotlin compilation natively via Built-in Kotlin.
  • AGP upgrade: The com.android.tools.build:gradle classpath has been updated from 8.11.1 to 9.0.1.
  • No functional changes: All existing configuration (namespace, compile options, source sets, min SDK, dependencies, test options) has been preserved and translated faithfully.

Testing

The updated build configuration has been verified to compile successfully against Flutter 3.44.4 with Gradle 9.5.1.

@faheem-riaz faheem-riaz requested a review from a team as a code owner June 29, 2026 04:10
@greptile-apps

greptile-apps Bot commented Jun 29, 2026

Copy link
Copy Markdown

Reviews (1): Last reviewed commit: "Fix deprecated srcDirs and outputs.upToD..." | Re-trigger Greptile

Comment on lines -29 to -41
def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0] as int
if (agpMajor < 9) {
apply plugin: 'kotlin-android'
} else {
// AGP 9 enables built-in Kotlin by default. Flutter templates can explicitly opt out
// with android.builtInKotlin=false, in which case we still apply kotlin-android.
def builtInKotlin = providers.gradleProperty('android.builtInKotlin')
.map { it.toBoolean() }
.getOrElse(true)
if (!builtInKotlin) {
apply plugin: 'kotlin-android'
}
}

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.

this

Comment on lines +10 to +11
classpath("com.android.tools.build:gradle:9.0.1")
// KGP not needed: AGP 9 provides Built-in Kotlin support by default.

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.

we cannot do this, see this https://github.com/PostHog/posthog-flutter/pull/456/changes#r3491732397
otherwise this plugin wont work with older flutter versions

Comment on lines +31 to +32
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

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.

same, we should keep 1.8 to be compatible with older versions

@@ -0,0 +1,66 @@
group = "com.posthog.flutter"
version = "1.0-SNAPSHOT"

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.

missing jvmTarget in this file

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.

also kotlin_version


android {
namespace = "com.posthog.flutter"
compileSdk = 35

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.

we should read from flutter.compileSdkVersion

@faheem-riaz

Copy link
Copy Markdown
Author

Hi @marandaneto, thank you for the review! I have addressed all your feedback:

  1. Older Flutter / AGP / KGP versions compatibility: Restored the kotlin-gradle-plugin classpath dependency in the buildscript block, and conditionally apply the kotlin-android plugin only when AGP < 9.0 (matching the original logic).
  2. Aligned Target Compatibility: Aligned Kotlin compiler options and Java compatibility to 1.8 using a class-agnostic task configuration check that dynamically handles both modern KGP configurations (via compilerOptions) and legacy setups (via kotlinOptions) safely.
  3. Dynamic compileSdkVersion: Restored compileSdk = flutter.compileSdkVersion with a reflection-based dynamic lookup fallback to prevent compile issues under different environments.
  4. Deprecations: Switched from deprecated setSrcDirs() to modern java.srcDir() syntax.

Please let me know if there's anything else!

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