Share your Gradle configuration with the Gradle Kotlin DSL — A guide for Android projects

Share your Gradle configuration with the Gradle Kotlin DSL — A guide for Android projects

This guide will help you to convert your Gradle build scripts from Groovy to Kotlin, allowing you to take advantage of some great benefits! These include:

  • Shareable Gradle configuration, minimizing the amount of duplicate code across your module build scripts
  • Shared dependency notation and versioning, making tasks like bumping dependency versions a breeze
  • Improved IDE support, enhancing features such as autocompletion, navigation to source, quick documentation, etc.

Step 1 – The buildSrc module

First, we will create a new directory at the root of our project named buildSrc. This is where all of our shared Gradle configuration, dependencies, versioning, etc. will live.

To make this directory a module we create a Kotlin build script at the root, so that the file path is {your_project}/buildSrc/build.gradle.kts

You should ensure that the versions used for and org.jetbrains.kotlin:kotlin-gradle-plugin match what you already have specified in your project build script ({your_project}/build.gradle) rather than using the versions in this example.

It’s also important to note the comments added to this file, it isn’t possible to utilise the shared versioning here so these dependencies will need to be updated manually.

Step 2 – Shared dependencies & versioning

It’s time to create the objects that will store our dependency notation and versioning, the process of moving all of your dependencies to these shared objects can be a bit painful depending on the number of dependencies your app uses.

Let’s start by creating the object that will hold our dependency notation. This should be created at {your_project}/buildSrc/src/main/kotlin/Dependencies.kt. I recommend going through your :app module’s build.gradle and copying any dependencies from there as a starting point.

I find it useful to group dependencies in nested objects as it increases readability, though you may structure this however you like. We can also group dependencies that are commonly included together using DependencyHandlerScope (you’ll see this in use later).

Now, let’s create the object that will house all of our versioning, create it alongside Dependencies at {your_project}/buildSrc/src/main/kotlin/Versions.kt.

As you can see, we’ve added all versions required by our dependencies, along with some versioning specific to our app in the nested App object.

I find it useful to also create a Modules object for storing module paths and a Plugins object for storing plugin identifiers to share even more values across build scripts!

Step 3 – Converting app module build script to Kotlin

The steepest learning curve with the Gradle Kotlin DSL is the syntax. Below is an example Kotlin build script for the :app module which covers some common configuration, along with using our previously created Dependencies, Versions, Modules, and Plugins objects. If you have some complicated configuration that you’re struggling to convert, there are some links at the bottom of this guide to documentation that may help.

Step 4 – Shared module configuration

In my opinion, this is the greatest benefit of using the Gradle Kotlin DSL, the ability to easily share build script configuration across modules. First, let’s create a script that contains our shared configuration.


Now we can utilise this to make module build scripts pithy by including common as a plugin!

Now you can go through each of your module build scripts and convert them to Kotlin, all whilst moving any dependencies, module paths, plugin identifiers, and versioning to the objects we created in step 2 so that they can be shared; allowing for easy alterations/updates in the future.

Step 5 – Converting project build script and settings.gradle to Kotlin

The final step is to convert your top-level (project) build.gradle and settings.gradle to Kotlin. This should be fairly straightforward as they won’t contain many configurations.


That’s it! 🎉 I hope this guide was useful and that you enjoy your new Gradle setup. All example code shown can be viewed in the example project linked below 🙂


Useful resources

Click 👏 to say “thanks!” and help others find this article.

To be up-to-date with great news on Kt. Academy, subscribe to the newsletter, observe Twitter and follow us on Medium.

If you need a Kotlin workshop, check how we can help you:

Share your Gradle configuration with the Gradle Kotlin DSL — A guide for Android projects was originally published in Kt. Academy on Medium, where people are continuing the conversation by highlighting and responding to this story.