Writing your Gradle scripts in Kotlin with Kotlin DSL

If you are writing your source code in Kotlin then it is good to have your build scripts in Kotlin as well. It has clear advantages over Groovy like having auto suggest support and writing custom Gradle tasks becomes easier as it is in the same language as your source code: Kotlin

Let’s go step by step on how we can achieve this.

Step 1: Create buildSrc directory

When creating a new project, Android Studio automatically generates two build.gradle files. One for the project configuration and other for the module configuration (app module)

We should organize dependencies in such a way that they can be managed throughout the project from one common place. This will also help to manage dependencies in modular projects as they have many modules and different modules can have common dependencies.


If two modules have the same dependency and both have different version then it can introduce subtle behavior differences into the app. It is good to avoid these kinds of problems, and the best way to do that is to ensure that we consistently use the same version of any given third-party dependency throughout the app.

Creating buildSrc directory in the root directory of the project will help us solve our problem

This directory is a Gradle feature that enables to define tasks and tools which can be available throughout the build scripts, and we can use this to make our version information available throughout our build scripts. We can also use Kotlin within this directory by specifying the kotlin-dsl plugin in the build config for the buildSrc directory as explained in the next step.

Check the Gradle documentation for buildSrc here

CreatebuildSrcdirectory, then create two 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

https://gist.github.com/passiondroid/e9079ff1864f0ad8058f28223db09301#file-build-gradle-kts

It applies the kotlin-dsl, declares the repository from which this plugin can be obtained, and disables a warning that it is experimental.

This file has a .kts suffix, which indicates to Gradle that this file is a Kotlin script and not a Groovy one.

Step 3: Add values to Dependencies.kt

This is just a Kotlin file that contains singleton classes which includes version numbers and the dependencies used throughout the project. Dependencies organized in a proper way makes it very easy to manage them from one common place.

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

Rename project level “build.gradle” file with “build.gradle.kts”. Update the 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:

https://gist.github.com/passiondroid/3ca281a8ea699816c707558bfaad2128#file-project-build-gradle-kts

Similarly, rename app-level “build.gradle file with “build.gradle.kts” and update the values from dependencies.kt as shown below

https://gist.github.com/passiondroid/0f31e5ccd2ce0d6e8fc4be906b158b27#file-app-build-gradle-kts

Here we have a single plugins block and id is actually a function call, so the arguments are in parentheses. This is a common thing that is required throughout the conversion process.

Finally, we can get auto suggestions now in our Gradle file.

You can find the complete project here:

https://github.com/passiondroid/gradlekotlindsl

 

Conclusion

This article outlines how you can refactor all your gradle files written in Groovy and use your very own Kotlin.

 

*Important*: I’ve created a SLACK  workspace for mobile developers where we can share our learnings about everything latest in Tech, especially in Android Development, RxJava, Kotlin, Flutter, and overall mobile development in general.

Click on this link to join the slack workspace. It’s absolutely free!

 

Like what you read? Don’t forget to share this post on FacebookWhatsapp, and LinkedIn.

You can follow me on LinkedInQuoraTwitter, and Instagram where I answer questions related to Mobile Development, especially Android and Flutter.

If you want to stay updated with all the latest articles, subscribe to the weekly newsletter by entering your email address in the form on the top right section of this page.

 

Author

This article is written by Arif Khan. You can find him on LinkedIn at https://www.linkedin.com/in/afk789/ 🙂