Creating a libGDX Project for Android Studio

Photo by Sigmund on Unsplash

Creating a libGDX Project for Android Studio

In the future, I intend to write an article about developing games for Android with libGDX and the Dagger dependency injection library. In this article, however, I'll resort to describing how a simple Android libGDX project can be created with the libGDX project generator software.

Why libGDX

libGDX is a multi-platform Java game development framework that is based on OpenGL (ES). I have created games with libGDX before and have had good experiences with the framework. With libGDX, you'll need to develop more things yourself compared to actual game engines like Godot, Unity, and Unreal Engine, but you also have more control and options regarding how things are implemented in the game.

Here are links to the most recent Android game I've developed with libGDX:

Google Play Store: Space Shooter

GitHub: space-shooter

Initializing the libGDX Project

I expect that you have Java and Android Studio already installed. The Android Studio version I'm using when writing this article is Android Studio Giraffe 2022.3.1.

libGDX projects are automatically generated with a tool provided by the developers of the framework. You can download it from the link below:

Creating a Project - libGDX

When you run the .jar file, you should get a view like this:

I've inputted the following values into the view:

  • Project name: DaggerDemo

  • Package name: com.mkkekkonen.daggerdemo

  • Game class: DaggerDemo

  • Output folder: a subfolder in the directory where I store my code

  • Android SDK: the path in the AppData directory where the Android SDK resides

As you can see, I've selected Desktop and Android as the platforms. Next, you'll need to simply click the Generate button. You'll need some patience as the process can take a while. The resulting text that indicates that the generation has been successful is as follows:

BUILD SUCCESSFUL in 2m 6s
3 actionable tasks: 3 up-to-date
Done!
To import in Eclipse: File -> Import -> Gradle -> Existing Gradle Project
To import to Intellij IDEA: File -> Open -> build.gradle
To import to NetBeans: File -> Open Project...

Next, I open the project in Android Studio. I click File -> Open and navigate to the output directory specified in the project generator. The generated project is implemented with Gradle, and Android Studio should automatically recognize the project.

The first thing I did while opening the project was to update the Gradle plugin. A box like below was showing in the bottom right corner of Android Studio. I simply clicked the "Start AGP Upgrade Assistant" button.

Next, I clicked the "Run selected steps" button in the Upgrade Assistant.

A message will be displayed if the update has been successful.

Running the Desktop App

Out of the box, only the Android app can be run through Android Studio. This means that if we want to run the desktop app, we'll need a new run configuration. I opened the android dropdown that is shown in the below image and selected the Edit Configurations... option.

I clicked the Add New Configuration button that is displayed below:

Then, I selected the Application option:

I named the configuration simply Desktop. In the "Build and run" section I set the path to the JDK to C:\Program Files\Java\jdk-17.0.5 i.e. to the path where the Java 17 JDK resides. The path may vary in different systems.

Next, we'll need to set the main class:

A new window opens. I selected the DesktopLauncher option that is automatically presented in the window.

The field that says -cp <no module> must also be changed to -cp daggerdemo.desktop.main . When all the configurations are ready, the window should look somewhat like this:

Next, I selected Desktop as the run configuration and ran the project. The project should be built successfully and the following default desktop app window should appear:

Now you should be ready to start developing your game for both Android and desktop!

When it is finished, I will add a link here to the next article about libGDX, Dagger, and Android.