Using Kotlin in Google App Engine

Jon Imanol Durán
4 min readApr 6, 2017

--

Google App Engine standard environment offers an excellent location to host your backend application; it is easy to use, easy to maintain and for most of the use cases it’s free. This feature combo makes App Engine a perfect place for prototyping and trying new ideas. Just write your code, upload it and you have your application ready to serve millions of users!

Sounds great, right? But as always, there is a negative point. To enable Google offering this kind of service, they need to enforce developers to run their application in a restricted environment. Most known limitations are incapability to open local files for writing, open socket connections or execute operating system actions. Besides that, if you want to run your application in the standard environment you have a limited choice of development languages to use; Go 1.6, Java 7, PHP 5.5 and Python 2.7. Selection seems to be OK, but the problem resides in the fact that versions hardly get updated… That’s probably familiar to any Android developer reading this, right?? :P

On this post, we are going to present a way to introduce a setup to let you use a new language that runs smoothly in Google App Engine standard environment. Kotlin! This new language introduces new features that for example Java (specially oldish 7 version…) does not provide:

  • Null safety
  • Lambda expressions
  • Extension functions; extend classes without inheriting them. Bye bye to utility mess!
  • Hey, the list is too long! Read more here :)

Besides the basic setup, on this example we will use Gradle as build system to have easier dependency management, and Dagger2 as library to enhance Dependency Injection in your code. All in one, out of the box! Yes, you caught me… I have Android development background :-)

Gradle setup

Gradle is an open source build system that provides the functionalities of apache Ant and Maven, using Groovy based DSL instead. It’s easy to maintain, easy to deal with dependencies and very extensible.

You can also use Gradle to test, build and upload your Google App Engine application to cloud. Just one command and you will have your application running. To enable it you need to use the Gradle Google App engine plugin in your project. No worries, it’s easy! Add following line into your gradle dependency section:

And also add new maven url to let gradle find the plugin:

Now, you have your gradle ready to build App Engine applications. Last but not least, you need to let gradle know which plugin need to be use during build process:

As you can see, we’ve applied java, war and appengine plugins. All are required to let the build system run app engine applications. On the example we will also need endpoints dependency to offer data over a RestAPI using Google Cloud Endpoints. Besides that, on the appengine section on the gradle file, we’ve added the settings to let the appengine download the required SDK.

Now, you should be able to build a Google App Engine application using Gradle… But, we don’t have any code to run. As you can see, there is no Kotlin setup in the gradle file so if we try to use any Kotlin source file it will basically fail on compilation time. We need to add Kotlin into our build system… Let’s go!

Kotlin Setup

We need to add kotlin gradle plugin into our dependency section. Before we added app engine plugin into it, so just a new line is needed there. It should look like this:

We also need other dependencies into compile time dependencies:

As you may have noticed, we also added kotlin-kapt to enable Kotlin annotation processing to our app. We are going to make use of Dagger2 to allow the use of dependency injection into our app. You will see later into our example how to inject dependencies into our endpoint classes :-) If you are using Intellij Idea IDE, you need to explicitly write that it needs to look for generated source code into kapt folder.

And… done! You are ready to compile Kotlin code and upload it to Google App Engine. Next, let’s go through a simple example built in Kotlin that exposes an endpoint, uses dagger to inject a required dependency into the endpoint class and you can execute into your local machine using gradle.

Example

You can clone the full example from github and use it as reference for your future applications. As a side note, if you want to upload the code to Google Cloud remember to change your application name in the file “appengine-web.xml”.

In this example, we’ve created an example endpoint that will serve it’s functionality under following route “_ah/api/kotlin/v1/greetings/{yourName}” . Dagger has injected the ExampleService dependency into our awesome endpoint offering us easy way to add any kind of external dependency to satisfy our requirements. This is a very basic use of Dagger, you can read and make use of the rest of dagger functionalities reading the official user guide. The endpoint route and definition is done using Google cloud endpoints annotations. Remember to change your appEngine web.xml setup to let this endpoint handle routed requests.

But… is it really working? Sure it does! You just need to run a single command on your console and have the backend running in your local machine. Execute following command on the root folder of the cloned project:

./gradlew appengineRun

After gradle fetches all dependencies, you can test your backend using the following url in your local computer:

localhost:8080/_ah/api/kotlin/v1/greetings/you

And now, there are no excuses to use a new language in App Engine!

Happy Kotlin!

--

--