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
https://medium.com/media/96ca14688c6ebb508d27c60d7c2211b4/href
You should ensure that the versions used for com.android.tools.build:gradle 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).
https://medium.com/media/15b390227cf6c356d7986d21b4b104e1/href
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.
https://medium.com/media/8cb1c4f7c663da91837c932d5080a7ff/href
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!
https://medium.com/media/d199a3a76856389128c3b16c1ea457e6/hrefhttps://medium.com/media/b2a92abe9a4bc77069cec45e92d37a72/href
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.
https://medium.com/media/138eceb26c5c19c2e79a2da1e778a20b/href
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.
{your_project}/buildSrc/src/main/kotlin/common.gradle.kts
https://medium.com/media/22c734d091bf1ef32557b5c30df04bc9/href
Now we can utilise this to make module build scripts pithy by including common as a plugin!
https://medium.com/media/0071d30d46131f23961ce755fdb66679/href
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.
https://medium.com/media/2e4d9c296dd0a695da0a3c7a21354b15/hrefhttps://medium.com/media/f13cbe8ceeac3b56d8fb17772370041f/href
Fin
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 🙂
MichaelM97/Gradle-Kotlin-DSL-Android-Example
Useful resources
- “Migrating build logic from Groovy to Kotlin” – https://docs.gradle.org/current/userguide/migrating_from_groovy_to_kotlin_dsl.html
- “Use buildSrc to abstract imperative logic” – https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources
- “Writing Build Scripts” – https://docs.gradle.org/current/userguide/writing_build_scripts.html
- “Developing Custom Gradle Plugins” – https://docs.gradle.org/current/userguide/custom_plugins.html
- “Kotlin Meets Gradle” – https://blog.gradle.org/kotlin-meets-gradle
- “Gradle Kotlin DSL Samples” – https://github.com/gradle/kotlin-dsl-samples
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: kt.academy.