Wednesday, May 21, 2014

Integrating Admob to LibGDX based games

Recently, I had to find out about how to put mobile advertisements of Admob on Android based games which are based on LibGDX. After searching on the web and reading different online articles, I figured it out. Since, I will be using this thing in most of the future LibGDX based games, I decided to write down the procedure I followed in a blogpost so that I will not forget it. So, here I write down the sequence of steps I went through to achieve it. 

1) The very first thing we have to do is creating an account in the Admob website in the URL:
After creating the account, you should login to your account and goto the monetize tab. There you can find the "Monetize new app" option which you should select. Now, you should create a new ad unit by filling the necessary information. Copy the ad unit ID to some temporary place which you need later in your programming codes. 

2) Open your eclipse IDE with the workspace where your LibGDX based game project resides. From the Android SDK manager, install the "Google Play services" library which is listed under the 'Extra' category. Now you have to import this installed library into your eclipse workspace.
Click File > Import, select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it. (It should be copied from the original location which is  <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/)

3) Make the imported "google-play-services_lib" a library for the android project by right-clicking on the android project, and following the rest of the instructions in the following URL.

4) Add these lines to the android manifest file,

under <application> tag,
    <meta-data android:name=""             android:value="@integer/google_play_services_version" />

            android:name="" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

under <manifest> tag,
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

5) Right-click on the android project, select properties. Goto the Android tab and set the build target API to something high as 15. (screenSize & smallestScreenSize attributes are not available in SDK 10.They have been introduced in API level 13.)

6) Now the background configurations are ready. The only thing left is to put some lines of codes to your LibGDX based game. Open the '' source file in Android project and add the following content to the 'onCreate' function.

 AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();  
 cfg.useGL20 = false;  
 cfg.useAccelerometer = true;  
 RelativeLayout layout = new RelativeLayout(this);  
 View gameView = initializeForView(new Main(), cfg);  
 AdView adView = new AdView(this);  
 AdRequest adRequest = new AdRequest.Builder().build();  
 adRequest.isTestDevice(this); //comment out later  
 RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
 layout.addView(adView, adParams);  

For more information, please refer to the URL:

Cheers! :)


  1. Thanks your article it's very helpfull

  2. I have successfully integrate both admob and startapp in libgdx. You can refer the code here :

  3. Hey man
    Thnx to this post I integrated admob into my funmusicplayer a year ago - thnk you much!
    But two months ago I saw this post: and decided to try Appodeal. It's programmatic mediation and allows you to use it in link with Admob account. It has also a lot of ad networks like chartboost, startapp, liverail, etc. in one sdk. I use interstitials in my game and thnx to them i've doubled my revenue. The ecpm in october and november is about $5.9 and I strongly advise you to try it.
    hope this information is useful for you