Adam Hurwitz

@AdamHurwitz

Deploy A Backend App As An Android Engineer — Part 2

Iterate Fast With Ktor

Original photo by SpaceX on Unsplash

In part 1 I outlined how to deploy an AppEngine app by deploying a .jar file. Since then Google has listened to mobile devs needing to build server side solutions and has launched Kotlin on Google Cloud Platform.

One of the highlighted solutions is Ktor, which allows a standalone backend app in Kotlin that can interact with other libraries just as one would on the client side. The great thing about Ktor is obscure library compatibility issues like this one with Guava, which is still a mystery, are less of an issue because the code runs similar to how it would on an IDE in that you can define the main method of the app and let it run.

The Google Cloud Platform documentation has room for improvement. I went through 2 other configuration tutorials, Standard and Flexible AppEngine setups in order to realize Ktor was what I needed to deploy a Kotlin app to run server tasks without endpoints.

Despite Jars being slow to work with I still am using a Jar deploy for some use cases. For instance, authenticating with Firebase is straightforward and reliable with service accounts used in Jars, whereas with ktor there is this issue with authentication that I’ve discovered.

Jars are Janky

  • ~10–20 min deploy times.
  • Must rebuild .jar every time to test new code or different environment types.
  • Library incompatibility issues.

Tutorial: Run a Kotlin Ktor app on Google App Engine Standard

This tutorial was quick, clear, and concise. I highly recommend it! One aspect it left out is versioning which is important as I like to have a staging and production version deployed to AppEngine.

File Notes

build.gradle

App Engine Gradle Plugin Tasks and Properties

Find latest version of Ktor to use: github.com/ktorio/ktor/releases.

buildscript {
ext.ktor_version = '1.0.0'
...
}

Defining AppEngine version:

appengine {
deploy {
version = '1-2-0'
}
}

ClassWithMainMethod.kt

In order to run the main method from your IDE such as IntelliJ you need to comment out the Application.main method and use a main method within an Object IntelliJ can configure from Run/Debug Configurations. In order to run with AppEngine and deploy use the Application.main method.

import io.ktor.application.Application
fun Application.main() {
// App logic here.
}

Run with IntelliJ:

object Initialization {
@JvmStatic
fun main(args: Array<String>) {
// App logic here.
}
}

appengine-web.xml sample uses:

web.xml: Under src/main/webapp/WEB-INF/ sample use:

  • web.xml: defines mappings between URL paths and the servlets that handle requests with those paths

Managing SDK Configurations

Rather than building artifacts in IntelliJ repeatedly the command line can quickly help you create and navigate between AppEngine projects, run your app to test, and deploy to the server.

See this terminal commands guide to make use of this.

I’m Adam Hurwitz — hit the clapping hands icon and check out the rest of my writing if you enjoyed the above. Thanks for reading!

More by Adam Hurwitz

Topics of interest

More Related Stories