Saturday 19 April 2014

LibGdx Google Mobile Ads SDK Tutorial

The number one ad service being used by Android and LibGdx developers at the moment is Google AdMob.

If you've not updated your app recently you should consider doing so soon. Google says:

Android (6.4.1 and earlier SDKs)
Deprecated. On August 1, 2014, Google Play will stop accepting new or updated apps that use the old standalone Google Mobile Ads SDK v6.4.1 or lower. You must upgrade to the Google Play version of the Mobile Ads SDK by then.

Ok, so we want to migrate to the new Google Play Services way of doing things - this blog post walks you through the process :)

Install this apk to see what we'll be building!


Barebones Sample App

First thing I did was grab an up to date version of libgdx. They are now up to 0.9.9 stable, I'm sure there is a lot of awesome new stuff in there for me to investigate, but TheInvaderOne isn't really leaving me with a lot of time for Android these days so that'll have to wait!

Next I created a new libgdx project using gdx-setup-ui.jar (see this old post for a walkthrough), added a .gitignore file, and made my initial commit.

Eclipse Setup

In eclipse, import the barebones sample app (file > import > existing projects into workspace) - you should now have at least three projects in package explorer (core, android, and desktop).

Open the Android SDK Manager, download the latest SDK Platform and Google APIs (at time of writing: 4.4.2/API19), the 2.3.1/API9 SDK Platform, and from Extras - Google Play Services.

Locate the <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ directory on your machine (on my windows machine - C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib) and copy into your working directory alongside the existing libgdx projects.

File > Import > Android > Existing Android Code, Next, Browse, navigate to the local copy of google-play-services_lib in your working directory, Ok, Finish.

Right-click your android project, select Properties, Android, scroll down and click Add, select the google-play-services_lib project, Ok.

A refresh and clean in eclipse probably wouldn't hurt at this point, so go ahead and do that.


Ensure that the target in android project's file is at least 13, and the android:minSdkVersion in your AndroidManifest.xml is at least 9. Sadly this does mean users running ancient versions of Android will be excluded, but there's nothing we can do about this. There are very very VERY few devices still running versions below 2.3/API9, so at least you won't be excluding many users...

Add these two lines as children of the 'application' element:
<meta-data android:name="" android:value="@integer/google_play_services_version"/><activity android:name="" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

Add these two permissions as children of the 'manifest' element:
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Save changes, then refresh and clean in eclipse for good luck...

Banner Ad

See this version of the android project's MainActivity class for a reasonably straightforward banner ad implementation.

Interstitial Ad

This diff shows an interstitial ad implementation (ActionResolver interface lets us trigger interstitial actions from the core project while retaining the invaluable LibGdx cross-platform functionality).


That's all there is to it!

As always, please feel free to leave comments below. I can't really offer support - TheInvaderOne and "Real Life Day Job" leave me very little free time these days, but other visitors will often leave helpful solutions, and if I find a spare moment every now and then I do try to answer any unresolved queries.

Anyway, I hope you have fun making your games - be sure to leave a note here if you release something on google play or the amazon market, I'm always happy to check out new games :)

PS - one final note if cloning from, pay attention to the problems view in eclipse! You will need to create an empty 'gen' directory in both the google-play-services_lib and tutorial-libgdx-google-ads-android projects, and ensure you have the required android sdks installed. As is often the case with eclipse, a liberal amount of refreshing and cleaning will do no harm...


  1. Well, i tried like that, it seems work, but even in your apk, there still a problem, the ad never shows...
    It says "loading ad" and that's all...
    An idea ?
    Btw, thatk's for your tutorial ! =)

    1. Hi Louka, thanks for posting :) first tap in my example app requests an interstitial ad, a toast message pops up to let you know. Soon after an interstitial ad should be received, and a second toast displayed letting you know. At this point tap the screen again to display the interstitial. If you are not getting the interstitial received toast, check you are connected to the internet. Another giveaway that you are not receiving ads from admob is that the banner ad won't be displayed either. Good luck getting to the bottom of your problem. If you are connected to the internet, connect your device to your machine and monitor logcat in ide, it is VERY helpful when troubleshooting this kind of thing!

    2. Well, the reason i wasn't working is so... Dumb...
      I forgot to add the line of my phone as a test device, so it weren't loading correctly, but with an other phone, it works well.
      (I mean, even if i add the test device line, the add still don't show on my own device... F*ck this shit, it works perfectly on others ! ;) )

      And a last "advice"... It could be very helpful for devs to have just one example to download, with only their ad-Key to write on and see the prog work, it could be nice to ad a link, not a github ;)

      Btw, thanks for your quick answer ! =D

  2. advertising crushes my APP, how can I avoid this?

    1. That's too vague, nobody can answer that kind of question... You need to use logcat and analyse the error. I'd guess maybe a problem with manifest as that's the easiest mistake to make, but really its just a guess 😁

  3. can u tell me please, how to deal with all screen sizes in android devices using libgdx (without black bars)? or maybe you can make tutorial for it?

    thanks & sorry for my bad english

    1. Hi BJ, my preferred way of dealing with resolutions is the black bars. This way all players can see the same amount of game world and the aspect ratio isn't stretched. The other options are of course to maintain aspect ratio and show more/less game world dependent on screen res, or to stretch the same amount of game world to fit the screen. Showing more game world is fine for some games, and practically cheating in others. Some games look fine stretched, others don't. Black bars works well for all games at the expense of losing a slice of screen space on some devices (though by targeting the most common resolutions you can minimise this). If you really don't want to use black bars, I would suggest looking into the Viewport and ScalingViewport classes - setViewport(new ScalingViewport(, MyGame.WIDTH, MyGame.HEIGHT)); is a nice and easy way to scale using black bars, Scaling.stretch could well do the trick if you are happy to screw with aspect ratio. Hope this helps and good luck making your game! :)

  4. Great tutorial! Thank you.
    If you have a little time check out my game:

  5. The tutorial getting Eclipse set up is awesome!

    Do you have any instructions that go along with the interstitial and banner ad code samples? I don't fully understand what I'm looking at.


  6. Thank you very much for your wonderful tutorials and blog posts! They have really inspired and helped me in the process of creating my own game - Sphere Assault. Please check it out:

    I really appreciate all that you have done to help other developers. Please keep up the good work!