Kotlin DSL Is Now the Default for New Gradle Builds

Read this post in other languages:

We always strive to help developers write better-structured and more maintainable builds. Given this aim, applying Kotlin to writing Gradle build scripts and plugins seems only natural. Kotlin provides many powerful features that make it an excellent choice for creating a DSL and significantly improve the developer experience.

We have done a lot of work since Kotlin was brought to Gradle for the first time, aiming to make the experience of Gradle users smooth and enjoyable. Today, we are thrilled to share some important news with you: Gradle announced that they have chosen the Kotlin DSL as the default for new Gradle builds! From now on it will be the default option in the plugins and documentation. Thanks to its rich features, like first-class functions and extension methods, Kotlin can maintain and even improve the best elements of creating Gradle scripts.

JetBrains, Google, and Gradle announced that the Kotlin DSL is now the default for new Gradle builds

Thanks to the collaborative effort between JetBrains, Google and Gradle, the Kotlin DSL reached the next level of maturity and is now an even more efficient and user-friendly tool for developers to use in their projects.” ~ The Gradle team

In addition, Google made a statement about the Kotlin DSL as the default script for Gradle build files for Android apps.

Google, Gradle, and JetBrains will continue to collaborate closely together to have a unified code style and defined idioms, which in the end will help us improve code quality.

In this post, we want to share some key advantages of the Kotlin DSL for writing Gradle scripts, along with the existing capabilities of IDE support of build.gradle.kts files.

Benefits of choosing the Kotlin DSL over Groovy

Different developers may have different reasons for choosing one language over the other. In the sections below, we highlight the key strengths of Kotlin that make the Kotlin DSL a better choice for writing Gradle build scripts and plugins.

So, if you are still on the fence about converting your Android’s Gradle build files from Groovy to Kotlin, here are the 4 main advantages of using Kotlin.

  1. Compile-time checking

A crucial difference between the two languages is that Groovy is a dynamic language, while Kotlin is statically typed. This means that, when the Kotlin DSL is used, build script errors might occur at compile time rather than at runtime.

With dynamic type checking, errors are not detected until the build script is executed. With compile-time errors, though, you have a chance to fix the problems earlier. What’s more, if you are working in an IDE, an editor can highlight the errors. For example, this is what you will see when trying to reference the configuration name `applications` instead of `application`.

When the Kotlin DSL is used, build script errors might occur at compile time

2. Better IDE experience

With more semantic information about code, you can use IDE features to their full extent, including:

  • Auto-completion and content assistance
  • Source code navigation
  • Refactorings

So far, IntelliJ IDEA and Android Studio provide the most integrated experience for Kotlin. However, you can import and work with Kotlin-DSL-based builds in other IDEs as well. In the Gradle documentation, you can check the IDE support matrix for the Kotlin DSL.

3. Simplified, declarative plugin syntax

The Kotlin DSL provides an alternative syntax to the traditional Groovy DSL and offers the same benefits as the core Kotlin language. Let’s look at the two languages side by side.

When you apply Gradle plugins to a project, you need to specify only the plugin ID. For example, when you specify the `application` plugin with the Groovy DSL, it will look as follows:

Specifying the plugin ID with the Groovy DSL

Thanks to the simplified syntax of the Kotlin DSL, you only need to type “application” in Kotlin:

Specifying the plugin ID with the Kotlin DSL

As mentioned above, if you try to apply a plugin that doesn’t exist, the IDE will show you a syntax error.

4. A growing community of Kotlin developers

Kotlin has a large and thriving community of developers who contribute to the language and libraries and provide Gradle plugins.

A growing community results in a wealth of information for both experienced developers and those who are new to Kotlin. These include Kotlin’s official documentation, forums, blogs, threads on social media, and online educational courses.

With many helpful resources available, you can easily find solutions to problems and take your skills to the next level.

Migrate from Groovy to the Kotlin DSL

As you can see, the listed advantages and robust features of the Kotlin DSL make it the perfect choice for boosting productivity and building high-performance software. If you want to migrate your existing Gradle build from Groovy to the Kotlin DSL, you can check out Gradle’s migration tutorial. And for Android developers, there is a thorough guide on how to convert your Android app’s Gradle build files from Groovy to KTS.

Gradle will still retain support for the Groovy DSL. When it comes to large projects with complex build logic that are using the Groovy DSL, Gradle recommends not to migrate them yet. However, now that Kotlin is Gradle’s default choice of DSL, we believe the Kotlin DSL will significantly benefit Gradle users who are looking to improve their experience when writing and editing build scripts.

As always, the JetBrains team is committed to enhancing our technology further to provide our users with feature-rich tools for developing efficient applications.