LibGDX and RevMob integration

I figured I would do a small tutorial on this subject since I had to implement it recently on my current project.

I want to display Full Screen RevMob ad in my Android game in between two levels. Displaying banner ads is slightly different and won’t be covered here.

Setup and configure RevMob

  1. First, grab the RevMob SDK from their website, for this tutorial, I will pick Android as the target platform. (You can also check the configuration step from their website for the most up to date instructions)
  2. Copy the¬†revmob-n.n.n.jar in your android project libs’s folder and add it to your build path (in Eclipse, right click on the jar -> Add to build path)
  3. Modify your project’s manifest, add the following permissions
    • <uses-permission android:name="android.permission.INTERNET"/>
    • <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    • <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    • <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> (This one is optional but recommended)
  4. Add the following activity to your manifest (supposing you want to use full screen ads in your application)
    • <activity android:name=”com.revmob.ads.fullscreen.FullscreenActivity” android:configChanges=”keyboardHidden|orientation”> </activity>
  5. Also don’t forget to create your application in the RevMob console and grab your application ID

Configure LibGDX projects

Next you want to configure your LibGDX projects. Let’s suppose your game is multiplatform and you have an Android, Web, Desktop. You want to display RevMob ads only in your Android game and use another ads platform for Web/Desktop (since RevMob is mobile only) or maybe your desktop version won’t be displaying any ads anyway.

First let’s create an Interface in your main project to specify the actions correspond to the display of ads.

public interface AdsDisplayInterface{
   public void showFullScreenAds();
   public void openAdsLink();
}

This interface correspond to the different actions you might want to do in your game (for example show a full screen ads between two levels, or open the affiliate link when the “More Applications” button is clicked…)

Now you want to create a class implementing this Interface in the platforms where you want to display ads

In the Android project, we will create the following class

public class RevMobDisplay implements AdsDisplayInterface {
   private static final String REVMOB_APP_ID = &quot;copy your RevMob App ID here&quot;;
   private MainActivity activity;
   private RevMob revmob;

   public RevMobDisplay(MainActivity mainActivity){
      this.activity = mainActivity;
      revmob = RevMob.start(mainActivity, REVMOB_APP_ID);
   }
   @Override
   public void showFullScreenAds(){
      revmob.showFullscreen(activity);
   }

   @Override
   public void openAdsLink(){
      revmob.openAdLink(activity, listener);
   }
}

Now, in the main Android class, create an instance of this class and pass it as an argument to the Game constructor:

public class MainActivity extends AndroidApplication {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();

      initialize(new Game(new RevMobDisplay(this)), cfg);
   }
}

Eclipse should warn you that there is an error with this modified code because the Game constructor does not expect this argument.So we will create a new constructor that expects this argument in your main project’s Game class (this is the Class that implements ApplicationListener)

public class Game implements ApplicationListener {
   ...
   public AdsDisplayInterface _adsDisplay;

   public Game(AdsDisplayInterface adsDisplay){
      _adsDisplay = adsDisplay;
   }

   public Game(){
      ...
   }

   @Override
      public void create() {
      ...
   }
}

Displaying Ads

Now, whenever you want to display a fullscreen ad, you can simply do the following:

if (_adsDisplay!=null){
   _adsDisplay.showFullScreenAds();
}

Remember we only created this class for the Android application, so if we tried to display ads from another platform, it would crash with a null pointer exception, so we check if it is null beforehand.

There you go, let me know if you have any questions in comments.

Advertisements