Hello World Kotlin application with the Gradle Kotlin DSL

David Herranz Fernández
4 min readMay 23, 2019

--

Gradle Kotlin DSL (GKD)

In this tutorial we will create a Hello World Kotlin application built with the Gradle Kotlin DSL.

The following software will be used in this tutorial:

  • Kotlin 1.3.21
  • SDKMAN
  • JDK 8
  • Gradle 5.4.1
  • Intellij >= 2018.3

What is Gradle?

Throughout the years, Ant and Maven are tools that have helped us manage the build automatization process. As software evolves, so does these kind of tools, so Gradle has risen as an alternative to Ant and Maven. The advantages that Gradle has over the tools mentioned above are the following:

  • Flexibility: Google has used Gradle as the official build automation tool for Android due to being more extensible in contrast to the rigidness of Maven.
  • Performance: Gradle performs tasks in parallel, but the thing that stands out from its competitors is that it only builds the files that are modified because the daemon uses a cache to keep information in memory.
  • User experience: the IDE support for Gradle is improving and now with the Gradle Kotlin DSL, the IDE support for code assist, refactoring is much better.
  • Dependency management: the dependency management is solid in Gradle, with capabilities of caching local dependencies and parallel downloads.

Gradle scripts are constructed with Groovy and a DSL with the goal to be more usable tool than Ant and Maven, which are written en XML, and recently in Kotlin.

Gradle Kotlin DSL.

Gradle first announced Gradle Kotlin DSL (GKD) in May 17 2016. GKD provides an alternative syntax to the traditional Groovy DSL with an enhanced editing experience in supported IDEs, with superior content assist, error highlighting, refactoring and documentation.

Project — Dependencies.

First off we install the necessary dependencies:

  • Install SDKMAN
> curl -s “https://get.sdkman.io" | bash
> source “$HOME/.sdkman/bin/sdkman-init.sh”
> sdk version
SDKMAN 5.7.3+337
  • Install Java
> sdk list java
================================================================================
Available Java Versions
================================================================================
19.0.0-grl 11.0.2-zulufx 1.0.0-rc-16-grl
13.ea.20-open 10.0.2-zulu 1.0.0-rc-15-grl
12.0.1-sapmchn 10.0.2-open
12.0.1-zulu 9.0.7-zulu
12.0.1-open 9.0.4-open
12.0.1.j9-adpt 8.0.212-zulu
12.0.1.hs-adpt 8.0.212-amzn
12.0.1-librca 8.0.212.j9-adpt
11.0.3-sapmchn 8.0.212.hs-adpt
11.0.3-zulu 8.0.212-librca
11.0.3-amzn 8.0.202-zulu
11.0.3.j9-adpt 8.0.202-amzn
11.0.3.hs-adpt 8.0.202-zulufx
11.0.3-librca 7.0.222-zulu
11.0.2-open 7.0.181-zulu
===================================================================
+ - local version
* - installed
> - currently in use
===================================================================
> sdk install java 8.0.202-amzn
> java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
  • Install gradle
> sdk install gradle
> gradle -v
------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------
Build time: 2019-04-26 08:14:42 UTC
Revision: 261d171646b36a6a28d5a19a69676cd098a4c19d
Kotlin: 1.3.21
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_172 (Oracle Corporation 25.172-b11)
OS: Mac OS X 10.14 x86_64

Project — Construction.

For the project construction, we follow these steps:

  1. We create the project directory and situate in it: mkdir … && cd $_
  2. gradle init, then “kotlin application” option and then “kotlin” option
  3. Import the project in Intellij IDEA. Check the “Use auto-import” option and set gradle home to skdman’s gradle home (/Users/{$USER}/.sdkman/candidates/gradle/current).
  4. Click the Finish button.

The project will appear with this structure in the IDE:

Project structure

The list of commands that could be executed is the following:

  • gradle projects: shows the root project and potential sub-projects.
  • gradle properties: shows the properties defined in the project.
  • gradle tasks: shows the tasks defined in the project.
  • gradle clean: cleans the project deleting the build directory.
  • gradle build: builds the project binaries.
  • gradle test: executes the tests present in the project.
  • gradle run: project execution.

These commands could be combined as follows:

gradle clean && gradle build && gradle test && gradle run

This command cleans the project, builds the binaries, runs the tests and executes the project.

If an IDE like Intellij is used, which has built-in support for Java, Kotlin and Gradle this commands could be executed from the IDE.

Project — Tests.

To execute the tests of the project, these two commands could be executed:

  • gradle test
  • gradle clean && gradle build && gradle test

The second command, more complete because it cleans and builds the project first, is used in the following picture:

Project tests execution

Project — Execution.

To execute the project, these two commands could be executed:

  • gradle run
  • gradle clean && gradle build && gradle run

The second command, more complete because it cleans and builds the project first, is used in the following picture:

Project execution

Conclusions.

This project serves as a starting point to Kotlin and the Gradle tool, written in Kotlin DSL. This is the first in a series of tutorials with the goal of beginning with “vanilla” Kotlin and up the ante integrating the language with frameworks such as Spring (MVC and the reactive Webflux) and Micronaut.

Stay tuned for these tutorials in the future!

--

--