James Williams

Navigation

Using Kotlin with libGDX

I've been kind of fascinated with Kotlin recently and when I learned
of the #libGDXJam, I decided to use it for whatever game I'd end
up creating. It turns out that it is remarkably easy to add Kotlin
support to a libGDX game.

1.Generate your game without the html target.

The JS sources are generated with Google Web Toolkit which operates on
Java source files. Because your files will be in Kotlin, the GWT
compiler will complain about them. This is a bit of a bummer but I'd
wager that you are more likely to deploy to desktop or mobile anwyays.

2.Tweak your root level build.gradle file.

In the buildscript section, you'll need to add the
kotlin-gradle-plugin on the classpath. This is so that the core
project will be able to find it.

In the core project, add apply plugin: "kotlin" and the compile
dependency as follows. You can add a variable in allprojects.ext for
the kotlinVersion or hard code it here. Either works. Use the same version
number from this page for both the kotlin-gradle-plugin and
kotlin-stdlib versions

3.Profit.

One thing you might notice is that sometimes the Android build gets
confused and says it can't find your game/application file. To fix that, go to the
AndroidLauncher file and make sure it has a proper import for your game/application.