Gradle Kotlin DSL: To make your Android Project better

Rizkyalfikri
5 min readDec 8, 2021

All Android developers must be familiar with Gradle, it’s build tool that common used for application development, especially Android. You can look the detail in here. Gradle scripts is commonly writed with Groovy programming language and that’s the problem for Android developers. Because not all Android developers familiar with Groovy, so it’s hard to understand deeply what we write when modified Gradle.

But no worry. In 2017 Kotlin becomes main language for android and then in 2019 there is Kotlin DSL where we can write Gradle script with Kotlin. Of course, it would be more fun to write scripts in a programming language that we like. Besides being easier, we can also better understand what we write. Especially with some of the “magic” offered by Android Studio such as auto complete, error detection, and others.

What is Kotlin DSL ?

First, let’s look at what is meant by Kotlin DSL. Kotlin is a programming language rich in interesting features that can make code more concise and expressive. One of them is support for writing a domain-specific language or DSL. According to Wikipedia, DSL is a computer language that is specific to a particular application domain. It differs from a general-purpose language that can be applied in all application domains.

Why Kotlin DSL ?

  • Compile-Time errors checking

Compile-Time errors is when Android Studio checking directly to our code if there any error when we typing codes. In Groovy if there is a typo, error message will not show until we sync again the gradle. But it is different when we use Kotlin DSL, an error message will be displayed every time we mistype the code without syncing gradle.

Compile-Time Errors Groovy Version
Compile-Time Errors Kotlin DSL Version

For example, both above pictures have same error, there is typo at code compileSdk. At Groovy (first picture) we need to sync gradle to know if there is an error code in gradle. At Kotlin DSL (second picture), we see red color at error code which means there is an error in our code without need to sync gradle.

  • Auto Completion and content assists

Auto Completion is feature in programming languages that help developer when developer want accessing property or method in object then will displaying mini window that contain contents of the object. Since Kotlin have this feature, it will help us when we want to modify gradle file.

Auto Completion and Content Assists Kotlin DSL
  • Friendly code navigation

Code Navigation is feature that will help us to trace our code when we use Control + Right Click (Window) or Command + Right Click (Mac) to the use of method or property then Android Studio will immediately show us the method or property it self. Groovy doesn’t have this feature in Gradle.

  • Quick documentation (Kotlin function based)

Because we use Kotlin, of cource we will use Kotlin methods that has familiar with us to modify gradle and each method in Kotlin have good documentation which we can see on the web or directly at Android Studion.

Quick documentation Kotlin DSL
  • Consistency of using the same language across the application

Base my opinion this is the most importance thing why we should use Kotlin DSL instead Groovy is consistency of using the same language. For example we use Kotlin for Android project and also we use it to Gradle too also if make Kotlin Multiplatform, we will use Kotlin to create bussiness logic. That means we have use one language across the application and it will make easy to us to trace bug. We don’t need to jump over different language in one project.

  • Enhanced IDE editing experience

It’s obvious, because Kotlin is made by JetBrains and we are using Android Studio which is made by JetBrains too. This will make the coding experience much better.

Migrating Groovy to Kotlin

In this article I will use existing project but if you have your own project to migrate it’s fine because I just migrate simple project. Here my existing project, you can use it. Now we will focus on the 3 (three) files contained in the project, namely settings.gradle, build.gradle (project), and build.gradle (app). The third file is Gradle scripts which are written in Groovy by default. Now it’s time for us to migrate from Groovy to Kotlin DSL on those files.

First, rename settings.gradle file to settings.gradle.kts and you will error code inside it. Why? because we already change Gradle file to Kotlin script and of course we need to change the code inside it from Groovy to Kotlin.

As we can see above, almost the whole code is similar. But there is different on include syntax. On Groovy the code include ':app' isn’t informative. We don’t know this code is method or variable. But on Kotlin code we know this is code is a method.

Next change file build.gradle (project) to build.gradle.kts (project)

Here we see the major difference between Groovy and Kotlin. The classpath is a method with string argument and also it’s clear enough what task method actually do.

Last let’s change file build.gradle(app) to build.gradle.kts(app) , Let’s fix them one by one by converting the code to Kotlin DSL. Start from the following top 2 (two) rows:

Proceed to all the code that is in the android block. Change it to like this:

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

Then in the dependencies block, we need to adjust the code to be like this:

So, so far, we have successfully migrated all Gradle scripts written with Groovy to Kotlin DSL. For more details, you can see the full code in here.

That’s it for today. Please be generous enough to comment for feedback or suggestions or any queries. Thanks for reading.

--

--