Migrating build logic from Groovy to Kotlin (KTS)
Migrate all .gradle (groovy files) to .gradle.kts (Kotlin DSL)
Benefits Auto-completion and content assist Quick documentation Navigation to source Refactoring and more
Why Migrate? You already highlighted key benefits:
Better auto-completion and IDE support
Safer type checking
Easier refactoring
Improved readability
Migration Steps
- Convert build.gradle (app-level) to build.gradle.kts Rename: mv app/build.gradle app/build.gradle.kts Translate syntax (Groovy → Kotlin):
Groovy:
plugins { id 'com.android.application' id 'kotlin-android' }
android { compileSdkVersion 34 defaultConfig { applicationId "com.example.myapp" minSdkVersion 21 targetSdkVersion 34 versionCode 1 versionName "1.0" } } Kotlin DSL:
plugins { id("com.android.application") id("org.jetbrains.kotlin.android") }
android { compileSdk = 34 defaultConfig { applicationId = "com.example.myapp" minSdk = 21 targetSdk = 34 versionCode = 1 versionName = "1.0" } } 2. Convert build.gradle (project-level) to build.gradle.kts Rename:
mv build.gradle build.gradle.kts Replace repository and classpath blocks accordingly.
Groovy:
buildscript { repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:8.3.0" } } Kotlin DSL:
buildscript { repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:8.3.0") } } 3. Update settings.gradle → settings.gradle.kts Rename:
mv settings.gradle settings.gradle.kts Convert:
rootProject.name = "MyApp" include(":app") 4. Update gradle.properties and local.properties No change in syntax — these are already Kotlin-agnostic.
- Update plugins and repositories if using plugins {} block For example, in build.gradle.kts:
plugins { alias(libs.plugins.androidApplication) alias(libs.plugins.kotlinAndroid) } Common Gotchas All string values must use = "value" syntax (no :).
Use fileTree(mapOf(...)) instead of Groovy-style maps.
buildConfigField and manifestPlaceholders require type-safe syntax:
buildConfigField("String", "BASE_URL", ""https://example.com"") Final Step: Sync & Build Once migrated:
Sync Gradle from Android Studio.
Run a build to ensure no regressions.