From 392bf024281fc3df2af6b301fa5d94ff5dc832f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20G=C3=B6ster?= Date: Wed, 17 Jun 2026 16:35:49 +0200 Subject: [PATCH] fix(package_info_plus): support AGP 9 / built-in Kotlin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AGP 9 removes support for applying the Kotlin Gradle Plugin (KGP). The Android module applies `kotlin-android` directly, so apps that depend on package_info_plus fail to build under AGP 9, and Flutter 3.44+ already warns about it. Apply KGP only when built-in Kotlin is inactive (AGP < 9, or `android.builtInKotlin=false` — the Flutter transition default), and migrate the deprecated `kotlinOptions` block to the `kotlin { compilerOptions { } }` DSL. The example app is migrated to built-in Kotlin as well. No change to the SDK range, compileSdk (uses flutter.compileSdkVersion), Kotlin version (2.2.0), or runtime. Verified by building the example with Flutter 3.44.2 on AGP 8.12 (KGP applied), AGP 9.0.1 + Gradle 9.1.0 with builtInKotlin=false (KGP applied via Flutter's shim) and builtInKotlin=true (package_info_plus is KGP-free and configures cleanly under built-in Kotlin). Closes #3881 Co-Authored-By: Claude Opus 4.8 --- .../package_info_plus/android/build.gradle | 27 +++++++++++++++---- .../example/android/app/build.gradle | 11 ++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/package_info_plus/package_info_plus/android/build.gradle b/packages/package_info_plus/package_info_plus/android/build.gradle index f03c20d32f..25ebf86b61 100644 --- a/packages/package_info_plus/package_info_plus/android/build.gradle +++ b/packages/package_info_plus/package_info_plus/android/build.gradle @@ -23,7 +23,19 @@ rootProject.allprojects { } apply plugin: 'com.android.library' -apply plugin: 'kotlin-android' + +// Built-in Kotlin migration (AGP 9): AGP 9 removes support for applying the Kotlin Gradle +// Plugin (KGP) directly and provides built-in Kotlin instead. Apply KGP only when built-in +// Kotlin is NOT active — i.e. on AGP < 9, or when `android.builtInKotlin` is disabled (the +// Flutter transition default, which Flutter still sets to false even on AGP 9). This keeps the +// plugin building across both the legacy-KGP and built-in-Kotlin paths, and stops applying KGP +// once the host enables built-in Kotlin on AGP 9+. +// https://docs.flutter.dev/release/breaking-changes/migrate-to-built-in-kotlin/for-plugin-authors +def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0] as int +def builtInKotlin = providers.gradleProperty('android.builtInKotlin').map { it.toBoolean() }.orElse(agpMajor >= 9).get() +if (agpMajor < 9 || !builtInKotlin) { + apply plugin: 'kotlin-android' +} android { namespace 'dev.fluttercommunity.plus.packageinfo' @@ -34,10 +46,6 @@ android { targetCompatibility JavaVersion.VERSION_17 } - kotlinOptions { - jvmTarget = 17 - } - defaultConfig { minSdk 19 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -51,3 +59,12 @@ android { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } } + +// Replaces the deprecated `kotlinOptions { jvmTarget = ... }` block, which is removed alongside +// KGP under built-in Kotlin. The `kotlin` extension is registered by KGP on AGP < 9 and by +// built-in Kotlin on AGP 9+, so this configures the Kotlin compiler on both. +kotlin { + compilerOptions { + jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17 + } +} diff --git a/packages/package_info_plus/package_info_plus/example/android/app/build.gradle b/packages/package_info_plus/package_info_plus/example/android/app/build.gradle index fb70d7caf2..4637e93607 100644 --- a/packages/package_info_plus/package_info_plus/example/android/app/build.gradle +++ b/packages/package_info_plus/package_info_plus/example/android/app/build.gradle @@ -1,6 +1,5 @@ plugins { id "com.android.application" - id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" } @@ -31,10 +30,6 @@ android { targetCompatibility JavaVersion.VERSION_17 } - kotlinOptions { - jvmTarget = 17 - } - sourceSets { main.java.srcDirs += 'src/main/kotlin' } @@ -60,6 +55,12 @@ android { } } +kotlin { + compilerOptions { + jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17 + } +} + flutter { source '../..' }