Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Kotlin in AppEngine projects using Gradle

Like the title says, how can I use Kotlin when developing AppEngine projects? I'm using IntelliJ/Android Studio with Gradle as my build tool.

like image 670
Kirill Rakhman Avatar asked Apr 08 '15 11:04

Kirill Rakhman


1 Answers

Since AppEngine executes compiled .class files, it doesn't care what JVM language produced these. This means we can use Kotlin.

One way to do this is by using Gradle and the Gradle App Engine plugin. Create a project with a build.gradle that looks something like this. Then add the Kotlin dependencies and apply the plugin. The final build file looks something like this:

buildscript {
    ext.kotlin_version = '1.0.6' //replace with latest Kotlin version
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.appengine:gradle-appengine-plugin:1.9.32' //Replace with latest GAE plugin version
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

repositories {
    mavenCentral();
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'war'
apply plugin: 'appengine'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.32' //Replace with latest GAE SDK version
    compile 'javax.servlet:servlet-api:2.5'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

appengine {
    downloadSdk = true
    appcfg {
        oauth2 = true
    }
}

Since M11 you don't need to have a separate directory for Kotlin files so you can just add your .kt files to src/main/java.

like image 60
Kirill Rakhman Avatar answered Nov 17 '22 22:11

Kirill Rakhman