Kotlin on Google Cloud

A community-supported project

Quickstart Community Tutorials

Build rich backends in your favorite language

  • Create and deploy autoscaling Kotlin backends for your mobile apps
  • Write Kotlin using your favorite Java frameworks and libraries
  • Use fully-managed MySQL/PostgreSQL or Firebase for your application database
  • Run Kotlin in a containerized application at scale on Google Kubernetes Engine
  • Use IntelliJ with Cloud Code to add Google Cloud API libraries and deploy to Cloud Run.
Kotlin IDE

Write Kotlin using your favorite Java and Kotlin frameworks

Learn how to build an application on Spring Boot, Spark, and Ktor. Integrate your applications with MySQL using Cloud SQL.

View Tutorials
Access all of Google Cloud with Kotlin

Build your applications in Kotlin using Google Cloud APIs and services.

1
Set up a Google Cloud project

In the Google Cloud console, go to the Manage resources page and select or create a new project.

Go to the manage resources page

Enable billing for your project.

Enable billing

Enable the Cloud Storage API.

Enable the API
2
Add dependencies to your project
Add the Cloud Storage client library to your gradle.build under dependencies:
compile group: 'com.google.cloud', name: 'google-cloud-storage', version: '1.36.0'
3
Write your code
// import com.google.cloud.storage.StorageOptions
val storage = StorageOptions.getDefaultInstance().service
val bucket = storage.get(bucketName) ?: error("Bucket $bucketName does not exist.")

println("Listing all blobs in bucket $bucketName:")
bucket.list().iterateAll().forEach { blob ->
    println("${blob.name} (content-type: ${blob.contentType}, size: ${blob.size})")
}
1
Set up a Google Cloud project

In the Cloud console, go to the Manage resources page and select or create a new project.

Go to the manage resources page

Enable billing for your project.

Enable billing

Enable the Cloud Natural Language API.

Enable the API
2
Add dependencies to your project
Add the Cloud Vision client library to your gradle.build under dependencies:
compile group: 'com.google.cloud', name: 'google-cloud-vision', version: '1.35.0'
3
Write your code
// import com.google.cloud.vision.v1.ImageAnnotatorClient
// import java.io.File
val imgProto = ByteString.copyFrom(File(imageFileName).readBytes())
val vision = ImageAnnotatorClient.create()

// Set up the Cloud Vision API request.
val img = Image.newBuilder().setContent(imgProto).build()
val feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build()
val request = AnnotateImageRequest.newBuilder()
    .addFeatures(feat)
    .setImage(img)
    .build()

// Call the Cloud Vision API and perform label detection on the image.
val result = vision.batchAnnotateImages(arrayListOf(request))

// Print the label annotations for the first response.
result.responsesList[0].labelAnnotationsList.forEach { label ->
    println("${label.description} (${(label.score * 100).toInt()}%)")
}
1
Set up a Google Cloud project

In the Cloud console, go to the Manage resources page and select or create a new project.

Go to the manage resources page

Enable billing for your project.

Enable billing

Enable the Cloud Storage API.

Enable the API
2
Add dependencies to your project
Add the Firestore client library to your gradle.build under dependencies:
compile group: 'com.google.cloud', name: 'google-cloud-firestore', version: '0.58.0-beta'
3
Write your code
// Create the client.
val db = FirestoreOptions.newBuilder()
    .build()
    .service

// Fetch the document reference and data object.
val docRef = db.collection(collectionName).document(documentName)
val data = docRef
    .get() // future
    .get() // snapshot
    .data ?: error("Document $collectionName:$documentName not found") // MutableMap

// Print the retrieved data.
data.forEach { (key, value) -> println("$key: $value") }

Upload files to Cloud Storage

1
Set up a Google Cloud project

In the Google Cloud console, go to the Manage resources page and select or create a new project.

Go to the manage resources page

Enable billing for your project.

Enable billing

Enable the Cloud Storage API.

Enable the API
2
Add dependencies to your project
Add the Cloud Storage client library to your gradle.build under dependencies:
compile group: 'com.google.cloud', name: 'google-cloud-storage', version: '1.36.0'
3
Write your code
// import com.google.cloud.storage.StorageOptions
val storage = StorageOptions.getDefaultInstance().service
val bucket = storage.get(bucketName) ?: error("Bucket $bucketName does not exist.")

println("Listing all blobs in bucket $bucketName:")
bucket.list().iterateAll().forEach { blob ->
    println("${blob.name} (content-type: ${blob.contentType}, size: ${blob.size})")
}
1
Set up a Google Cloud project

In the Cloud console, go to the Manage resources page and select or create a new project.

Go to the manage resources page

Enable billing for your project.

Enable billing

Enable the Cloud Natural Language API.

Enable the API
2
Add dependencies to your project
Add the Cloud Vision client library to your gradle.build under dependencies:
compile group: 'com.google.cloud', name: 'google-cloud-vision', version: '1.35.0'
3
Write your code
// import com.google.cloud.vision.v1.ImageAnnotatorClient
// import java.io.File
val imgProto = ByteString.copyFrom(File(imageFileName).readBytes())
val vision = ImageAnnotatorClient.create()

// Set up the Cloud Vision API request.
val img = Image.newBuilder().setContent(imgProto).build()
val feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build()
val request = AnnotateImageRequest.newBuilder()
    .addFeatures(feat)
    .setImage(img)
    .build()

// Call the Cloud Vision API and perform label detection on the image.
val result = vision.batchAnnotateImages(arrayListOf(request))

// Print the label annotations for the first response.
result.responsesList[0].labelAnnotationsList.forEach { label ->
    println("${label.description} (${(label.score * 100).toInt()}%)")
}
1
Set up a Google Cloud project

In the Cloud console, go to the Manage resources page and select or create a new project.

Go to the manage resources page

Enable billing for your project.

Enable billing

Enable the Cloud Storage API.

Enable the API
2
Add dependencies to your project
Add the Firestore client library to your gradle.build under dependencies:
compile group: 'com.google.cloud', name: 'google-cloud-firestore', version: '0.58.0-beta'
3
Write your code
// Create the client.
val db = FirestoreOptions.newBuilder()
    .build()
    .service

// Fetch the document reference and data object.
val docRef = db.collection(collectionName).document(documentName)
val data = docRef
    .get() // future
    .get() // snapshot
    .data ?: error("Document $collectionName:$documentName not found") // MutableMap

// Print the retrieved data.
data.forEach { (key, value) -> println("$key: $value") }
Quickly find and debug issues

Google Cloud Observability provides powerful monitoring, logging, and diagnostics. It equips you with insight into the health, performance, and availability of cloud-powered applications, enabling you to find and fix issues faster.

Cloud Monitoring
Google Cloud Observability
Unified monitoring, logging, and diagnostics for applications on Google Cloud and AWS.
Stackdriver Error Reporting
Error Reporting
A walk through of getting an error alert and investigating the error in the Cloud console.
Stackdriver Monitor, diagnose, and fix
Monitor, diagnose, and fix
In this video, Aja Hammerly uses Cloud Monitoring, Cloud Logging, Error Reporting, and Cloud Trace to find and fix some subtle errors in an example app.
Learn more
Kotlin on Google Cloud communities

Got something to say? Join our community to ask questions, or just chat with the experts at Google who help build Kotlin for Google Cloud.