Migrating an Android build to Gradle Kotlin DSL on AGP 8.4
I migrated our Android build from build.gradle to build.gradle.kts last month. We're on AGP 8.4.0, Gradle 8.6, Kotlin 1.9.24. I would do it again, but I'd also tell you that about 70% of the pain was syntax I should have known going in and 30% was plugin code I didn't know existed.
## The first error
Started with the obvious conversion, replacing implementation 'androidx.core:core-ktx:1.13.1' with implementation("androidx.core:core-ktx:1.13.1"). Then Gradle sync died with:
e: file:///home/dana/src/app/build.gradle.kts:41:13: Unresolved reference: minSdkVersion
Right. AGP 8 removed minSdkVersion entirely and Kotlin DSL doesn't have the old Groovy dynamic-property fallback. The block has to be:
android {
namespace = "com.example.app"
compileSdk = 35 defaultConfig {
applicationId = "com.example.app"
minSdk = 24
targetSdk = 35
versionCode = 42
versionName = "2.4.0"
}
buildTypes {
getByName("release") {
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}
The = signs and the explicit getByName("release") are the two things that tripped me up. In Groovy, release { } works because Gradle creates the method dynamically. In Kotlin it's a compile error.
If you're converting an existing build, do it with a from-scratch file rather than a search-and-replace. I did it the lazy way first and ended up with a half-converted Groovy file that Gradle parsed in ways I didn't expect. Deleting the file and writing the Kotlin version on a clean page took less time than fixing the hybrid.
## The plugin block
Top-level buildscript with classpath is gone. This is cleaner:
plugins {
id("com.android.application") version "8.4.0" apply false
id("org.jetbrains.kotlin.android") version "1.9.24" apply false
}
And in the app module:
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
The gotcha: if a plugin is declared with apply false at the root, you must not repeat version in the subproject block, or you get:
* What went wrong:
Plugin request for plugin already on the classpath must not include a version
## The part nobody warned me about
We have a subprojects { } block in the root build that configures shared JUnit settings. In Kotlin DSL, subprojects is fine, but accessing an Android extension from it needs:
subprojects {
plugins.withId("com.android.library") {
extensions.configure<com.android.build.gradle.LibraryExtension> {
compileSdk = 35
}
}
}
Without the plugins.withId guard, configuration runs before the plugin applies and you get an unknown extension error. Took me an afternoon.
## Would I do it again
Yes, mostly because the IDE actually knows what minSdk is now, autocomplete works and refactoring a dependency name updates every usage. The first week was slower, not faster. If your build is 200 lines and stable, I'm not sure the migration buys you much. Ours was 600 lines across four modules and a lot of copy-paste, so the type safety earns its keep.
One thing I still haven't solved elegantly: build logic shared between modules lives in a buildSrc directory, which adds another compile step. If you've got a cleaner pattern for that on AGP 8, say something.