简体   繁体   English

如何使用Gradle在AppEngine项目中使用Kotlin

[英]How to use Kotlin in AppEngine projects using Gradle

Like the title says, how can I use Kotlin when developing AppEngine projects? 就像标题所说,在开发AppEngine项目时如何使用Kotlin? I'm using IntelliJ/Android Studio with Gradle as my build tool. 我正在使用带有Gradle的IntelliJ / Android Studio作为我的构建工具。

Since AppEngine executes compiled .class files, it doesn't care what JVM language produced these. 由于AppEngine执行编译的.class文件,因此它不关心JVM语言产生的内容。 This means we can use Kotlin. 这意味着我们可以使用Kotlin。

One way to do this is by using Gradle and the Gradle App Engine plugin . 一种方法是使用Gradle和Gradle App Engine插件 Create a project with a build.gradle that looks something like this . 使用看起来像这样build.gradle创建一个项目。 Then add the Kotlin dependencies and apply the plugin. 然后添加Kotlin依赖项并应用插件。 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 . 从M11开始,您不需要为Kotlin文件创建单独的目录,因此您只需将.kt文件添加到src/main/java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM