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: https://apps.admob.com
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.
https://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject

4) Add these lines to the android manifest file,

under <application> tag,
    <meta-data android:name="com.google.android.gms.version"             android:value="@integer/google_play_services_version" />

    <activity
            android:name="com.google.android.gms.ads.AdActivity" 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 'MainActivity.java' source file in Android project and add the following content to the 'onCreate' function.

 super.onCreate(savedInstanceState);  
 AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();  
 cfg.useGL20 = false;  
 cfg.useAccelerometer = true;  
 RelativeLayout layout = new RelativeLayout(this);  
 requestWindowFeature(Window.FEATURE_NO_TITLE);  
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  
 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);  
 View gameView = initializeForView(new Main(), cfg);  
 AdView adView = new AdView(this);  
 adView.setAdSize(AdSize.BANNER);  
 adView.setBackgroundColor(Color.WHITE);  
 adView.setAdUnitId("our-admob-adunit-id-should-go-here");  
 AdRequest adRequest = new AdRequest.Builder().build();  
 adRequest.isTestDevice(this); //comment out later  
 adView.loadAd(adRequest);  
 layout.addView(gameView);  
 RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
 adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);  
 adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);  
 layout.addView(adView, adParams);  
 setContentView(layout);  

For more information, please refer to the URL: https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx

Cheers! :)

4 comments:

  1. Thanks your article it's very helpfull

    ReplyDelete
  2. I have successfully integrate both admob and startapp in libgdx. You can refer the code here : http://sheerapps.com/integrate-admob-startapp-libgdx/

    ReplyDelete
  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: http://forum.xda-developers.com/monetization/mobile-ad-networks/appodeal-experience-vs-admob-t3157116 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
    Cheeers:)

    ReplyDelete