How to start creating android games using the LibGDX game development framework

UPDATE: There is a newer version of this article, which includes using the Setup GDX UI to create LibGDX projects much faster: http://chrismweb.com/2013/03/25/how-to-start-creating-android-games-using-the-libgdx-game-development-framework-updated-with-gdx-setup-ui-jar/

Why Android?

I’ve been creating games using flash/actionscript for a while now, but have really wanted to get started with android game development, since it’s an open source platform, the sky is the limit to how much you can learn about it.

The devices are usually fairly non-restrictive, being able to load custom roms, install any available software – even not directly through the market, being more practical (using internal storage as storage, not needing to install bloatware to connect to a computer), as well as having many more choices in hardware, since there is such a wide diversity of available mobile phones.

All in all, Android is very non-restrictive and flexible to it’s users, as well as it’s developers, and the market is supposed to be much easier to upload your app on, without getting turned down without a reason. It also caters more towards free apps (with ads), which is what I had wanted to offer, to allow everyone to be able to play my games.

Why LibGDX?

I have read a few android programming books, and most go into quite a bit of detail about application programming, but very little about OpenGL, not to mention I hadn’t done much programming using multiple separate threads in the past.

LibGDX takes care off pretty much all of the low-level stuff for you, so you can focus on game programming, or basic engine programming, and not about techniques for loading different image and music types, creating and handling multiple threads, or creating OpenGL surfaces to render 2d bitmaps on to.

Using it also does not take away any of the control you have, if you wanted to or needed to program lower level stuff, such as creating surfaces and 3d objects using OpenGL. Thus, it provides an excellent platform to get started with without having to worry about creating everything from scratch yourself, but also allows you to create things later on when you need them.

And for super-icing on the cake, it is specifically made for creating games, and it also allows you to compile and test using native Java on your computer before deploying to your android device, which makes debugging and compile time so much faster! No needing to deal with the android emulator for most of your design, unless you wish to, no needing to compile the apk, move it to the SD card, install, open, and finally debug- you can do it all on your computer before deploying to your android device!

So in summary:

  • Handles a lot of the stuff that you would normally need to program from scratch, saves you time, as well as allows you to expand it if needed
  • Specifically created for games!
  • Debug, test and try out your android app without having to rely on having a device/ emulator running almost all the time!
  • Open source and free, as well as great forums for support

So Now that you know why it is a great framework to use to develop android games, lets get up and running.

Downloading and installing everything you need

The things we’ll be downloading and installing include:

  • Java SE JDK/SDK (to be able to compile Java code)
  • Eclipse IDE (for creating/editing code, compiling, running and deploying your game)
  • LibGDX .jar library files
  • Android SDK
  • Eclipse ADT plugin

Downloading and installing the Java SE JDK

Android programming by default is done in Java. To be able to program in it, you will need the Java JDK. You can get the Java SE JDK from from:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Download and install it with the defaults (unless you want to chose a different folder).

Downloading and setting up the Eclipse IDE


Now that you have the Java SDK, you can download your IDE for programming and compiling.
You can use any IDE you want, but it is recommended to use the Eclipse, so that is what I use, although it took a little bit of time to get used to it.

Download it from:
http://www.eclipse.org/downloads/
And select ‘Eclipse IDE for Java Developers’, for whichever OS you use (I use windows 64bit).

Once it finishes downloading, open up the zip file. It does not come with an install, but just an eclipse folder. Just move it out onto your hard drive somewhere that you will want to keep your android programming stuff (I put mine in “c:\android\eclipse”).

Next, open up that folder, and double click on “eclipse.exe” (it will have a blue circle icon with 3 horizontal white lines through it). This will open Eclipse, and prompt you for a workspace location, which is where all of your source files for any game projects will go. I put mine in “c:\android\workspace”, and click on “Use this as the default and do not ask again”.

You can just close the welcome screen by clicking on the ‘X’ on the top left, which will bring up the main interface you’ll be using. On the left is the “package explorer”, where your different games/ projects will go. In the center is where you’ll be coding. The bottom is where you’ll see the console, and any syntax problems before compiling your game.

Now that we have Java Ready to go, we need to get the stuff for creating android applications/games.

The Android SDK

Head on over to http://developer.android.com/, click on “SDK” on the top tab, and download the SDK .zip.
It should contain one folder called “android-sdk-windows”, which you just need to move into your android folder area you created earlier (or anywhere you like, really), which I put at: “c:\android\android-sdk-windows”.

Note: developer.android.com is a great place to find info on setting up getting started developing android apps, as well as for reference.

Installing the ADT plugin for Eclipse

Now we need to install the ADT plugin to allow eclipse to easily create android applications/games.

Open Eclipse (if not already open), click on ‘Help->Install New Software” from the top menu.
Click the ‘Add’ button, and enter ‘ADT’ for name, and ‘https://dl-ssl.google.com/android/eclipse/’ for location(You can also see these instructions at: http://developer.android.com/sdk/eclipse-adt.html#installing – and there is also a manual method if yours does not connect) and click ‘OK’. (if it doesn’t seem to download, you may need to turn off a firewall if you’re using it- zone alarm would not let it fetch properly while on)

Select “ADT – http://dl-ssl.google.com/android/eclipse/” in the drop down box for “Work with:”, if it was not already selected. Below, after a time, it should show “Developer Tools” under “Name”. Click the check box next to it, and click “Next”and click “Next” again on the next screen. Read through the license agreement, click the radio button for “I accept…” and click “Finish”.

It should have a popup for “Installing software”, and another popup warning for “installing unsigned content”, which you just need to click “OK” to, since you trust android for adding the ADT to your eclipse.

Click “Restart Now” on the next popup, and let Eclipse Restart.

Once this restarts, you can “Configure/setup” your ADT plugin.

Setting up the ADT plugin for Eclipse

We just need to setup the ADT plugin for android development.

You first need to tell it where you put the Android SDK, so click on “Window->Preferences” on the top menu. Click “Android” on the left side, click on “Browse” next to “SDK Location” and go select where you unzipped your android SDK folder (“C:\android\android-sdk-windows” is where I put it on my computer). and click “OK” twice to get back to eclipse. I had a popup for “Android SDK Verification” “SDK Platform Tools component is missing”, and to install it through the SDK Mangaer, which we’ll do next. Click “Ok” again.

Next Click on “Window->Android SDK and AVD Manager” from the top window. The first screen is for creating “virtual android devices”- to later test out different display sizes and configurations to make sure your app/game looks good (although it runs very slow compared to a real device, or in Java on your computer).

Click on “Available packages”, click the small arrow next to “Android Repository”. From here click whichever ones you think you will need, or all if you have extra disk space + time for downloading. I just needed “Android SDK Platform-tools”, “Documentation for Android”, “SDK Platform Android 3.2, API13”, as well as “API 3” for android 1.5, and samples for API13.
Click “Install Selected”, “Accept all” radio button on the new popup, and “Install”. Next the download window will popup, and it will begin downloading the selected Android packages, which may take a while.
Afterwards, it should ask you if you want to restart ADB, so click yes.

Now that we have the ADT and the SDK for android setup, we could begin developing applications or games without LibGDX by using the top menu and selecting “File->New->Project->Android->Android Project”, but we will first setup LibGDX and a quick game demo, to get right into game development.

Downloading LibGDX

Go to http://code.google.com/p/libgdx/ to download the latest LibGDX version on the right hand side of the screen (libgdx-0.9.2.zip at this time).

After it downloads, create a folder in your android folder, and copy the files from the zip file into it (I created the folder “libgdx092″, so c:\android\libgdx092”, but it doesn’t really matter what you call it as long as you remember it).

Creating a Java project with LibGDX

One of LibGDX’s biggest strong points is being able to compile and debug in native Java on your computer, without having to try it out on your android phone or the emulator, which saves massive amounts of time.

It also used the java project as a reference, so everything you create there will be part of your android project, so you don’t have to re-type anything.

Open Up Eclipse, click on “File->New->Java Project”, and name the project something clever, such as “MyLibGDXGame”. Now make sure you change the execution environment to JavaSE-1.6.
(Otherwise it for some reason work with the android version) And click “Finish”.

Right click on your Java project folder on the left hand panel, and click “New->Folder”, enter the “Folder name” as “libs”(short for libraries), and click “Finish”. You should see your “libs” and the “src” folder under the project folder on the left hand side.

Now we need to add the required libraries to use LibGDX.

Go into the folder where you unzipped your LibGDX stuff, and find and copy the following files:
gx-backend-jogl-natives.jar
gdx-backend-jogl.jar
gdx-natives.jar
gdx.jar

Then, go back into Eclipse, and paste them into your “libs” folder.

Now that they are there, we just have to tell our project to use them.

In Eclipse, from the top menu, click on “Project->Properties”. Click on “Java Build Path” on the left panel, “Libraries” from the top, and “Add JARs” button.
Double click on your project, double click on your “libs” folder, and click the first .jar file, hold shift and click the last one, so that all the jar files are highlighted, and click “Ok” (alternatively you could have done them one by one repeatedly). Click “OK” again to get back to the main eclipse window.

Now that we have access to our LibGDX libraries, we’ll create the main class.

Creating the basic classes

Right click on the src folder, and click on “New->Class”. Change the package to something like “com.mylibgdxgame”, and change the “name” to “DesktopStarter”. Click on the checkbox next to “public static main(String[] args”, and click “Finish”.

Now delete the auto generated comments, and we’ll put one of our own:

package com.mylibgdxgame;

public class DesktopStarter 
{

	public static void main(String[] args) 
	{
		//Create a new JoglApplication, which we need to pass our main GAME class when it is finished

	}
}

So now we have the “main” class, which will be used on the desktop to start our game class, after we create it, which we’ll now do.

Right click on the src folder again, and add another new class. This one we will name “Game”, put the package name the same as the “DesktopStarter” class (com.mylibgdxgame) and click “Finish”.

To make this “Game” class derive from LibGDX, we have to implement the base class “ApplicationListener”. So after “class Game”, type “implements App” and press the “Ctrl+Space” keys together to bring up the auto-complete window. double click on “ApplicationListener – com.badlogic.gdx”, which will import the required library to use this class above your class definition line.
You should notice a folder icon with a red X through it on the left of the line with your class on it, and “Game” underlined with a squiggly line. If you hover over “Game” It will show you the problem with the line: “The type Game must implement the inherited abstract method ApplicationListener.create()”. Rather than typing out the function/method missing (create()), you can have eclipse auto-correct the problem by clicking “Add unimplemented methods”.
As you can see, this will not only fix the “create” method missing error we saw, it will also create a bunch of other required function, including “dispose”, “pause”, “render”, “resize”, and “resume”, so that we don’t have to put each one in manually.

package com.mylibgdxgame;

import com.badlogic.gdx.ApplicationListener;

public class Game implements ApplicationListener
{

	@Override
	public void create() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

}

We’ll probably want to also add something visually to the screen, just for kicks.
We’ll add a somewhat conventional vector smiley face that moves each frame.

Change the code to the following:

package com.mylibgdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Game implements ApplicationListener
{
	//whatever screen width and height we want to have in the desktop version
	public static int SCREEN_WIDTH = 800;
	public static int SCREEN_HEIGHT = 480;
	
	//things needed to draw
	SpriteBatch batcher;
	Texture texture;
	Pixmap pixmap;
	
	Vector2 smiley_pos;
	
	
	@Override
	public void create() 
	{
		Gdx.app.log("MyLibGDXGame", "Game.create()");
		//we'll get whatever the set width is- 800x480 above, but will be the device resolution when running the android version
		SCREEN_WIDTH= Gdx.graphics.getWidth();
		SCREEN_HEIGHT= Gdx.graphics.getHeight();
		
		//setup these 3 for rendering- sprite batch will render out textures, and pixmaps allow you to draw on them
		batcher = new SpriteBatch();
		pixmap = new Pixmap(256, 256, Pixmap.Format.RGBA8888);
		texture = new Texture(pixmap);
		
		//setup where we want out smiley face vector graphic to start at
		smiley_pos = new Vector2(SCREEN_WIDTH/2-256/2, SCREEN_HEIGHT/2-256/2);
		
		//function to draw a smiley face on the pixmap, which we can use to draw on the texture
		DrawSmiley();
	}
	private void DrawSmiley()
	{
		Gdx.app.log("MyLibGDXGame", "Game.DrawSmiley()");
		//-------Going to draw a smiley face on the pixmap, which will show on the texture
		//draw a yellow circle for the smiley faces head
		pixmap.setColor(1, 1, 0, 1);
		pixmap.fillCircle(256/2, 256/2, 256/2);
		
		//first draw a black circle for the smile
		pixmap.setColor(0, 0, 0,1);
		pixmap.fillCircle(256/2, 140, 80);
		
		//then a yellow larger over it, to make it look like a partial circle/ a smile
		pixmap.setColor(1, 1, 0, 1);
		pixmap.fillCircle(256/2, 100, 100);
		
		//now draw the two eyes
		pixmap.setColor(0, 0, 0,1);
		pixmap.fillCircle(256/3, 100, 30);
		pixmap.fillCircle(256-256/3, 100, 30);
		
		texture.draw(pixmap, 0, 0);
		texture.bind();
	}

	@Override
	public void dispose() 
	{
		Gdx.app.log("MyLibGDXGame", "Game.dispose()");
		//dispose any object you created to free up the memory
		texture.dispose();
		pixmap.dispose();
		batcher.dispose();
	}

	@Override
	public void pause() 
	{
		Gdx.app.log("MyLibGDXGame", "Game.pause()");
	}

	@Override
	public void render() 
	{
		//need to get the deltaTime- the amount of time passed since the last frame in seconds. We'll want to move everything based on the time passed- so if it renders slower on the phone, it should still move at the same rate
		float deltaTime=Gdx.graphics.getDeltaTime();
		
		//clear the background to black each frame- otherwise we'll see our smiley face "ghosting" as it moves
		GLCommon gl = Gdx.gl;
		gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_STENCIL_BUFFER_BIT);
		
		//start the batcher, so we would want to do all of our draw calls between batcher.begin and .end
		batcher.begin();
		batcher.draw(texture, smiley_pos.x, smiley_pos.y);
		batcher.end();
		
		//move it up to 4 px per frame
		if(200*deltaTime<4)
			smiley_pos.x+=200*deltaTime;
		//otherwise, we'll only want to move him 4pixels every frame
		else
			smiley_pos.x+=4;
			
		//if the left side of the smiley face is off the left side of the screen, move it to -smiley width- which puts the right side of the smiley face just off the left side of the screen, so it looks like it wraps around
		if(smiley_pos.x>SCREEN_WIDTH)
			smiley_pos.x=-256;
	}

	@Override
	public void resize(int arg0, int arg1) 
	{
	}

	@Override
	public void resume() 
	{
		Gdx.app.log("MyLibGDXGame", "Game.resume()");
	}
}

Line 3: For all the import statements, we just type out the classes that we need, use “ctrl+space” to autocomplete it, which automatically also adds the import statement above, of the classes we’re using for this file.

Line 15: Next we set up a static screen size, which will let us put down whatever screen resolution that we want to test, and see the changes in our Java version, so we can have an idea about how our game will look on devices with other resolutions that we haven’t been able to test on.

Line 19: From there, we create a SpriteBatcher for the class, which will let us render out many different textures if we need to, and be more optimal, since it will all happen at once later.
The texture is what we can actually draw using the SpriteBatcher. With OpenGL, all your textures NEED to be an multiple of 2- so 32,64,128,256,512,1024,2048 ect. You can have a texture 512px X 1024px, but you can’t have one 800×480, since both numbers aren’t a multiple of 2.

This seems very restrictive, but there are ways around it, which also increase efficiency. You can have “spritesheets”, with multiple different images on an overall texture that is a multiple of 2. So the engine only needs to load one image file, which saves time loading, and you can have many images stored on that one image.
You can do this in Photoshop, but if you have a lot of images, or might update them regularly, I highly recommend Texture Packer, it’s a great little program that saves loads of time. The free version works for everything, but if you want to have optimal texture packing for the smallest memory use, you would be wise to later get the “pro” version with more efficient algorithms.

Line 98: Next, we create a ‘smiley_pos’ 2d vector to keep track of the x and y position we will be drawing the smiley face at.

Line 27: The ‘create’ function is one of the many built in functions implemented from “ApplicationListener”. Here, we set up and initialize everything that we will need for the game.

Line 29: ‘Gdx.app.log()’ is used to log stuff out to the console, at the bottom of the eclipse window. This is the primary way you debug your game- output stuff, so you can see where it fails, or values that you might not be sure of, so you can trace down where problems might be, and there will always be problems 😉

Line 31: We set the screen width/height to whatever GDX has for the current screen dimensions- which will be whatever we put at the top of our game class if running the Java version of our app, or whatever the native resolution is for our android version.

Line 35: Next we setup the objects we’ll need to draw out our vector image. We made the pixmap 256×256 (Note: this is a power of 2, because it has to be), and we assign the texture the pixmap that we’ll be drawing on.

Line 40: We then setup the starting position of the smiley face to be centered – at half the width and height of the screen, minus half the width and height of the image (so the center of the image is aligned with the center of the screen).

Line 43: Lastly, we call out “DrawSmiley” function/method which will actually draw the smiley face on the pixmap, and the pixmap on the texture.

Line 71: We’ll set our “dispose” method to dispose of any object that we created that has a dispose function.

Line 87: Finally, we get to our main game loop, which is the “render” method. Every frame this gets called, so if we move, or do stuff here, it will update on the screen each frame.

Line 90: We create a variable for the deltaTime- the amount of time passed since the previous render, which we’ll use to move our smiley face at the same speed, regardless of render speed.

Line 93: The next 3 lines are used to clear the screen, so we start with a black canvas every frame, otherwise we’d see our smiley face not clear each frame, and see a “ghosting” effect from having each image rendered on top of the old ones.

Line 98: Now we finally get to rendering our texture with the smiley face out to the screen. We have to start the batcher before we draw, and end it after we finish drawing everything.

Line 103: We’ll check if the time passed multiplied by 200 is less than 4, and if so, we’ll move it by that much. If it tries to skip over 4, we’ll only move it at 4. This makes it so that when it loads, the first frame might be before initializing everything, and the second one might be a second longer, so it could stutter and move the smiley face 200px in one frame, and be an obvious stutter!

Line 110: Finally if the left side of the smiley face is off of the right side of the screen, we’ll move it so that the right side of the image is at the left side of the screen- so it appears to wrap around the screen.

Now that we have the game class created, which will be shared between both the desktop and android version, we can go back to the “DesktopStarter” class, and tell it to start the game.

So back in the “DesktopStarter.java”, change it to the following:

package com.mylibgdxgame;

import com.badlogic.gdx.backends.jogl.JoglApplication;

public class DesktopStarter 
{

	public static void main(String[] args) 
	{
		//Create a new JoglApplication, which we need to pass our main GAME class when it is finished
		new JoglApplication(new Game(), "My First LibGDX Game", Game.SCREEN_WIDTH, Game.SCREEN_HEIGHT, false);
	}
}

If you started typing out “JoglA” and press ctrl+space (Eclipse auto complete), the code completion will have shown you two different options, one of which was “new JoglApplication(arg0, arg1, arg2, arg3, arg4)”, which it tells us is an application listener, a string, 2 ints, and a boolean value.
But what the heck do they need to be? This is where the API documents come in very handy, which are located at: http://libgdx.l33tlabs.org/docs/api/.
On the left hand side, scroll down and click on “JoglApplication”, which will bring up information about that class in the main window. You’ll see the line:
“JoglApplication(ApplicationListener listener, java.lang.String title, int width, int height, boolean useGL20IfAvailable) “, which tells you exactly what you want to know- the ApplicationListener is your game class, the title is whatever you want to call your game, the width, height, and whether you want it to use OpenGL 2.0 if available (we just set this to false for now).

Now with the DesktopStarter.java still selected, hit F11 on your keyboard (or the little bug icon on the top left of your screen), run as a Java Application, and you should see the smiley face moving on screen.

Now that we have something to show as our first “game” using LibGDX and showing in java on our computer, we’ll make the Android project to be able to run it on our phone or the android emulator, which can be setup through the android SDK.

Creating the Android Project

Now we can finally create the Android project, to run our small demo. In Eclipse, click on “File->New->Project”. Double click on the “Android” folder, then click on “Android Project”, and click “Next”.

Give it a project name of “MyLibGDXGameAndroid”, click on the highest available build target (Android 3.2 in my case- one of the things we downloaded through the ADT plugin), change the “package name” to “com.mylibgdxgame”, and the min SDK version to “3”, which is android 1.5. This allows you to let your application be installed on lower versions of android, as well as being able to use more recent features, if available on the device. Click “Finish”, and it will create your Android Project.

Now we need to add the libraries for libGDX that we need for our android version. Right click on “MyLibGDXGameAndroid” project on the left hand side, and click “New->Folder”, name it “libs” and click “Finish”.
Go to the folder on your computer where you unzipped the LibGDX files, and copy:

  • The two arm folders (armeabi, and armeabi-v7a)
  • gdx.jar
  • gdx-backend-android.jar

and copy them into your android project “lib” folder.

Now we have to tell the project about them again, like before. So Click on the top “Project->Properties”, click on “Java Build Path” on the left hand side.
While we’re here, we will add our desktop game to the resources, so that it uses those files to create the android game.
Click on “Projects” at the top of this window, click “Add…” on the right hand side, click the check box next to “MyLibGDXGame”, and click “OK”.

Next click “Libraries” at the top, click “Add JARs” on the right, double click “MyLibGDXGameAndroid”, double click on “libs”, and click on the two jar files, with shift held for the second to select both of them, and click “OK”.

Now, back in the main Eclipse window, on the left side in the projects window/”package explorer” double click the ‘src’ folder inside your “MyLibGDXGameAndroid” project, and double click on your package name (com.mylibgdxandroidgame), and double click on the default created main class (MyLibGDXGameAndroidActivity), which will bring up that class file in your editor on the right hand side.

We need to change it from a default activity, to one that is derived with libgdx functionality.

You should originally have something like:

package com.mylibgdxgame;

import android.app.Activity;
import android.os.Bundle;

public class MyLibGDXGameAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Which we’ll change to:

package com.mylibgdxgame;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.mylibgdxgame.Game;
import android.os.Bundle;

public class MyLibGDXGameAndroidActivity extends AndroidApplication {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        initialize(new Game(), false);
    }
}

We are just extending the “AndroidApplication” class from LibGDX, and starting our Game class from the Java version of the project, and that is it for programming!

Hit ‘F11’ (or click the little bug icon towards the top of Eclipse), click “Android Application”. If you have an android device plugged in to your usb (and with “USB Debugging” enabled under “settings->Applications->Development” on your android) you can just double click on your device at the top of this new window (Android Device Chooser). If you don’t or would rather try it through the emulator for now, you need to create an android virtual device (just create an android 1.5 device to test on- maybe give it 64mb “SD card” in the settings)

(Note: If you are trying to run on your device, but it does not show it when you try to run, you need to install the USB drivers for your device. You can find some here:
http://developer.android.com/sdk/win-usb.html, but you may need to download them for your device
for HTC devices, I uploaded the 32bit and 64bit drivers:
HTC driver for 32-bit Windows 7:
HTC_Driver_32
HTC driver for 64-bit Windows 7: HTC_Driver_64
[right click on my computer, properties, device manager, find the android device, right click and update driver, point to the folder where you unzipped your usb driver])

(Note: It may take a few minutes to load in the emulator, should show the android logo, then finally get to a conventional home screen, and finally launch your app. If it doesn’t, but is at the home screen, you can try hitting “F11” again in the eclipse interface, while leaving the emulator open, to make it compile again. You can see the progress on the bottom of the eclipse interface in the console window- or clicking on the “debug” on the top right will bring you to the debug perspective, which will show you the ‘Logcat’ window, with even more information)

Now you should see the smiley face moving on your android device, or the android emulator!!

What is next?

Now that you have a basic “game” working on both the java and android version, it would be best to start making it more of a game, with some interaction, loading graphics and displaying them.

We’ll go through this in the next part, but until then I reccomend looking at the game demo’s for libGDX, the jumper game is especially helpful (showing you how to load textures, use input, play music+ sounds, create different screens, and a menu system)

Resources

You can see the LibGDX forums (Search for your question before asking at: http://www.badlogicgames.com/forum/

You can see Mario’s blog (one of the creators of LibGDX) at: http://www.badlogicgames.com/forum/

The main LibGDX sites are:
The google code site with a few tutorials and a video: http://code.google.com/p/libgdx/
The new main site for LibGDX: http://libgdx.badlogicgames.com/

Bookmark the permalink.

16 Comments

  1. WOW THANK U SOOO MUCH…….. 🙂 IT HELPED ME ALOT

  2. thx a lot.

    good job.

  3. Hi! puneet i had the same problem but i just modify the order and i put the project desktop on top in Order and export (your project android -> properties -> java Build Path -> Order and Export ) and it work now on device

  4. Best Android + LibGDX article ever!

  5. 32bit Virtual Machine images and emulators cannot run libgdx programs, as it does not compile to x86.

    If it doesn’t work on the device, try what mamadou said:
    Android-project -> properties -> java Build Path -> Order and Export
    Click the project you’ve linked to (your plain java-project or desktop-project) and click the “Up”-button until it is at the very top. Make sure it’s ticked [V].
    I did that with the jars too, just in case, and now it runs on my device. I still get that same error on my x86 VM, because libgdx does not support x86 Android devices

    • Thanks- but it finally actually did work on the x86 emulator. Project somehow got screwed up- created a new one, imported source, and that x86.so from the forums and everything works great on x86 now 😀

  6. This is a very helpful tutorial for me :]

    Very small nitpick though: for your Line 19 comment, you said:
    “The texture is what we can actually draw using the SpriteBatcher. With OpenGL, all your textures NEED to be an multiple of 2– so 32,64,128,256,512,1024,2048 ect. You can have a texture 512px X 1024px, but you can’t have one 800×480, since both numbers aren’t a multiple of 2. ”

    I think you mean “powers of 2”, since 800 and 480 are clearly multiples of 2. However, I understood what you meant, so no big deal :]

  7. If you get this problem ‘Could not find class ‘com.mylibgdxgame.Game’, referenced from method com.mylibgdxgame.MyLibGDXGameAndroidActivity.onCreate..’

    Try to go to your android project > Properties > Java Build Path.

    Then Click on ‘Projects’ and press the ‘+’ on the project that’s there(its probably your normal java version of the game).
    Then select the ‘Access rule’ and click on ‘Edit..’.
    Press ‘Add…’.

    Choose for Resolution : ‘Accessible’ and enter ‘*’ as the ‘Rule pattern’.

    That should solve it.

  8. Hi. I don’t know why I can’t use the new JoglApplication (….). It tells me JoglApplication cannot be resolved as a type. When I try to manually import import com.badlogic.gdx.backends.jogl.JoglApplication; it asks me to “Create Class JoglApplication”. Everything else in the class game it seems to be working, so the lib should be well installed… Thanks in advance

    • I think they now have deprecated jogl, so instead they now use lwjgl, but you use it exactly the same.
      There is also now a setup.jar that you can use to create your projects easily, which is in the nightly builds.

      I’ll update the tutorial to use LwjglApplication instead of jogl. Thanks!

  9. Thanks amigos ! All works !

  10. Hey chris when running the application either on device or on emulator i am getting this error and then i am not able to generate the .apk file what could be the reason for it ?

    [2013-03-21 12:28:00 – FirstLibgdx] Dx
    trouble processing “java/io/BufferedInputStream.class”:

    Ill-advised or mistaken usage of a core class (java.* or javax.*)
    when not building a core library.

    This is often due to inadvertently including a core library file
    in your application’s project, when using an IDE (such as
    Eclipse). If you are sure you’re not intentionally defining a
    core class, then this is the most likely explanation of what’s
    going on.

    However, you might actually be trying to define a class in a core
    namespace, the source of which you may have taken, for example,
    from a non-Android virtual machine project. This will most
    assuredly not work. At a minimum, it jeopardizes the
    compatibility of your app with future versions of the platform.
    It is also often of questionable legality.

    If you really intend to build a core library — which is only
    appropriate as part of creating a full virtual machine
    distribution, as opposed to compiling an application — then use
    the “–core-library” option to suppress this error message.

    If you go ahead and use “–core-library” but are in fact
    building an application, then be forewarned that your application
    will still fail to build or run, at some point. Please be
    prepared for angry customers who find, for example, that your
    application ceases to function once they upgrade their operating
    system. You will be to blame for this problem.

    If you are legitimately using some code that happens to be in a
    core package, then the easiest safe alternative you have is to
    repackage that code. That is, move the classes in question into
    your own package namespace. This means that they will never be in
    conflict with core system classes. JarJar is a tool that may help
    you in this endeavor. If you find that you cannot do this, then
    that is an indication that the path you are on will ultimately
    lead to pain, suffering, grief, and lamentation.

    [2013-03-21 12:28:00 – FirstLibgdx] Dx 1 error; aborting
    [2013-03-21 12:28:00 – FirstLibgdx] Conversion to Dalvik format failed with error 1

    • Hey, I saw your post yesterday, but I wasn’t entirely sure what causes that problem.

      It might be that the Android project has some duplicate jars as the desktop? Does it work fine on the desktop version, but not the android version?

      For the android project, you only have to have its files in the build properties, and the desktop project.

      I’ve been meaning to update the tutorial- there is a much easier way to setup your projects now, that does it all automagically. In the LibGDX nightlies, use: “gdx-setup-ui.jar”- double click on it, and it will actually walk you through creating your project, and setting up the libraries and everything- so it you have the eclipse environment all setup, you can just run it perfectly without having to mess with anything in eclipse every time.

  11. very nice article. this will help a lot for android game and app programmer and developer to develop a interactive mobile app. thanks for posting.

Leave a Reply

Your email address will not be published. Required fields are marked *