Migrating from Groovy DSL to Gradle Kotlin DSL

Migrating from Groovy DSL to Gradle Kotlin DSL

As Android Developers, we tend to write a little bit of Groovy (default programming language for writing Gradle scripts) code in our Gradle files as it is the spot where all dependency related information is stored, so if your app needs to use an external library like Material Design Components, you have to write this line of code in your build.gradle file:

implementation 'com.google.android.material:material:material_version'

Nowadays, since Google I/O 2017, we write majority of our codebase in Kotlin, how nice would it be to also be able to do the same when specifying dependencies in our build.gradle files?

In this article, we are going to walk through the process of migrating a project from the default Groovy DSL to Kotlin DSL while writing plain Kotlin code.

Let's get started :)

Migrating .gradle files to Kotlin DSL

1. Replace single quotes with double quotes

In your build.gradle file, the first thing to do is to replace single quotes (' ') with double quotes (" ") as Kotlin uses double quotes for strings but doesn't recognize characters delimited by single quotes. For example, the Material Design Components dependency above will be:

implementation "com.google.android.material:material:material_version"

2. Use function calls

Kotlin recognizes a function call if it ends in parentheses, in the case of a function with parameters, we have to delimit the parameters with parentheses. The code snippet above then becomes:

implementation("com.google.android.material:material:material_version")

3. Use assignment operator (=)

Assigning values in Groovy doesn't require the equal sign =, but Kotlin requires it. Also, it is difficult to differentiate between a function call and a variable assignment like in the case below:

defaultConfig {
        applicationId "com.example.sampleapp"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
    }

In cases like this, we can use the function call for all cases and see if an error is triggered, we can then make changes using the equal sign for lines that trigger an error.

An example of using the equal sign is:

defaultConfig {
        minSdkVersion = 21
    }

4. Changing the filename

Now that we have started using Kotlin DSL, the .gradle file name should be changed from .gradle to .gradle.kts. For example, build.gradle becomes build.gradle.kts.

5. Using Plugins

Plugins are usually situated at the top of the file and they should be written this way:

plugins {
    id("com.google.gms.google-services")
}

or

apply(plugin = "com.google.gms.google-services")

However, using the plugins {} is recommended.

6. Using URLs

While using URLs, you should specify URLs this way:

maven("https://jitpack.io")

With this, you are getting started with Kotlin DSL on a strong foot. The future of Kotlin is bright and using Kotlin DSL in our projects is a good way to go.

This is a basic setup that helps you with migrating small or medium-sized projects from Groovy DSL to Kotlin DSL, if you want to dive deep into Kotlin DSL and improve on it, check out the additional resources below.

Additional resources

image.png

Thank you for reading, if you found it useful, do not hesitate to like and share as others may find it useful as well. If you have questions or feedback for me, kindly drop them in the comments or send me a direct message on Twitter or LinkedIn, I'll be happy to connect with you.