LibGDX, Gradle, and Scala

Walk into a bar...

My spare time this week has been spent getting my tools (and my coffee) ready for the Ludum Dare 48-hour compo this weekend. Tonight's battle was getting the shiny new release of LibGDX building with the shiny new Gradle setup and playing nicely with my shiny Scala code. Everything's shiny, Cap!

Making this work was just a matter of Googling about (mostly because I haven't made time to read Gradle's docs; shame on me). But here I am trying to save you some time in case you're walking the Scala path like myself.

Step 1

Create your project. If you haven't already, just follow LibGDX's wiki. You will probably want to use the most recent versions of things (like the setup GUI). After that you have a root directory named after your game – let's assume you called it "test-game" – with subdirectories for the modules "core", "desktop", "android", etc.

Step 2

Edit your root build.gradle file. That's the one in the base "test-game" directory. Find the project(":core") section and add a couple lines so it looks like this:

project(":core") {
    apply plugin: "java"
    apply plugin: "scala"

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "org.scala-lang:scala-library:2.11.0"
    }
}

We've added the Scala plugin so Gradle knows to try to compile Scala sources, and added Scala as a dependency. You can of course choose a different version and add more packages as needed.

Now edit the build.gradle file in the "core" directory. Add this line:

sourceSets.main.scala.srcDirs = [ "src/" ]

Gradle now knows where to find your source files.

Step 3

That's it. Go make your game.

If you're using Eclipse (and the Scala plugin), you'll want to add the Scala nature to your core project.

I should mention I've only tested this with a desktop build as I don't intend to target Android or iOS for Ludum Dare. An HTML build seems to be completely out of the question since the project to get GWT and Scala to play nice appears to be long dead. Such is the price of being choosey about your language, I suppose.

Best of luck to all this weekend!