[LibGDX] Integrating RevMob

So, It took me awhile to get this working… and after I finally got it working… I realized RevMob isn’t what I wanted. Lol. :facedesk:

Anyway, let’s get started.

In your core project, create a new package. “com.companyname.gamename.monetization”

Create a new interface called RevmobAdInterface. (Created the same way as a class)
Make sure that class looks like this.

package com.companyname.gamename.monetization;

public interface RevmobAdInterface {
    public void showInterstitialAds(boolean show);
    public void showBannerAd(boolean show);
}

Once completed, head over to your android project.
Create a new package – Once again called com.companyname.gamename.android.monetization
Make that class name “RevMobIntegration” and look like this

Note: You’ll have to replace all of your imports with the proper thing, (IE Replace com.fmeg.tapout with com.company.game)
Note: This class assumes your main android class is AndroidLauncher.java. Your main class will extends AndroidApplication.

package com.fmeg.tapout.android.monetization;

import android.util.Log;

import com.fmeg.tapout.android.AndroidLauncher;
import com.fmeg.tapout.monetization.RevmobAdInterface;
import com.revmob.RevMob;
import com.revmob.RevMobAdsListener;
import com.revmob.RevMobTestingMode;
import com.revmob.ads.banner.RevMobBanner;
import com.revmob.ads.fullscreen.RevMobFullscreen;

public class RevMobIntegration implements RevmobAdInterface {
	
	private static final String APPLICATION_ID = "YourAdmobAppIDHere";
	
        // Set this to false when creating the version for the store.
	private static final boolean DEBUG = true;
	
	private RevMobAdsListener listener;
	
	private RevMobFullscreen fullscreenAd;
	private RevMobBanner bannerAd;
	
	private AndroidLauncher application;
	private RevMob revmob;
	
	public RevMobIntegration(AndroidLauncher _application) {
		this.application = _application;
		this.revmob = RevMob.start(application);
		if(DEBUG) {
			revmob.setTestingMode(RevMobTestingMode.WITH_ADS);
		}
		
		listener = new RevMobAdsListener() {

			@Override
			public void onRevMobAdClicked() {
				Log.i("[RevMob]", "Advertisement Clicked!");
				revmob.openAdLink(application, APPLICATION_ID, this);
				//showInterstitialAds(false);
			}

			@Override
			public void onRevMobAdDismiss() {
				Log.i("[RevMob]", "Advertisement Closed!");
				fullscreenAd.hide();
			}

			@Override
			public void onRevMobAdDisplayed() {
				Log.i("[RevMob]", "Advertisement Displayed!");
			}

			@Override
			public void onRevMobAdNotReceived(String message) {
				Log.i("[RevMob]", "No Advertisement Available!");
			}

			@Override
			public void onRevMobAdReceived() {
				Log.i("[RevMob]", "Advertisement Pulled from network!");
			}

			@Override
			public void onRevMobSessionIsStarted() {}
			@Override
			public void onRevMobSessionNotStarted(String arg0) {}
		};
		
		fullscreenAd = revmob.createFullscreen(application,
				APPLICATION_ID, listener);
		bannerAd = revmob.createBanner(application, APPLICATION_ID, listener);
		revmob.setTimeoutInSeconds(5);
	}
	
	@Override
	public void showInterstitialAds(boolean show) {
		if(show) {
			if(fullscreenAd == null) {
				fullscreenAd = revmob.createFullscreen(application,
						APPLICATION_ID, listener);
				fullscreenAd.show();
			} else {
				fullscreenAd.show();
			}
		} else {
			fullscreenAd.hide();
		}
	}
	
	@Override
	public void showBannerAd(boolean show) {
		if(show) {
			if(bannerAd == null) {
				bannerAd = revmob.createBanner(application, APPLICATION_ID, listener);
			}
		} else {
			bannerAd.hide();
		}
	}
	
	public RevMobBanner getBannerAd() { return bannerAd; }
}

Now, go to your main class for the android project (in my case it’s AndroidLauncher.java)

Search for

protected void onCreate (Bundle savedInstanceState) {

Remove the line

initialize(

and replace it with this.

// Custom Init
		RevMobIntegration revmob = new RevMobIntegration(this);
		RelativeLayout bannerLayout = new RelativeLayout(this);
		RelativeLayout applicationLayout = new RelativeLayout(this);
		bannerLayout.setVisibility(View.GONE);
		float screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
		int bannerHeight = (int) (180/1920f*screenHeight);
		
		RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, bannerHeight);
		
		adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        adParams.addRule(RelativeLayout.CENTER_VERTICAL);
		
        bannerLayout.setLayoutParams(adParams);
        
        View gdxView = initializeForView(new TapoutGame(revmob), config);
        applicationLayout.addView(gdxView);
        
        if(revmob.getBannerAd() != null) {
        	applicationLayout.addView(revmob.getBannerAd());
        }
        
        setContentView(applicationLayout);


This basically does everything that initialize does, however creates a relativelayout so we can draw the advertisement ontop of the LibGDX display. (I tried to override initialize, but it wouldn’t work)

Make sure to replace “TapoutGame” with your games main class. (in package com.company.game) in the core project

In the game class, add the following variable

private static RevmobAdInterface adInterface;

Create the following constructors.

public TapoutGame(RevmobAdInterface adInterface) {
		this.adInterface = adInterface;
	}

public TapoutGame( ) {

	}

Create a getter for the advertisements

	public RevmobAdInterface getAds() {
		return adInterface;
	}
	

Save, Compile. Complete.

Make sure to change your application id in RevMobIntegration.java.
Also, don’t forget to import the Jar file and set it as a library (The RevMob instructions show you how to do this)

To show a fullscreen advertisement you simply call

game.getAds().showInterstitialAds(true);

(from another class, considering game is what you called your instance of the main class)
or

getAds().showInterstitalAds(true);

from the main class.

To disable the banner advertisement, simply comment out the line

bannerAd = revmob.createBanner(application, APPLICATION_ID, listener);

in RevMobIntegration.java

If you want to change the height of the Advertisement, in your android main class, look for

	int bannerHeight = (int) (180/1920f*screenHeight); 

Edit the above to make changes.

Note, that there is some dead code at work here that I forgot to remove before posting…

@Override
	public void showBannerAd(boolean show) {
		if(show) {
			if(bannerAd == null) {
				bannerAd = revmob.createBanner(application, APPLICATION_ID, listener);
			}
		} else {
			bannerAd.hide();
		}
	}

in RevMobIntegration.java can be deleted, but you will have to remove

    public void showBannerAd(boolean show);

in RevmobAdInterface.java

So I’ve been trying to get this working for about 3 days.

Whenever I open my app, it automatically crashes. Does nothing need to be done to handle the EULA acceptation that shows when you first launch the app?

Following Logcat, the error seems to be coming from when I start the RevMob session. It originates at the line in the RevMobIntegration class constructor:

this.revmob = RevMob.start(application)

Java says that method is throwing a NullPointerException, but I can’t for the life of me figure out what it is. Application doesn’t appear to be null, and that seems to be the only thing going into it. I tried also running RevMob.startWithListener(application, listener) to no avail, same problem.

I’m assuming that this is due to something with the EULA, I don’t know if this article is supposed to cover that(If it’s like an automated process or something that I don’t need to do anything with), but I can’t even start the app at this point.

I’m honestly not sure what the problem is with your application, I haven’t had any problems with my implementation. (And no, I don’t use anything for the EULA, everything you see here is what I used)