如何正确设置/构建LibGDX + Kotlin项目?

时间:2018-07-11 05:16:33

标签: kotlin libgdx

我已经下载了gdx-setup.jar并尝试使用以下设置来构建空项目:

enter image description here

这是我在控制台中得到的内容:

Generating app in C:\Users\Divelix\Desktop\test
Executing 'C:\Users\Divelix\Desktop\test/gradlew.bat clean --no-daemon idea'
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/4.6/userguide/gradle_daemon.html.
Daemon will be stopped at the end of the build stopping after processing
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation'.
It will be removed at the end of 2018

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':android'.
> Failed to notify project evaluation listener.
   > com.android.build.gradle.internal.variant.BaseVariantData.getOutputs()Ljava/util/List;

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 22s
Done!
To import in Eclipse: File -> Import -> General -> Existing Projects into Workspace
To import to Intellij IDEA: File -> Open -> YourProject.ipr

如果我未标记为“ Use Kotlin”,它会很好地构建,那为什么会这样,我在做什么错呢?

1 个答案:

答案 0 :(得分:2)

好吧,我终于找到了如何使用Kotlin正确构建项目的方法(如果您在gradle方面遇到问题,它也将为您提供帮助)。首先,您只应在不使用“ Use Kotlin”的情况下进行构建,然后编辑项目的build.gradle文件,如下所示:

buildscript {
    ext.kotlinVersion = '1.1.1'

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

    }
}

如果在jcenter()之后有google(),也应该删除它{p}

:desktop中,将java替换为kotlin插件,然后添加kotlin-stdlib

project(":desktop") {
    apply plugin: "kotlin"

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    }
}

:android中添加kotlin-android插件和kotlin-stdlib

project(":android") {
    apply plugin: "android"
    apply plugin: "kotlin-android"

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.ashley:ashley:$ashleyVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    }
}

:core中,将java替换为kotlin插件,然后添加kotlin-stdlib

project(":core") {
    apply plugin: "kotlin"

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.ashley:ashley:$ashleyVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    }
}

如果只是Kotlin问题,就足够了。

如果您还有像我这样的gradle问题,请打开gradle-wrapper.properties并将gradle版本修复为3.3:

distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip
相关问题