avatarGabriel Shanahan

Summary

The article advocates for using Gradle with the Kotlin DSL for building Kotlin projects due to its partnership with Kotlin, despite the challenges of transitioning from Groovy and current performance issues.

Abstract

The author presents a compelling case for why Gradle with the Kotlin DSL is the preferred choice for building Kotlin projects. Despite the historical use of XML in Maven and the Groovy-based DSL in Gradle, the official partnership between Gradle and Kotlin, along with the Kotlin DSL's development trajectory, suggests a promising future. The article acknowledges the difficulties beginners may face due to the abundance of Groovy-targeted resources and the relative newness of the Kotlin DSL, which has led to some performance concerns. However, the author points out that Gradle is actively working to improve the Kotlin DSL's performance and usability, including faster imports and reduced memory usage, and is providing resources to ease the transition from Groovy. The commitment from Gradle to make Kotlin a first-class citizen in its ecosystem implies that the Kotlin DSL will continue to evolve and potentially become the standard, similar to its adoption in Android development.

Opinions

  • The author expresses a clear preference for Gradle over Maven and Ant, primarily due to the verbosity and visual frustration associated with XML configuration in Maven.
  • Gradle's partnership with Kotlin and its use in building Kotlin itself are seen as strong endorsements for using Gradle with the Kotlin DSL.
  • The Kotlin DSL, while promising, is noted to be in its early stages of development compared to the established Groovy-based DSL, leading to a lack of resources and some performance issues.
  • Despite current limitations, the author is optimistic about the future of the Kotlin DSL, expecting continued improvements and potential dominance in the Kotlin build tool landscape.
  • The author suggests that the investment in learning and using the Kotlin DSL will pay off as Gradle continues to align itself closely with Kotlin, enhancing the tooling and resources available.

Why you should use Gradle w/ the Kotlin DSL to build Kotlin projects

A brief overview of Gradle, Maven & Ant and the pros and cons of using the Kotlin DSL

Have you heard about the Kotlin Primer? It’s an extensive, hands-on and detailed guide to the Kotlin language full of unique and original explanations, interactive exercises, and concrete recommendations based on experience. It will transform anyone who knows Java into a Kotlin expert within a matter of days. Check it out!

This article is the first in a series about creating a Kotlin project with Gradle configured using the Kotlin DSL. The series is written with complete beginners in mind and assumes no previous experience with Gradle, but also strives to provide references to all presented material and to leave no questions unanswered. At times, getting satisfactory answers required digging into the actual source code and, to my knowledge, this information is not available elsewhere at the time of writing. This makes it interesting to more advanced users as well.

In this article, I want to talk about why I chose to build my Kotlin projects using Gradle w/ the Kotlin DSL in the first place.

When setting up a Kotlin project, the first thing you need to look at are build/project management tools. There are three major players that are worth mentioning: Gradle, Maven and Ant. All three support building Kotlin projects, but only Gradle and Maven can also take care of dependencies, which I consider a must. I chose Gradle for a couple of reasons.

Right off the bat, I was discouraged from using Maven because it’s configured using XML, which I dislike for its verbosity and generally find it visually frustrating. But more importantly, Gradle and Kotlin are officially partnered up, which is reflected (among other things) in the Kotlin DSL that Gradle offers to configure the builds. Kotlin itself is built with Gradle, and Gradle is the official build tool for Android, which in turn is one of the main reasons people learn Kotlin in the first place. Therefore, it seems like a sensible choice.

Courtesty of gradle.org

Paradoxically, the Kotlin DSL can actually be the trickiest part of using Gradle. Since Gradle was created in 2007, the language it used for declaring project configurations was a Groovy-based DSL, while the first alpha release of the Kotlin DSL happened mid 2016 and the first major version wasn’t released until mid 2018. This has a couple of consequences, the most painful one being that a large portion of Gradle resources and tutorials you’ll find target Groovy, and translating between the two isn’t always straightforward. It also means that the Kotlin DSL has only been in development for 2–3 years, so there are still some things lacking — for example, there are complaints about a big speed gap between the two DSLs, among other things.

However, it’s not like there aren’t any options. There are steps you can take to make your Gradle builds faster (some of which we’ll discuss in future articles). Gradle imports are already 2.5x faster and use 75% less memory in Kotlin 1.3.60 than they did in Kotlin 1.3.10 — this improvement corresponds to a year of development. Gradle is also investing in creating resources to simplify migration from Groovy to Kotlin. It is reasonable to expect that these trends will continue.

In total, considering that Gradle has clearly committed to turning Kotlin into a first class citizen, I think using the Kotlin DSL is the smart thing to do. They aren’t going to invest this much effort in an initiative only to abandon it in the future, which means they’re going to keep working at it until it’s on par with what one can do with Groovy. And by that time, if for no other reason than marketing and hype, they might very well do the same thing Android did and turn Gradle into a Kotlin-first tool.

In the next article, we’ll take a look at setting up Gradle and creating our first Kotlin project.

Gradle
Kotlin
Kotlin Dsl
Setup
Begginer
Recommended from ReadMedium