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.