Add libGDX to existing Android (Kotlin) project

May 17, 2018

LibGDX and Kotlin

Current libGDX version (night build v1.9.8) has issues with generating the boilerplate code when ‘Use Kotlin as the main language’ option is selected.

the error:

You can try generate the code for Java but the gradle scripts generated by libGDX are built for older versions of Gradle than the one used by the newest Android Studio 3.1.2 (stable) and the build process of your Android project may fail. Other than that if you’d like to add libGDX to your existing project (as opposed to importing generated code by libGDX) you need to update the gradle script for your app (app/build.gradle).

Here are the versions of my setup:

  • Android Studio 3.1.2

Build #AI-173.4720617, built on April 13, 2018 JRE: 1.8.0_152-release-1024-b01 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Linux 4.13.0-41-generic

  • Gradle version 4.4 (Android Studio build plugin 3.1.2)

  • Kotlin version 1.2.41-release-74 (JRE 8.0.5.11)

Empty Android Kotlin Project

Let’s create an empty Android Project.


Android projects have two files called build.gradle I will make changes to the one that is used by our app module : KotlinLibGDXBoilerplate/app/build.gradle (it’s basically placed in the root of the app folder).

Your file may have a similar content to this:

Now, the let’s add dependencies for libGDX and extensions like Box2d. You may not need all of them or you may only need libGDX alone, so feel free to configure it yourself by removing some lines. Firstly let’s put that somewhere at the top of the file:

Next we have to extend our dependencies:

Don’t worry I’m showing changes on images, you can download the entire code from my github repository (the link at the very bottom). And a tiny piece inside the android section:

Finally, the last part is to add a task that takes the native dependencies of the natives configuration, and extracts them to the proper libs/ folders so they get packed with the APK.

Now, go ahead and build the project:

Let’s test I added a simple demo to the boilerplate showing libGDX “in action” - nothing fancy, just a simple text transition launched once a button is clicked:


It works, so I hope it helps with seting up a new Android/Kotlin/libGDX project! A link to the code: https://github.com/rafalgolarz/android-kotlin-libdgx-boilerplate