Skip to content
Simon edited this page Dec 2, 2020 · 5 revisions

Seems like you are interested to learn something new today! Good to hear ;)

Like with any other game we need to start with setting up our development environment.

I will use IntelliJ as IDE which should be similar to Android Studio. I won't explain how it works in Eclipse since I would not recommend this IDE unless you are working on a Rich Client Platform project which we don't. The version that I use while writing this page is 2019.2 with LibGDX 1.9.10 and LibKTX 1.9.10-b1

Update as of 2020-12-02: updated codebase to LibGDX 1.9.12 and LibKTX 1.9.12-b1.

To start it off download the latest version of LibGDX Setup Jar and run it. Provide the mandatory information according to your needs but before you click on Generate you should click on Advanced and tick Use Kotlin (refer to screenshot below).

In my case it mentions that I have a newer version for a couple of things. Just accept the newer versions and continue with generating your project files.

After that open the generated build.gradle file as mentioned in the setup window at the end.

Setup


Most likely your IDE will mention some warnings about deprecations and other stuff. Just ignore them. The reason is that LibGDX tries to support all important IDEs including eclipse and it also tries to stay compatible with older devices/versions of android/IOS and therefore those warnings are to be expected.


Convert your DesktopLauncher and Game class to Kotlin, add lateinit to the batch and img variable in your Game class and setup a run configuration. If everything works you should see the usual empty libgdx game window with the bad logic texture and a red background.

class Game : ApplicationAdapter() {
    lateinit internal var batch: SpriteBatch
    lateinit internal var img: Texture

Convert Create run configuration Update configuration Empty game screen

Known issues

In case you receive a "Could not load main class" error then don't be frustrated. This is a very common problem that a lot of people face out there. To save you the time of googling hours for a solution, here it is:

  • Go to the build.gradle file of your desktop project
  • Add following lines
    project.ext.mainClassName = "com.libktx.game.desktop.DesktopLauncher"
    project.ext.assetsDir = new File("../android/assets")
    
    + dependencies {
    +   runtimeClasspath files("../core/build/classes/kotlin/main")
    +   runtimeClasspath files("build/classes/kotlin/main")
    + }
  • Refresh your gradle project
  • Try it again. It should work now

In my opinion the official LibGDX setup is not creating very good gradle files. As an alternative you can use gdx-liftoff. It creates more modern gradle files which are causing less issues at least on my end.

Clone this wiki locally