Playing Gradle Script with Kotlin DSL in Android Project

Hi everyone :)

Now, I will share a little experience using Kotlin DSL as a replacement for Groovy in the Android project.

It seems like a long time ago I didn’t write tutorial again on this Medium. The previous tutorial I wrote was Fetch Data From Firebase Cloud Firestore to RecyclerView Using FirestoreRecyclerAdapter, it’s about two years ago. LOL! Even so, I didn’t stop writing. Last year, I released Kotlin Android Developer Expert and recently I also released Memulai Pemrograman Dengan Kotlin (an online course to learn Kotlin programming language from scratch).

Back to the main topic. All of Android developer definitely familiar with Gradle.

What is Gradle?

That is the file with .gradle extension on the Android project. :D
Gradle is a build tool that is commonly used in application development, including Android. You can read on for more details.

A Gradle script is usually written in Groovy programming language. Then, can Gradle script be written with Kotlin? Yes you can! Please read this blog: Kotlin Meets Gradle.

Of course, it would be more fun to write a script with a programming language that we love. Besides being easier, we can also better understand what we write. That’s what I felt when I first tried Kotlin DSL. Especially with some “magic” offered by Android Studio such as auto complete, error detection, and others.

How About Kotlin DSL?

Kotlin is a programming language that is rich with interesting features that can make the code more concise and expressive. One of them is support for writing domain-specific language or DSL. DSL is a computer language devoted to a particular application domain. It is different from general-purpose language that can be applied in all application domains.

The query command in SQL is one example of DSL implementations. If we pay attention to statements in SQL, we can conclude that they are almost like sentences in English: expressive and easy to read.

Expressive man

See this code:

The most noticeable difference from the above code is grammar. The code on DSL is easier to read and understand, even for people who don’t understand programming. In Kotlin, we can find examples of DSL implementations in several libraries, for example Anko. Anko is a library on Kotlin that makes Android application development faster and easier.

Let’s Migrate from Groovy to Kotlin DSL!

To start it, please create a new project with Android Studio. Don’t forget to choose Kotlin as the language to be used. Here I use Android Studio version 3.4 with Kotlin version 1.3.31. After that, check the Gradle version in the file:

Here we will focus on 3 files contained in the project, settings.gradle, build.gradle (project), and build.gradle (app). The three files are Gradle scripts which are written by default with Groovy. Now it’s time for us to migrate from Groovy to Kotlin DSL in these files.

First, change settings.gradle to settings.gradle.kts and make sure the code inside will be error. Why? Because you have changed the Gradle file to Kotlin scripts, of course you have to change the code inside to Kotlin DSL.

At this point we have seen a simple difference from Groovy and Kotlin DSL, the String on Groovy can be quoted with single quotes ‘string’ while Kotlin requires double quotes “string” and parentheses () to carry out their functions.

Next, do the same in the build.gradle (project) file. Change to build.gradle.kts and update the code inside using Kotlin DSL. Consider the following commit history for more details:

On this change, you can also see how a clean task is declared on Kotlin DSL.

Last but not least, we need to modify the build.gradle (app) file. After changing it to build.gradle.kts, you will facing errors like the following picture:

build.gradle.kts (app)

Let’s fix one by one by changing the code to Kotlin DSL. Start with the top 3 lines:

Usually we use apply plugins to declare a plugin. With Kotlin DSL, we only need to use block plugins.

Continue to all codes that are on the Android block. Change it to something like this:

Here you need to pay attention to how a plugin is applied. In Groovy, you can use an equal sign (=) to set a value, with Kotlin DSL you need to know in advance whether the configuration is a function or property.

Inside the buildTypes block we use getByName() because actually the release is a string that is passed to a function called by Groovy and Kotlin will not be able to access it. MinifyEnabled is also not an actual property name, so we need to use the original name, which is isMinifyEnabled.

Then in the block dependencies, we need to adjust the code to be as follows:

Well, until here we have successfully migrated from all Gradle scripts written with Groovy to Kotlin DSL. For more details, you can see the following commit history:

You can also learn more complete migration steps in this documentation.

Hopefully this tutorial is useful. Have any question? Feel free to drop your question or comment below.