Summary

Introduction

This tutorial will guide you for all the steps to add Airpush adds in your libGDX Powered Android Games. You will be able to add both Banner Ads, and Smartwall Ads. The basic structure is very like the Admob Tutorial. But there are several changes and it will be more straight forward with not much text. (If you want a very detailed explanation of why every step, feel free to read that tutorial first).

You need:

  1. A libGDX Powered Android Game Project.
  2. And Airpush Account.

Step 1. Choose SDK.-

Airpush has 2 SDKs that you will be able to use in Google Play. Standard and Bundle. Both have advantages and flaws.

Bundle.-

Supports less devices than Standard (around 15% less), but pays you for each install of your app in which the user accepts the EULA additional to Ad clicks.

Standard.-

Support the most devices, but won’t pay you for users accepting the EULA. Only Ad clicks.

This tutorial will cover the 2 SDKs, any differences in their implementation will be noted.

What SDK do I recommend?. Well I use both, I make an app that implements the standard SDK with certain Version Number, lets say 1. And then I implement the Bundle and make that with a higher Version Number, lets say 2. Then I upload the 2 APKs in the same app in Google Play Console (they have same package name, don’t be confused). Then Google Play automatically will install the Bundle version if supported or the Standard if not supported. The reason Bundle support less devices is because required permissions.

Step 2. Create App in Airpush Dashboard.-

Log in to your Airpush Account, you will be taken to the Dashboard. Click the Add Application button left of the screen.

Airpush in libGDX - 图1

Fill all the fields in the first tab and Click Continue. Don’t worry if you haven’t uploaded your app to Google Play yet, just put your package name there and it will be automatically checked when its up. But try not to take more than 48 hours to publish it.

Airpush in libGDX - 图2

In the next tab, uncheck Push Notification ads, and Icon Ads, you don’t need those and also they don’t complain with Google Play policies. You will be using Standard or Bundle SDK or both, and they don’t use them. Check SmartWall Ads, and Banner Ads. Also you can check “Exclude dating campaigns” if you don’t want your users to see sexually suggestive ads (I always check it).

Airpush in libGDX - 图3

Back in the Dashboard save your API Key and Your App ID. You will need these later.

Airpush in libGDX - 图4

Step 3. Add Google Play Services Library.-

You must import Google Play Services Library and link it to your Android libGDX Project.

First of all, install/update it. Open Android SDK Manager and install Google Play Services under Extras.

Airpush in libGDX - 图5

Now go to your Android SDK Installation ‘Folder -> extras -> google -> google_play_services -> libproject’ and copy “google-play-services_lib” folder to your workspace.

Now, import the project like this in eclipse: ‘File -> import… -> Android -> Existing Android Code Into Workspace’. You will end up with something like this:

Airpush in libGDX - 图6

Right click the Android Project, go to ‘Properties -> Android’ and scroll down. Click the “Add…” button and choose the services project. Will end up like this:

Airpush in libGDX - 图7

Step 4. Adding the Airpush SDK.-

Go to the Airpush Dashboard and download your SDK. Standard or Bundle. Now this is where gets tricky, every developer has a different SDK name and also SDK packages. So you must be aware you have to change these values when you are implementing them.

Unzip the rar. And copy “mraid_attrs.xml” to your Android Project ‘res- > values’ folder. And copy the .jar file to your Android Project ‘libs’ folder.

Right click your Android Project and choose ‘Properties -> Java Build Path’, go to ‘Libraries’ tab. And click ‘Add JARs’. Choose the ‘android project -> libs -> airpushsdk.jar’ (remember it has a different name for each developer). Now also add the Goggle Play Services jar, because even if its added as a library, android won’t export the jar. click ‘Add JARs’ again, go to ‘services project -> libs -> google-play-services.jar’.

Switch to the ‘Order and Export’ tab and be sure to check both new jars. Will end up like this:

Airpush in libGDX - 图8

Click Ok to finish.

Step 5. Editing the Android Manifest.-

Ad the following to your Android Manifest. Be aware this is different for Standard and Bundle SDKs.-

Standard.-

Permissions.-

  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  3. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Activity declarations.-

  1. <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  2. <meta-data android:name="com.<sdkpackage>.APPID" android:value="<Your appId>" />
  3. <meta-data android:name="com.<sdkpackage>.APIKEY" android:value="android*<Your ApiKey>"/>
  4. <activity android:exported="false" android:name="com.<sdkpackage>.MainActivity"
  5. android:configChanges="orientation|screenSize"
  6. android:theme="@android:style/Theme.Translucent" />
  7. <activity android:name="com.<sdkpackage>.BrowserActivity"
  8. android:configChanges="orientation|screenSize" />
  9. <activity android:name="com.<sdkpackage>.VDActivity"
  10. android:configChanges="orientation|screenSize" android:screenOrientation="landscape"
  11. android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
  12. </activity>

Bundle.-

Permissions.-

  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  3. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  4. <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
  5. <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  6. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  7. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  8. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  9. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

(see why it supports less devices).

Activity declarations.-

  1. <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  2. <meta-data android:name="com.<sdkpackage>.APPID" android:value="<Your appId>" />
  3. <meta-data android:name="com.<sdkpackage>.APIKEY" android:value="android*<Your ApiKey>"/>
  4. <activity android:exported="false" android:name="com.<sdkpackage>.AdActivity"
  5. android:configChanges="orientation|screenSize"
  6. android:theme="@android:style/Theme.Translucent" />
  7. <activity android:name="com.<sdkpackage>.BrowserActivity"
  8. android:configChanges="orientation|screenSize" />
  9. <activity android:name="com.<sdkpackage>.VActivity"
  10. android:configChanges="orientation|screenSize" android:screenOrientation="landscape"
  11. android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
  12. </activity>
  13. <service android:name="com.<sdkpackage>.LService" android:exported="false"></service>
  14. <receiver android:name="com.<sdkpackage>.BootReceiver" android:exported="false">
  15. <intent-filter>
  16. <action android:name="android.intent.action.BOOT_COMPLETED" />
  17. </intent-filter>
  18. </receiver>

Don’t forget to change your <sdkpackage>, <Your ApiKey> and <Your appId> for your unique values. Also, your min sdk version must be set to 9 or higer.

  1. <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19"/>

Step 6. Adding dem Codes.-

First make an Interface in your core project called “ActionResolver”. And add the following 2 methods.

  1. public interface ActionResolver {
  2. public void showAds(boolean show);
  3. public void startSmartWallAd();
  4. }

Make your ApplicationListener receive an ActionResolver when created as a parameter. Like this:

  1. public class TheMonsterFree extends Game{
  2. public ActionResolver ar;
  3. public TheMonsterFree(ActionResolver ar){
  4. this.ar = ar;
  5. }

You will notice errors in your platform projects, because they aren’t sending an Action Resolver already. Make the Desktop and the HTML project send it. Like this:

Desktop.-

  1. public class Main {
  2. public static void main(String[] args) {
  3. LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
  4. cfg.title = "TheMonsterFree";
  5. cfg.width = 480;
  6. cfg.height = 320;
  7. new LwjglApplication(new TheMonsterFree(new ActionResolver(){
  8. @Override public void startSmartWallAd(){}
  9. @Override public void showAds(boolean show){}
  10. }), cfg);
  11. }
  12. }

HTML.-

  1. public class GwtLauncher extends GwtApplication implements ActionResolver{
  2. @Override
  3. public GwtApplicationConfiguration getConfig(){
  4. GwtApplicationConfiguration cfg = new GwtApplicationConfiguration(480, 320);
  5. return cfg;
  6. }
  7. @Override
  8. public ApplicationListener getApplicationListener(){
  9. return new TheMonsterFree(this);
  10. }
  11. @Override public void showAds(boolean show){}
  12. @Override public void startSmartWallAd(){}
  13. }

They have an empty implementation as Airpush only supports ads in Android. Add this to your Android MainActivity.java:

Android.-

  1. public class MainActivity extends AndroidApplication implements ActionResolver{
  2. @Override
  3. public void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);
  5. AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
  6. cfg.useWakelock = true;
  7. cfg.useAccelerometer = false;
  8. cfg.useCompass = false;
  9. if(ma==null) ma=new Prm(this, adCallbackListener, false);
  10. RelativeLayout layout = new RelativeLayout(this);
  11. requestWindowFeature(Window.FEATURE_NO_TITLE);
  12. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  13. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  14. View gameView = initializeForView(new TheMonsterFree(this), cfg);
  15. RelativeLayout.LayoutParams adParams = new
  16. RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  17. adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
  18. adParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
  19. adView = new AdView(this, AdView.BANNER_TYPE_IN_APP_AD, AdView.PLACEMENT_TYPE_INTERSTITIAL, false, false,
  20. AdView.ANIMATION_TYPE_LEFT_TO_RIGHT);
  21. adView.setAdListener(adlistener);
  22. layout.addView(gameView);
  23. layout.addView(adView);
  24. setContentView(layout);
  25. }
  26. AdListener adCallbackListener=new AdListener(){
  27. @Override
  28. public void onSDKIntegrationError(String message){
  29. //Here you will receive message from SDK if it detects any integration issue.
  30. Log.w("Airpush", "onSDKIntegrationError() "+message);
  31. }
  32. public void onSmartWallAdShowing(){
  33. // This will be called by SDK when it's showing any of the SmartWall ad.
  34. Log.w("Airpush", "onSmartWallAdShowing()");
  35. }
  36. @Override
  37. public void onSmartWallAdClosed(){
  38. // This will be called by SDK when the SmartWall ad is closed.
  39. Log.w("Airpush", "onSmartWallAdClosed()");
  40. }
  41. @Override
  42. public void onAdError(String message){
  43. //This will get called if any error occurred during ad serving.
  44. Log.w("Airpush", "onAdError() "+message);
  45. }
  46. @Override
  47. public void onAdCached(AdType arg0){
  48. //This will get called when an ad is cached.
  49. Log.w("Airpush", "onAdCached() "+arg0.toString());
  50. }
  51. @Override
  52. public void noAdAvailableListener(){
  53. //this will get called when ad is not available
  54. Log.w("Airpush", "noAdAvailableListener()");
  55. }
  56. };
  57. AdListener.BannerAdListener adlistener = new AdListener.BannerAdListener(){
  58. @Override
  59. public void onAdClickListener(){
  60. //This will get called when ad is clicked.
  61. Log.w("Airpush", "onAdClickListener()");
  62. }
  63. @Override
  64. public void onAdLoadedListener(){
  65. //This will get called when an ad has loaded.
  66. Log.w("Airpush", "onAdLoadedListener()");
  67. }
  68. @Override
  69. public void onAdLoadingListener(){
  70. //This will get called when a rich media ad is loading.
  71. Log.w("Airpush", "onAdLoadingListener()");
  72. }
  73. @Override
  74. public void onAdExpandedListner(){
  75. //This will get called when an ad is showing on a user's screen. This may cover the whole UI.
  76. Log.w("Airpush", "onAdExpandedListner()");
  77. }
  78. @Override
  79. public void onCloseListener(){
  80. //This will get called when an ad is closing/resizing from an expanded state.
  81. Log.w("Airpush", "onCloseListener()");
  82. }
  83. @Override
  84. public void onErrorListener(String message){
  85. //This will get called when any error has occurred. This will also get called if the SDK notices any integration mistakes.
  86. Log.w("Airpush", message);
  87. }
  88. @Override
  89. public void noAdAvailableListener(){
  90. //this will get called when ad is not available
  91. Log.w("Airpush", "noAdAvailableListener()");
  92. }
  93. };
  94. private final int SHOW_ADS = 1;
  95. private final int HIDE_ADS = 0;
  96. Prm ma = null;
  97. AdView adView = null;
  98. protected Handler handler = new Handler(Looper.getMainLooper()){
  99. @Override
  100. public void handleMessage(Message msg) {
  101. switch (msg.what) {
  102. case SHOW_ADS: {
  103. if (adView != null) adView.setVisibility(View.VISIBLE);
  104. break;
  105. }
  106. case HIDE_ADS: {
  107. if (adView != null) adView.setVisibility(View.GONE);
  108. break;
  109. }
  110. }
  111. }
  112. };
  113. @Override
  114. public void showAds(boolean show){
  115. handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
  116. }
  117. @Override
  118. public void startSmartWallAd(){
  119. if (ma!=null) ma.runAppWall();
  120. }
  121. }

For the SmartWall ads you have several options:

  • Overlay Dialog Ad: ma.runOverlayAd();
  • AppWall Ad: ma.runAppWall();
  • Rich Media Interstitial Ad: ma.displayRichMediaInterstitialAd();
  • Video Ad: ma.runVideoAd();
  • And Landing Page Ad: ma.callLandingPageAd(); only for Bundle.

And you can even do ma.callSmartWallAd(); and let the server decide for you. I recommend using AppWall as thats the one with better results.

Btw that above is for Standard SDK. If you are using Bundle you have to make these changes:

  1. Replace ma.runAppWall(); for ma.callAppWall();
  2. Replace AdListener.BannerAdListener adlistener = new AdListener.BannerAdListener(){ for AdListener.MraidAdListener adlistener = new AdListener.MraidAdListener(){
  3. Replace all Prm for MA

Do Ctrl+ Shift + O for the correct imports to update.

Thats it, now inside your core project you can show Banner ads like this:

  1. ar.showAds(true);

Hide Banner ads like this:

  1. ar.showAds(false);

And Call SmartWall ads like this:

  1. ar.startSmartWallAd();