Thursday, July 31, 2014

Adding Chartboost to LibGDX games

Chartboost is a way of monetizing mobile gaming applications. In this article, I'm writing down the steps I followed to get Charboost SDK running on a LibGDX based Android game. 

The very first task we have to do is signing up for Chartboost by creating an account in their website. Then we can move into our dashboard where we can create a new app. Since our Android app is still not published into Google Play the only information we need to fill are (1) Platform name which is Android in our case, (2) App nickname which we fill with our app name, (3) App orientation which was portrait in my case, (4) Test mode which we set to 'enabled'. Now click the 'Done' button and note down the App ID and App Signature which we need later in our apps source code. Now, move into the 'Publish' menu and from their, click on the 'Add Campaign' -> 'Publish in the Network' option. Set a name for the campaign and select our app from the Android platform. Now click on the 'Save' button.

Now our configurations from the Chartboost dashboard side is ready. It's time to prepare our LibGDX game source code to use Chartboost. On this, the first thing we have to do is downloading latest Chartboost SDK from here. Go through the following steps to prepare our LibGDX source code to use Chartboost.

(1) Extract the downloaded Chartboost SDK ZIP folder to somewhere in the file system. Copy the chartboost.jar file, chartboost.jar.properties file and doc folder into the libs folder of the Android project in Eclipse. Those are the components which are useful to our project.

(2) Add a new interface called 'ActionResolver' into our LibGDX main project. The content of the 'ActionResolver' interface would be as follows. 

1
2
3
4
public interface ActionResolver {

 public void showChartBoostInterstitial(); 
}

(3) Make the Android projects MainActivity.java to implement that interface. Now put the following codes in the specified locations inside the Main Activity.

Inside OnCreate function:
// make sure to add the correct app ID and app signature from the Chartboost site.

1
2
3
4
5
6
7
 // Configure Chartboost
 this.cb = Chartboost.sharedChartboost();
 // temporary from my account
 String appId = "53d8b2ae8xxxxxxxxxxxxxxxxx";
 // temporary from my account
 String appSignature = "2c37f8f3380259b3efxxxxxxxxxxxxxxxxx";
 this.cb.onCreate(this, appId, appSignature, null);

Inside OnStart() function:

1
2
3
4
5
6
@Override
public void onStart() {
 super.onStart();  
 this.cb.onStart(this);     
 this.cb.showInterstitial(); 
}

Inside OnStop() function:

1
2
3
4
5
@Override
public void onStop(){
        super.onStop();
 this.cb.onStop(this); 
}

Inside OnDestroy() function:

1
2
3
4
5
6
@Override
public void onDestroy() {

        this.cb.onDestroy(this);   
 super.onDestroy();   
}

Inside OnBackPressed() function:

1
2
3
4
5
6
7
8
@Override
public void onBackPressed() {  
 
    if (this.cb.onBackPressed()) 
        return; 
    else 
        super.onBackPressed(); 
}

The function which was defined in the interface. We have to implement it here.


1
2
3
4
5
@Override
public void showChartBoostInterstitial() {
    
    this.cb.showInterstitial();
}

(4) Our ActionResolver interfaces' function implementation is inside the MainActivity of the Android project. To be able to call it from somewhere inside our LibGDX main project, we need to have access to the main activity instance within the main project. The way to do it is to pass the main activity instance as 'this' into the constructor of the initial class in the main libGDX project. The constructor of that class must be modified to accept an ActionResolver object. By doing so, we get the capability show Interstitials within our game whenever necessary by calling the ActionResolvers' showChartBoostInterstitial() function.

Now, building and running the LibGDX android project should show up Interstitials for testing purpose.

1 comment:

  1. Hey, nice tutorial you have here! I have 1 question, how do we add chartboost ads to a specific screen that we want like for example, game screen or between 2 levels? Thanks for your help!

    ReplyDelete