James Williams

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.

image of gdx-setup

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.

image of gradle buildscript

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

image of gradle core project dependencies


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.

If you'd like to learn more about making 2D games with libGDX, Udacity has couple courses and a nanodegree program, check them out.