In April 2023, at the Kotlin Conf, it was announced that “Kotlin DSL is now the default for new Gradle builds.” Kotlin was integrated into Gradle’s build tools as early as 2016 and has become the default choice for developers in 2023.
This section explores the origins and development of Kotlin Script and DSL, providing insights into *.gradle.kts
files.
KTS, or Kotlin Script, is often mistakenly thought to be limited to Gradle scripts. However, *.kts
files are versatile Kotlin scripts used in various domains. Similar to Shell or Python scripts, KTS offers uniformity and ease of use, with the added ability to reference external dependencies. The downside is that systems need to install Kotlin dependencies, which are not typically pre-installed.
There are two main KTS standards:
*.kts
extension and includes features like script caching, external dependency references, command line interpreters, and auto-generation of Gradle projects for IDE integration. Besides Kotlin, KScript tool installation is required.*.main.kts
extension. It incorporates many of KScript’s main features and supports dependency referencing directly in IntelliJ IDEA without needing a Gradle project.Examples of KTS in use:
For a detailed comparison and history of these standards, refer to:
Like KTS, Kotlin DSL is not confined to Gradle scripts and can be found in many other areas. To understand its application, it’s important to know the fundamental elements that make up Kotlin DSL. The following articles and documents offer a comprehensive look at Kotlin DSL, from high-order functions to Type-safe builders:
[“Kotlin DSL | Introduction”@GlennSandoval](https://medium.com/kotlin-and-kotlin-for-android/kotlin-dsl-introduction-6123c43ae770) |
Kotlin DSL is also implemented in these well-known libraries:
Gradle Kotlin DSL is built on generic KTS scripts, further defining the script’s Context (like Project
and Settings
objects) and adding DSL features for simplified configurations. The following resources provide extensive information on Gradle Kotlin DSL, migration from Groovy to Kotlin, and developing plugins with Kotlin:
Understanding generic Kotlin Script and DSL can bring significant convenience to our work, allowing one language to meet multiple job requirements. While official support isn’t exclusively for KTS, as mentioned earlier, Gradle Kotlin DSL is the recommended approach by three major companies. For plugin development, Kotlin is widely supported, offering higher readability, better compile-time checks, and improved IDE support compared to Groovy.