The very first version of Gradle had build scripts written in the dynamic Groovy language.

Why then, years later, did team Gradle add Kotlin as a 2nd option? Was Groovy somehow not good enough?

Now when you start a new Gradle project you can choose either a Groovy or Kotlin build script. It can be confusing for developers like you and I.

The question for developers starting a new Gradle project

The question for developers starting a new Gradle project

So let’s explore why Gradle added Kotlin support in the first place, and what are the pros and cons compared to Groovy.

Plus, I’ll reveal a recent announcement from Gradle that might influence your language choice on new projects.

A build tool powered by Groovy

One big advantage of Gradle build scripts is they’re written in code.

Compared to Maven’s XML format, you can more accurately describe your build process in fewer lines.

But did you ever look at a Gradle build script and think it looks more like markup than code?

plugins {
    id 'java'
}

group = 'org.example'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
    useJUnitPlatform()
}

Groovy was originally chosen for Gradle, because it:

  • runs as a script
  • supports closures
  • doesn’t need parentheses

All that makes describing your build very concise, in what’s called the Gradle Groovy DSL.

But Groovy is dynamically typed. The same variable can be set to a String, Integer, or any other type.

Whether you like these kinds of languages is personal preference.

But in the case of Gradle, it has some problems. Editing Groovy build scripts in the IDE isn’t that developer-friendly, although it has improved over time.

For example, auto-complete and error highlighting don’t work as well as statically typed languages like Java.

Error highlighting missing in IntelliJ IDEA due to the dynamic nature of Groovy

Error highlighting missing in IntelliJ IDEA due to the dynamic nature of Groovy

JVM developers want their tools, and they want them now.

Those words rang in the ears of Gradle founder Hans Dockter for almost 10 years.

Something had to change. Thankfully, Hans was about to pull a very large rabbit out of his hat.

Gradle and JetBrains join forces

In 2015, Hans sat down with JetBrains, the creators of Kotlin. Yes, they also make IntelliJ IDEA!

They all realised that writing Gradle build scripts in Kotlin could fix many of the shortcomings of Groovy.

The following year, Gradle 3.0 was released. Now by simply renaming your build.gradle build script to build.gradle.kts, you could cross into the world of Kotlin.

Was that a good thing?

Initially, there were some sticking points, like most of the the documentation being for Groovy. But over the years, the kinks were ironed out, and using the Kotlin DSL became even Groovier than Groovy.

For example:

  • you get more relevant autocomplete suggestions
  • you can see helpful documentation without browsing the Gradle website
  • you can create Strings without wondering whether to use single or double quotes

And here’s the cherry on the cake. Notice how similar the build scripts are?

plugins {
    id("java")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}
plugins {
    id 'java'
}

group = 'org.example'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
    useJUnitPlatform()
}

Kotlin took inspiration from Groovy, so it’s not too hard to migrate between the two.

Should you use the Groovy or Kotlin DSL?

It’s definitely worth considering both and choosing what suits your project, but there’s one more fact to reveal.

Gradle just announced that Kotlin is now the default language for new Gradle builds!

What does that mean?

  • when you create new projects in IntelliJ IDEA or Android Studio, Kotlin is first
  • from Gradle 8.2, Kotlin is default for gradle init
  • Gradle documentation now shows Kotlin first

Practically, that doesn’t make much difference. But it does show which language Gradle is guiding you towards.

They’ve listed some edge cases where build performance with Kotlin still doesn’t match Groovy. Like very large or complex projects.

Recommendation from Kotlin DSL is Now the Default for New Gradle Builds

Recommendation from Kotlin DSL is Now the Default for New Gradle Builds

But personally, I’m going all in on Kotlin for any new project.

Watch this video demonstrating the ideas from this article.

Stop reading Gradle articles like these

This article helps you fix a specific problem, but it doesn't teach you the Gradle fundamentals you need to actually help your team succeed.

Instead, follow a step-by-step process that makes getting started with Gradle easy.

Download this Free Quick-Start Guide to building simple Java projects with Gradle.

  • Learn to create and build Java projects in Gradle.
  • Understand the Gradle fundamentals.