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
- Download and install Java SE Development Kit (verify if installed).
- Download and install Android Studio (if not installed)
- Download the libGDX Setup App (gdx-setup.jar), and run it (by double-clicking on it).
- 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.
- Android SDK – specify location where Android Studio installed it (e.g. On Windows, typically:
- Check options for this tutorial, choose Sub Projects: Desktop, Android, and Html.
- Click button: Generate
- If any dialog pops up, it’s OK to specify newer tool or API versions.
Import into Android Studio
- Open Android Studio and choose: File > New > Import Project
- Select the project Destination folder specified above.
- Give Android Studio time to import, and accept any Gradle Update request dialogs.
- Wait for Gradle build to finish.
- Create a Run/Debug Configuration for our Desktop version (since most of our development and testing will be in this version).
- Select: Run > Edit Configurations…
- Click toolbar icon: +
- Choose: Application
- For ‘Name’, enter: desktop
- 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.
Note: Since we chose the Android module in the libGDX Setup App, libGDX then looks for all its resources in the Android module’s
assetsfolder. All the music, image, and texture files, etc. will need to go in this folder.
- Click: OK
- 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.
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:
This section describes the configurations for the Android version of our game.
No changes are necessary since the libGDX Setup App created an AndroidManifest.xml file that already meets the above requirements by defaulting to
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.
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:
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:
All the libGDX Launcher classes are now correctly configured, so you can move on to implementing your fantastic new game!