Blog

Kotlin, Gradle, and the Cloud

1 gwkh x1wcy3xqveg6tnw8g (1)

How to build Kotlin apps in the cloud with Gradle.

Kotlin is Java reinvented, incorporating 20 years of hard-learned lessons.

It was built with all the features of a modern programming language that Java is slowly gaining, but which cannot be fully retrofitted such as:

  • Type inference
  • Lambdas
  • Pattern matching
  • Non-method functions
  • Null safety
  • Smart casts
  • …and much more.

Kotlin easily interoperates with Java for a huge selection of libraries and minimal transition cost.

Though Kotlin is better known as an official language for Android, it is a general-purpose language, and I was curious about using Kotlin in the cloud — in a Docker container, of course. So I decided to try that out, drawing on my background in Java.

Though I have more experience with Maven as a tool for gathering dependencies and building Java code, I also set out to learn some Gradle, as it is slightly favored over Maven for building Kotlin apps.

Read further to see how to make this happen.

1 gwkh x1wcy3xqveg6tnw8g (1)
Clouds over Kotlin Island

Instructions

  • Install Docker.
  • Install Gradle. Your OS’s package manager will help you with that: On Mac, that is just brew install
  • From here on, feel free to just copy my simple project and work with that. Or follow these steps to get there.
  • Set up your Gradle/Kotlin project. This guide from the Kotlin side is useful, as is this one from Gradle. I’ll put them together and lay out how to set up a new app as easily as possible.
  • Create your directory and switch to it. E.g., mkdir multicloud_pubsub && cd multicloud_pubsub .
  • Run gradle init, then enter these in the interactive menus: 2 (application), 4 (Kotlin), 1 (no: only one application project), 1 (build scripts in Groovy), Enter and Enter for default project and sample source package names.
  • Type gradle runfor a beautiful “Hello World!”
  • To add your own code, move on as in my sample project at Github here — which is a small part of an asynchronous app designed for maximum decoupling and robustness in the fact of cloud instability. I’m planning to write a separate article on that soon.
  • Put code in a package-based subdirectory under src/main/kotlin, and delete the sample code in src/main/kotlin and src/test/kotlin .
  • Delete the sample guava in build.gradle, and add your dependencies. These are just the same artifact identifiers that you use in Maven, with a more readable syntax.
compile 'com.google.cloud:google-cloud-pubsub:1.42.0'
compile 'com.google.cloud:google-cloud-storage:1.42.0'
  • Add the Kotlin standard runtime library tobuild.gradle with the following standard library in thedependencies section. There are multiple versions of this, sorted out in this article, but this is the one to use. The jdk8 suffix does not mean that you can’t use higher Java language or bytecode levels when mixing Java and Kotlin code; it’s just that the Kotlin runtime is itself compiled at Java 8 bytecode level.
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
  • Edit the mainClass inbuild.gradle , in this case 'com.doitintl.mulicloud.ProcessorKt' . Note the Kt suffix on the filename, for a synthetic class giving access to the main function.
  • Add the following to the end ofbuild.gradle. It builds an all-in-one jar file usable with java -jar app.jar
jar

{

manifest

{

attributes 'Main-Class': mainClass

}

from

{

configurations.implementation.collect

{

it.isDirectory() ? it : zipTree(it)

} } }

  • Test that with gradle clean run .
  • Now, on to the cloud! Put this Dockerfile in the directory.
  • Note that we use a separate Gradle image at build-time, allowing us to use a lightweight, clean image at runtime.
  • We use using OpenJDK’s Java image. The Kotlin standard runtime library is built with the application, so there is no need for a Kotlin image.
  • Build the image. Don’t forget the dot at the end to designate the build directory.
docker build -t pubsubapp .

Now run it.

docker run pubsubapp

That’s it! Since Docker images are so wonderfully self-contained, your knowledge gained in apps built with any other technologies will apply equally to one built with Kotlin and Gradle. There is more to do, like service accounts to create and roles to grant, as usual; I’ve created a script for that. (See the README.)

Enjoy!

Subscribe to updates, news and more.

Related blogs