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

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)

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”)

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.

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.

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.

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
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’s Web:
Contact me:
Medium Blog:
Support me on Patreon!