From Gradle to Gradle.kts: The definitive Groovy to Kotlin conversion guide.

davthecoder
4 min readNov 27, 2021

Files with .kts extension are not new, but at some point, in your Kotlin and/or Android development career you are likely to require converting your .gradle files(groovy) into .gradle.kts (Kotlin).

The main purpose of this article is to show you by example the code changes or updates required. Some changes are quite obvious, unfortunately, this is not always the case and the lacking of documentation or clear error logs or messages can make this job frustrating.

Note: In this guide, I will use an Android Project, but you can use these examples for Any Kotlin projects.

  1. The first thing you would need to do is add .kts to your .gradle file:
    build.gradle > build.gradle.kt
add .kts extension to Gradle file.

2. The following snippet will be showing the same code in Groovy and Kotlin. The updates are quite simple and clear, however, I will point them out to make them clear.

  • Add buildscript > repositories: gradlePluginPortal()
  • Add Parenthesis to the Classpath dependencies and use ALWAYS double quotes (“) instead of single () for the dependencies
  • Refactor the Gradle task “Clean”:
    - use tasks variable and use register inline extension function and pass 3 parameters: name of the task, type Class, and a lambda Action callback.
    - add parenthesis after delete: delete(rootProject.buildDir)
// From Groovy:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31'
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
// To Kotlin:
buildscript {
repositories {
gradlePluginPortal() <- add this plugin

google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:7.0.3")
classpath
("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31") <- parenthesis and from single to double quotes
}
}

tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
} <- Refactor the code

At this point, we did convert build.gradle from the project into .kts

Now we will convert the build.gradle file from the application itself into .kts

  1. Usually allocated at the top part of the file we have Plugins:
    - for any id can still use id but with parenthesis and using always double quotes
    - for Kotlin plugins it can be replaced id for Kotlin: kotlin(“name”)
// From Groovy
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
// To Kotlin
plugins {
id("com.android.application") <- Parenthesis and double quotes

kotlin("android") <- Replace id for Kotlin and double quotes
kotlin(
"kapt")
}

2. Dependencies are quite simple to update the code to .kts you just need to add parenthesis after DependencyHandler and use double quotes for the dependency. here are some examples of different DependencyHandlers.

// Groovy
dependencies
{
implementation project(path: ':your-module') implementation 'androidx.core:core-ktx:1.7.0' debugImplementation 'androidx.compose.ui:ui-tooling:1.0.5' kapt 'androidx.room:room-compiler:2.3.0' testImplementation 'junit:junit:4.13.2'

androidTestImplementation '
androidx.test.ext:junit:1.1.3'
}// Kotlin
dependencies
{
implementation(project(":your-module")) implementation("androidx.core:core-ktx:1.7.0") debugImplementation("androidx.compose.ui:ui-tooling:1.0.5") kapt("androidx.room:room-compiler:2.3.0") testImplementation("junit:junit:4.13.2")

androidTestImplementation(
"androidx.test.ext:junit:1.1.3")
}

3. If the project uses the Kapt Kotlin plugin, you can still enable/disable variables by just adding an equal sign. this equal sign is a Must in each variable in Kotlin, however, in Groovy you can use an equal sign or not.

// From Groovy:
kapt {
correctErrorTypes true
}
// To Kotlin:
kapt {
correctErrorTypes = true
}

4. The following example is quite extensive, but the changes are simple, just pay attention to the Kotlin code set as BOLD for differences:
- all buildTypes need to be declared as getByName(“debug”).
- minifyEnabled needs to be replaced for isMinifyEnabled.

For the rest of the code, please follow the following guide:
- Variables need to have an equal sign.
- Replace all single quotes with double quotes.
- Use always parenthesis for functions such as proguardFiles.

// From Groovy:
android {
compileSdk 31

defaultConfig {
applicationId "com.domain.appname"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
useIR = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerVersion '1.5.21'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
// To Kolin:
android
{
compileSdk = 31

defaultConfig {
applicationId = "com.domain.appname"
minSdk = 23
targetSdk = 31
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
getByName("debug")
{ <- change from debug {}
isMinifyEnabled = false <- change from minifyEnabled

}
getByName("release") {
isMinifyEnabled = true
proguardFiles( <- add parenthesis
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
useIR = true
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = Versions.Compose
}
packagingOptions {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

Usually, this process is straight forwards, however after you change the code and the extensions correctly chances are of your IDE, in my case Android Studio, will show errors. You easy fix those by cleaning and rebuilding or cleaning the cache and restarting:
Build > Clean Project & Build > Rebuild Project
or
File > Invalidate Caches / Restart

I hope this article helps you to convert easy and efficient files from Groovy to Kotlin.

If you have issues or questions please feel free to share in the comments.

Happy Coding!
David Cruz

Let’s Keep in touch:
davthecoder Web: https://www.davthecoder.com
Medium Blog: https://davthecoder.medium.com/
Twitch: https://www.twitch.tv/davthecoder
GitHub: https://github.com/davthecodercom
Instagram: https://www.instagram.com/davthecoder
Twitter: https://twitter.com/davthecoder
Youtube: https://youtube.com/@davthecoder
Support me on Patreon! https://www.patreon.com/davthecoder

--

--