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.

推荐答案

因为AppEngine执行编译.类文件,它不关心JVM语言生成了这些文件.这意味着我们可以使用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.

Kotlin相关问答推荐

如何使用 Kotlin Maven 更改 Minecraft 插件中的 Shulker GUI 标题

垂直滚动条下拉菜单的桌面组合

使用调度程序运行异步 Kotlin 代码

如何使用函数类型或 lambdas 作为 Kotlin 上下文接收器的类型?

在jetpack compose中将默认方向设置为横向?

通用接口继承

第二个协程永远不会执行

如何将 `throw` 放置在辅助函数中但仍然具有空安全性?

Kotlin 列表扩展功能

内联函数导致单元测试代码覆盖率报告出错

Kotlin 中多个 init 块的用例?

如何在 Kotlin 中判断数组类型(不是泛型类型)

片段内的 Kotlin 按钮 onClickListener 事件

如何从kotlin中的类实例化对象

添加抽象的私有getter和公共setter的正确方法是什么?

用于代码生成的ANTLR工具版本4.7.1与当前运行时版本4.5.3不匹配

修改扩展函数中的this

导航架构组件 - 未生成 DestinationFragmentArgs

在 IntelliJ Idea 中未为 Kotlin @ConfigurationProperties 类生成 spring-configuration-metadata.json 文件

具有多个 parameter的 Kotlin 枚举