Diving into Kotlin DSL Part-2: Kotlin enters the Gradle realm

Malik Motani
Mindful Engineering
4 min readJan 4, 2020
Unsplash : Artem Verbo

If you are one of them who doesn’t like how you write stuff in build.gradle, stay tuned you have come to the right place.

Groovy is widely used in Android app development for the usual building process and its customization. Some of the tasks are performed by Android Studio configuration and we can also write our own gradle tasks.

I belong to the community who doesn’t have much knowledge about groovy and feels yaaaak when anything requires to code in groovy.

Giphy: Angry Hate GIF

Here Kotlin DSL will become a lifesaver. Most interestingly it will allow us to write code in Kotlin, which will perform all the gradle tasks. Additionally, all the third-party dependencies (Libraries we use most often, you know what I mean :D) can be managed with Kotlin itself.

Giphy : Lifesaver GIF

You might be wondering what is Kotlin DSL? I will recommend you to read the first blog from this series to get a basic idea of Kotlin DSL

Now if you have done that already we can dive deep into the world of Kotlin DSL.

There are few steps by which you can set up DSL with your Android Studio:

Step 1: Create “buildSrc” directory

Android Studio automatically creates app level and module level build.gradle files to handle project level and module level dependencies.

Now, we are going to manage every dependency in a way, so that it will be accessible from a single place. Don’t worry if you have modularization in your project, even multiple modules can be configured with this mechanism.

Sometimes you might have faced two different versions of the same library that have been used in multiple modules and that creates inconstancy problems. By this method, we can have the same version of the library available to all the modules.

First, create buildSrc directory in the root directory of your project. This will provide a gradle feature that enables us to define tasks and access tools that can be used for the build scripts. By adding kotlin-dsl plugin in the build config, we can use Kotlin within this directory.

For more information check the Gradle documentation for buildSrc here

Now let’s create the following files inside buildSrc directory in the package structure as shown in the image below:

  1. build.gradle.kts
  2. Dependencies.kt (Inside src/main/kotlin package)

Step 2: Apply “kotlin-dsl” plugin in build.gradle.kts

Here I have supplied buildSrc by which we can get this plugin from the repository.

using .kts extension will let Gradle know that this file is a Kotlin script and not a Groovy one.

Step 3: Add values to Dependencies.kt

Above is Kotlin’s file which has singleton classes to define dependencies used throughout the project.

In this way, we can define all the dependencies easily and in a more readable manner at a single place.

Step 4: Convert build.gradle files and update with values from Dependencies.kt

Update the project level build.gradle file with classpath inside the dependencies block with the values from “Dependencies.kt” and convert the Gradle clean task to Kotlin syntax. Now the updated file will look like below:

Similarly, update app level build.gradle values from “Dependencies.kt”

Now, its time for magic!

Giphy: The Magicians Magic GIF By SYFY

You will start getting suggestions while writing dependencies.

That’s it! Now your configuration is ready to use. I’ll keep exploring this series by the time feel free to share your view on Kotlin DSL in the comments below.

--

--

Malik Motani
Mindful Engineering

An Android expert and Flutter enthusiast, Senior Experience Engineer at Publicis Sapient.