Set up libGDX game in Android Studio

A quick tutorial for setting up your libGDX game to run in Android Studio on Windows

First we’ll create a libGDX project, then import it into Android Studio, and finally configure the desktop, Android, and HTML5 modules.

NOTE: These instructions and screenshot(s) are based on a Windows 10 machine running Android Studio 2.1.2.

Create New libGDX Project

  1. Download and install Java SE Development Kit (verify if installed).
  2. Download and install Android Studio (if not installed)
  3. Download the libGDX Setup App (gdx-setup.jar), and run it (by double-clicking on it).libGDX setup app
  4. Enter:
    • Name – enter the display name of your game (spaces are OK).
    • Package – enter the base package for your game (e.g. com.mycompany.mygame).
    • Game class – enter the name of your main game class (e.g. MainGame).
    • Destination – enter your new game folder to hold all its project files (e.g. C:\dev\MyGame).
    • Android SDK – specify location where Android Studio installed it (e.g. On Windows, typically:  C:\Users\YourUsername\AppData\Local\Android\sdk)
  5. Check options for this tutorial, choose Sub Projects: Desktop, Android, and Html.
  6. Click button: Generate
  7. If any dialog pops up, it’s OK to specify newer tool or API versions.

Import into Android Studio

  1. Open Android Studio and choose: File > New > Import Project
  2. Select the project Destination folder specified above.
  3. Give Android Studio time to import, and accept any Gradle Update request dialogs.
  4. Wait for Gradle build to finish.
  5. Create a Run/Debug Configuration for our Desktop version (since most of our development and testing will be in this version).
    1. Select: Run > Edit Configurations…
    2. Click toolbar icon: +
    3. Choose: Application
    4. For ‘Name’, enter: desktop
    5. Under Configuration tab, specify:
      • Use classpath of module – click dropdown arrow and choose: desktop
      • Main class – click browse (…) and choose: DesktopLauncher
      • Working directory – click browse (…) and navigate to ‘android/assets’ folder (e.g. C:\dev\MyGame\android\assets)

        Note: Since we chose the Android module in the libGDX Setup App, libGDX then looks for all its resources in the Android module’s assets folder. All the music, image, and texture files, etc. will need to go in this folder.

    6. Click: OK
    7. Run the Desktop configuration, and the application will pop up with the bad logic logo image on a red background.

Configure libGDX Launcher classes

Your libGDX game is now runnable. However, a few extra tweaks will improve your development and enhance your game.

For all the platform versions chosen in this tutorial for our game, we establish for the graphics, a target resolution of 800 x 480 pixels (landscape aspect ratio) — which is currently typical for an Android mobile device. To achieve this we need to add platform-specific configuration code to the libGDX Launcher classes for the chosen ‘desktop’, ‘android’, and ‘html’ modules.

Desktop module

For the desktop version of our game, we need to specify a 800 x 480 window size, and optionally set the title for the name of our game. For this we modify DesktopLauncher.java like this:

DesktopLauncher.java
package com.mycompany.mygame.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mycompany.mygame.MainGame;

public class DesktopLauncher {
    public static void main(String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.title = "My Game";
        config.width = 800;
        config.height = 480;
        new LwjglApplication(new MainGame(), config);
    }
}

Android module

This section describes the configurations for the Android version of our game.

AndroidManifest.xml

No changes are necessary since the libGDX Setup App created an AndroidManifest.xml file that already meets the above requirements by defaulting to android:screenOrientation="landscape"

Note: If the game needed to run in portrait mode, we would set this attribute to android:screenOrientation="portrait" and swap the resolution width and height for the desktop and html modules.

Unlike the ‘desktop’ module, we can’t define the resolution of the Activity since it is set by the Android operating system. So instead, our game will need to handle this by scaling the 800 x 480 target resolution to whatever the device’s resolution is.

AndroidLauncher.java

The only changes we need to make to the AndroidLauncher.java file are optional, such as conserving the battery by disabling the accelerometer and the compass like this:

AndroidLauncher.java
package com.mycompany.mygame;

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.mycompany.mygame.MainGame;

public class AndroidLauncher extends AndroidApplication {
	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
		config.useAccelerometer = false;
		config.useCompass = false;
		initialize(new MainGame(), config);
	}
}

HTML module

For the HTML5 version of our game, we need to specify a 800 x 480 drawing area. For this we modify the HtmlLauncher.java file like this:

HtmlLauncher.java
package com.mycompany.mygame.client;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.gwt.GwtApplication;
import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration;
import com.mycompany.mygame.MainGame;

public class HtmlLauncher extends GwtApplication {
        @Override
        public GwtApplicationConfiguration getConfig () {
	    return new GwtApplicationConfiguration(800, 480);
        }

        @Override
        public ApplicationListener createApplicationListener () {
            return new MainGame();
        }
}

Conclusion

All the libGDX Launcher classes are now correctly configured, so you can move on to implementing your fantastic new game!