diff --git a/.changes/agp9-built-in-kotlin b/.changes/agp9-built-in-kotlin new file mode 100644 index 000000000..bb0857676 --- /dev/null +++ b/.changes/agp9-built-in-kotlin @@ -0,0 +1 @@ +patch type="fixed" "Android plugin compatibility with AGP 9 built-in Kotlin" diff --git a/android/build.gradle b/android/build.gradle index dbc0cb96b..17fa1f448 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -2,7 +2,7 @@ group = "io.livekit.plugin" version = "1.0-SNAPSHOT" buildscript { - ext.kotlin_version = "1.8.22" + ext.kotlin_version = "2.1.0" repositories { google() mavenCentral() @@ -22,7 +22,18 @@ allprojects { } apply plugin: "com.android.library" -apply plugin: "kotlin-android" + +// AGP 9's built-in Kotlin compiles Kotlin itself and rejects the Kotlin Gradle +// Plugin. Apply KGP only when built-in Kotlin is NOT active: that means AGP < 9, +// or AGP 9 with android.builtInKotlin=false (the configuration Flutter currently +// ships by default while the ecosystem migrates). +def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize(".")[0] as int +def builtInKotlinActive = agpMajor >= 9 && + (!project.hasProperty("android.builtInKotlin") || + Boolean.parseBoolean(project.property("android.builtInKotlin").toString())) +if (!builtInKotlinActive) { + apply plugin: "kotlin-android" +} android { if (project.android.hasProperty("namespace")) { @@ -36,10 +47,6 @@ android { targetCompatibility = JavaVersion.VERSION_1_8 } - kotlinOptions { - jvmTarget = JavaVersion.VERSION_1_8 - } - sourceSets { main.java.srcDirs += "src/main/kotlin" test.java.srcDirs += "src/test/kotlin" @@ -68,3 +75,19 @@ android { } } } + +// Configure the Kotlin JVM target. The compilerOptions DSL requires KGP 1.9+ or +// AGP 9 built-in Kotlin; older Flutter app templates ship KGP 1.8.x, which only +// supports the legacy kotlinOptions DSL. +def kotlinExt = project.extensions.findByName("kotlin") +if (kotlinExt?.hasProperty("compilerOptions")) { + kotlin { + compilerOptions { + jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8 + } + } +} else { + android.kotlinOptions { + jvmTarget = JavaVersion.VERSION_1_8.toString() + } +}