Skip to content
  • Products
    • Portfolio overview >

      Flexsave™

      Automatically manage cloud compute for optimized costs and agility

      Cloud Analytics

      Make sense of spend and chargeback to align with your business

      google cloud msp

      BigQuery Lens

      Optimize BigQuery costs with actionable recommendations and usage insights

      Spot Scaling

      Maximize AWS Spot savings and minimize disruptions for optimized scaling

      Anomaly Detection

      Autonomously identify cost spikes early, with zero configuration

      Platform overview >

      Organize your billing data for better business decisions

  • Services
    • Services overview >

      How we work

      Learn how we’re redefining support with our customer reliability engineering

      Stats

      View our live support and customer satisfaction statistics in real-time

      Cloud solutions

      Proven solutions to cloud complexity

      Areas of expertise

      Cloud Architecture

      Ensure your cloud architecture is future-ready and built for success

      Cloud Cost Optimization

      Identify opportunities to optimize costs and target spend for added value

      Cloud Migration

      Realize greater efficiency and innovation with successful cloud migration

      Cloud Security

      Center security in your cloud strategy to ensure ongoing efficacy and growth

      Data and Analytics

      Harness the potential of big data and analytics to gain a competitive edge

      Data Management

      Build your data practice with expert guidance tailored to your business goals

      DevOps Jump Start

      Accelerate your AWS workloads & release pipelines while also increasing automation, monitoring & reliability

      Infrastructure

      Maximize the full suite capabilities from your cloud infrastructure

      Kubernetes

      Manage the complexity of Kubernetes to enable innovation and scalability

      Location-Based Services

      Transform geolocational data into real-world, real-time intelligence

      Machine Learning

      Level-up key data with ML capabilities that accelerate innovation

      Multicloud

      Create meaningful business value with a robust multicloud strategy

      Training

      Build skills and capability across teams with certified, expert-led training

  • Partners
    • Alliances

      Proud to be an award‒winning multicloud partner to top‒tier cloud providers

      doit-together

      DoiT Together

      Enabling cloud growth and unlocking revenue through expert partnership

      ISV Go-Global

      Accelerate new customer growth and Marketplace integration on AWS and GCP

  • Resources
    • Resources hub >

      Blog

      Read the latest insights, tips and perspectives from our team of cloud experts

      Case Studies

      See how we’ve helped thousands of public cloud customers achieve their goals

      Ebooks and Guides

      Discover foundational expertise and future-ready recommendations for the cloud

      Events and Webinars

      Tech talks and interactive expert sessions delivered both virtually and in person

      GCPInstances.info

      Google Cloud Compute Engine instance comparison

      Help center

      Read documentation, product updates, and more

      Newsroom

      See what's new from DoiT in our latest news and announcements

      Trust Center

      How we focus on security, compliance, and privacy

      Videos

      Watch product demos, interviews and more from our cloud experts

  • About
    • About DoiT >

      Careers

      Browse our open positions and learn more about what it takes to be a Do’er

      Leadership

      Meet the team leading DoiT and our customers on a journey of hypergrowth

      Newsroom

      See what's new from DoiT in our latest news and announcements

  • Pricing
  • Contact us
  • Sign In
  • Products
    • Flexsave ™
    • Cloud Analytics
    • Spot Scaling
    • BigQuery Lens
    • Anomaly Detection
    • DoiT Platform
  • Services
    • How We Work
    • Stats
    • Cloud Solutions
    • Areas of expertise
      • Cloud Architecture
      • Cloud Cost Optimization
      • Cloud Migration Consulting Services
      • Cloud Security
      • Data and Analytics
      • Data Management
      • DevOps with AWS & DoiT
      • Infrastructure
      • Kubernetes
      • Location Based Services
      • Machine Learning
      • Multicloud
      • Training
  • Partners
    • ISV Go-Global
    • Award-winning public cloud partner
    • DoiT Together
  • Resources
    • Blog
    • Case Studies
    • Ebooks and Guides
    • Events and Webinars
    • GCPInstances.info
    • Help center
    • Newsroom
    • Trust Center
    • Videos
  • Pricing
  • About
    • Careers
    • Leadership
    • Newsroom
  • Contact us
  • Sign In
Contact us
Sign in

Blog

Kotlin, Gradle, and the Cloud

  • Joshua Fox Joshua Fox
  • Date: February 22, 2021

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.

Subscribe

Subscribe to updates, news and more.

Related blogs

Ramp Plans Resource Hub Header1

Monitor your cloud commit attainment with DoiT Ramp Plans

DoiT Ramp Plans help you visualize, manage, and track your commit attainment so you can make sure you spend what you committed to, and act proactively.

Keep reading 
Secure-access-to-GCP-services-in-GitLab-Pipelines-with-Workload-Identity-Federation-DoiT-International

Secure access to GCP services in GitLab Pipelines with Workload Identity Federation

Traditionally, when using services like Google Cloud in a non-GCP environment (e.g., a CI/CD environment like GitLab pipelines), developers

Keep reading 

3 Tips to Improve Your AWS Cost Optimization Strategies

Global cloud spend is expected to approach the $600 billion mark in 2023 – 21% growth over the previous

Keep reading 
View all blogs
Let’s do it

From cost optimization to cloud migration, machine learning and CloudOps, we’re here to make the public cloud easy — without the costs.

Ready to get started?

Get in touch

Company

  • About us
  • Blog
  • Careers
  • MS-HT statement
  • Newsroom
  • Privacy policy
  • Terms

Offering

  • Compliance
  • Products
  • Services
  • Solutions
  • Resources

Support

  • Sign in
  • Help center
  • Open ticket
  • Contact us

Never miss an update.

Subscribe to our newsletter

Subscribe to updates, news and more.