People who know me may be surprised that I’m about to write about and praise Gradle. I have often expressed the love-hate relationship I had with Gradle.
But I want to focus here on the second half of the problem.
Maintenance of those build.gradle files can become quickly super tedious…
The bright side of Gradle is that it brings a lot of power and flexibility to the table, with build files that are very declarative and easy to read.
But with great power comes the need for great tooling… or a very frustrating user experience.
And that was the dark side of Gradle. While the build.gradle files are easy to read, they are quite hard to write. When you need to customize them, you are mostly on your own. Usually, it doesn’t work for a while, and you don’t know why. Then at some point, you copy-paste the right solution, and it works, but you don’t really know why either.
What happened is that the dynamic nature of Groovy combined badly with all the meta-programming done in Gradle to give us poor tooling support.
Ok, so that’s the background of Gradle's kotlin-dsl project on which
Gradle and JetBrains have been quietly working on. It’s about to reach maturity, so now is a good time to pay attention to the topic.
Migrating the existing syntax of your build to Kotlin, bit by bit, while retaining the structure — what we call a mechanical migration
Restructuring your build logic towards Gradle best practices and switching to Kotlin DSL as part of that effort
For our existing projects, the latter will usually make much more sense.
There is a story that a lot of C programmers got started doing C++ by taking this baby step.
// before.cintmain(){/* I was doing C programming */printf("Hello, World!");return0;}// after.cppintmain(){// Now I'm now doing c++printf("Hello, World!");return0;}
In this spirit, I will focus on a practical first step you can take now with Gradle & Kotlin.
Dependencies management
Gradle allows us to declare dependencies in a very terse way
The code from the buildSrc folder is automatically available to all of your build.gradle or build.gradle.kts files.
Laziness as a great virtue of the programmer
I had done that for a few projects, and I was happy at first. On my third project or so, it became obvious that extracting all those strings is tedious.
Also, we didn’t solve yet the problems 3) What is Krangl? and 4) What new versions are available?
There is a very nice plugin from Ben Manes, gradle-versions-plugin, that once applied to your Gradle project answers this. So as a lazy programmer, I wondered: could I extend it to generates exactly the Kotlin code I need?
As it turns out it is possible, so let me present my new Gradle plugin!
This new declarative plugins block is the preferred way to declare a plugin instead of the buildscript {...} syntax which provides a worse Kotlin DSL user experience.
$ ./gradlew buildSrcVersions
This is the command you run the first time, and every time you added a new dependency or just want to check what versions are available.
It first executes the task :dependencyUpdates that connects to the internet and may take a while. This task produces a JSON report with meta-data about all your dependencies, which Kotlinpoet helps to convert to the following code.
This generated code is all we need to experience the much better IDE integration provided by Gradle’s kotlin-dsl in IntelliJ IDEA or Android Studio ≥ 3.2.
We have now solved the problems we intended to solve
No more magic strings, we type Libs.<TAB> and auto-completion does the rest.
We can use those it in all the build.gradle and build.gradle.kts files.
To discover what Krangl is, we press Quick documentation and click on the link to the website.
To know which newer versions are available, we just jump from the IDE to Libs.okio and from there to Versions.okio. 2.0.0 is currently used, and a useful comment tells us that 2.1.0 is available. (The plugin never auto-apply a dependency update, that should always be the developer’s job!!).
Give it a try
Groovy build.gradle and Kotlin build.gradle.kts files look almost similar once written. What is completely different is the writing experience of it, so I will encourage you to fire up IntelliJ IDEA or Android Studio.
I began by confessing that I hated the black magic aspect of Gradle. Opening the black box with Kotlin has been super useful. I realized that Gradle is simply a good, useful, well-documented API. An API that you can work with using your developer tools and skills like with any other Java API. An API whose goodness had been hidden from us. I plan to go deeper in following articles if there is enough interest.
Welcome! The Gradle Kotlin DSL provides support for writing Gradle build scripts using JetBrains' Kotlin language. It aims to provide Gradle users with a rich, flexible and statically-typed approach to developing build logic in conjunction with the best IDE and tooling experience possible.
Getting Started
The fastest way to get up and running with a Kotlin-based Gradle build is to use gradle init
The Gradle Kotlin DSL is documented in a dedicated chapter in the Gradle user manual.
Moreover, the Gradle user manual and guides contain build script excerpts that demonstrate both the Groovy DSL and the Kotlin DSL. This is the best place where to find how to do this and that with the Gradle Kotlin DSL; and it covers all Gradle features from…