Summary

Introduction

This article will show you how to add Smaato ads to your libGDX Android projects. You will be able to monetize your games with Banner, Interstitial and Rewarded Ads. This tutorial implies that you are already familiar with libGDX basics and how Android Views are handled. But there will be a short recap.

Just for a reference, the latest documentation is available on Smaato official website.

You will need:

  1. A libGDX Android Game Project
  2. Your Smaato account

Configuration

Add the following repository setup to your project’s main build.gradle file:

  1. buildscript {
  2. repositories {
  3. mavenCentral()
  4. google()
  5. jcenter()
  6. maven { url "https://s3.amazonaws.com/smaato-sdk-releases/" }
  7. }
  8. }
  9. ...
  10. allprojects {
  11. repositories {
  12. google()
  13. jcenter()
  14. maven {
  15. url "https://s3.amazonaws.com/smaato-sdk-releases/"
  16. }
  17. }
  18. }

Set the compile options to Java 8 and minSdkVersion to at least version 16, in android module build.gradle file:

  1. android {
  2. defaultConfig {
  3. minSdkVersion 16
  4. ...
  5. }
  6. compileOptions {
  7. sourceCompatibility JavaVersion.VERSION_1_8
  8. targetCompatibility JavaVersion.VERSION_1_8
  9. }
  10. }

Add required dependencies to your application module build.gradle file under project(“:android”) —> dependencies.

implementation 'com.smaato.android.sdk:smaato-sdk:21.5.3

If you’re using Proguard in your project, please add the following lines to your Proguard config file:

  1. -keep public class com.smaato.sdk.** { *; }
  2. -keep public interface com.smaato.sdk.** { *; }

Add the following permissions to application AndroidManifest.xml file:

  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.ACCESS_COARSE_LOCATION" />

If your application targets Android 5.0 (API level 21) or higher, then you need to add the following line to your application AndroidManifest.xml file:

  1. <uses-feature android:name="android.hardware.location.network" />

The latest version of Smaato NextGenSDK has been validated against com.android.tools.build:gradle:3.5.4 and gradle-wrapper gradle-5.6.3-bin

Main build.gradle:

  1. buildscript {
  2. ...
  3. dependencies {
  4. classpath 'com.android.tools.build:gradle:3.5.4'
  5. }
  6. }

The gradle-wrapper.properties:

  1. distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.3-bin.zip
  2. distributionBase=GRADLE_USER_HOME
  3. distributionPath=wrapper/dists
  4. zipStorePath=wrapper/dists
  5. zipStoreBase=GRADLE_USER_HOME

Recap

LibGDX Android game is created as a View. In order to add ads that are constantly displayed (like Banners) another view holding that should be used. In other words, a Relative Layout having both views should be created. You can see that in the following code snippet:

  1. public class AndroidLauncher extends AndroidApplication {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. RelativeLayout layout = new RelativeLayout(this);
  6. layout.setLayoutParams(new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
  7. layout.addView(createGameView());
  8. createAdView(layout);
  9. setContentView(layout);
  10. }
  11. private View createGameView() {
  12. View gameView = initializeForView(new MyGame(this), new AndroidApplicationConfiguration());
  13. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
  14. params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
  15. params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
  16. gameView.setLayoutParams(params);
  17. return gameView;
  18. }
  19. }

The only missing piece here is the createAdView method that will be created in the next section.

Banner

In order to add Banner ads we will use com.smaato.sdk.banner.widget.BannerView. As mentioned previously, the Banner view will reside in the same Relative Layout as the Game view.

  1. public class AndroidLauncher extends AndroidApplication {
  2. private static final String PUBLISHER_ID = "1100042525";
  3. private static final String BANNER_AD_SPACE_ID = "130635694";
  4. private BannerView smaatoBanner;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. RelativeLayout layout = new RelativeLayout(this);
  9. layout.setLayoutParams(new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
  10. layout.addView(createGameView());
  11. createAdView(layout);
  12. setContentView(layout);
  13. }
  14. private void createAdView(RelativeLayout layout) {
  15. SmaatoSdk.init(getApplication(), PUBLISHER_ID);
  16. createSmaatoBanner();
  17. layout.addView(smaatoBanner);
  18. }
  19. private void createSmaatoBanner() {
  20. smaatoBanner = new BannerView(this);
  21. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
  22. params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
  23. params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
  24. smaatoBanner.setLayoutParams(params);
  25. smaatoBanner.setBackgroundColor(Color.TRANSPARENT);
  26. smaatoBanner.setVisibility(View.INVISIBLE);
  27. }
  28. @Override
  29. public void onDestroy() {
  30. if (smaatoBanner != null) smaatoBanner.destroy();
  31. super.onDestroy();
  32. }
  33. public void showBannerAds() {
  34. runOnUiThread(() -> {
  35. smaatoBanner.setVisibility(View.VISIBLE);
  36. smaatoBanner.setEnabled(true);
  37. smaatoBanner.loadAd(BANNER_AD_SPACE_ID, BannerAdSize.XX_LARGE_320x50);
  38. });
  39. }
  40. public void hideBannerAds() {
  41. runOnUiThread(() -> {
  42. smaatoBanner.setVisibility(View.GONE);
  43. smaatoBanner.setEnabled(false);
  44. });
  45. }
  46. }

Let me walk you through this code snippet:

  • BannerView smaatoBanner is kept as an instance variable, so an ad can be shown and hidden depending on the game lifecycle
  • BannerView should be always destroyed in order to release resources properly
  • The banner will be placed according to RelativeLayout.LayoutParams rules
  • showBannerAds/hideBannerAds are supposed to be called as per the game lifecycle
  • PUBLISHER_ID and BANNER_AD_SPACE_ID can be used during testing. Please, refer to Test Ads section for all available test ads configurations.

Interstitial

Unlike Banners, Interstitial ads are not constantly displayed on the screen. Such ads should be only shown at natural transition points in the flow of an app, like finishing/failing of a level or switching between the screens. Hence there is no need to have an additional view for Interstitial ads as they will typically occupy the whole screen and will be destroyed upon closing.

  1. public class AndroidLauncher extends AndroidApplication {
  2. private static final String PUBLISHER_ID = "1100042525";
  3. private static final String INTERSTITIAL_AD_SPACE_ID = "130626426";
  4. private InterstitialAd smaatoInterstitial;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. RelativeLayout layout = new RelativeLayout(this);
  9. layout.setLayoutParams(new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
  10. layout.addView(createGameView());
  11. createAd();
  12. setContentView(layout);
  13. }
  14. private void createAd() {
  15. SmaatoSdk.init(getApplication(), PUBLISHER_ID);
  16. requestSmaatoInterstitialAd();
  17. }
  18. public void showInterstitial() {
  19. runOnUiThread(() -> {
  20. if (smaatoInterstitial == null) {
  21. requestSmaatoInterstitialAd();
  22. return;
  23. }
  24. smaatoInterstitial.showAd(AndroidLauncher.this);
  25. requestSmaatoInterstitialAd();
  26. });
  27. }
  28. private void requestSmaatoInterstitialAd() {
  29. Interstitial.loadAd(INTERSTITIAL_AD_SPACE_ID, new EventListener() {
  30. @Override
  31. public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
  32. smaatoInterstitial = interstitialAd;
  33. }
  34. @Override
  35. public void onAdFailedToLoad(@NonNull InterstitialRequestError interstitialRequestError) {
  36. log(AndroidLauncher.class.getName(), "Smaato Interstitial failed to load; error: " + interstitialRequestError.getInterstitialError());
  37. }
  38. @Override
  39. public void onAdError(@NonNull InterstitialAd interstitialAd, @NonNull InterstitialError interstitialError) {
  40. }
  41. @Override
  42. public void onAdOpened(@NonNull InterstitialAd interstitialAd) {
  43. }
  44. @Override
  45. public void onAdClosed(@NonNull InterstitialAd interstitialAd) {
  46. }
  47. @Override
  48. public void onAdClicked(@NonNull InterstitialAd interstitialAd) {
  49. }
  50. @Override
  51. public void onAdImpression(@NonNull InterstitialAd interstitialAd) {
  52. }
  53. @Override
  54. public void onAdTTLExpired(@NonNull InterstitialAd interstitialAd) {
  55. }
  56. });
  57. }
  58. }

Let me walk you through this code snippet:

  • InterstitialAd smaatoInterstitial is kept as an instance variable, so an ad can be shown depending on the game lifecycle
  • It is recommended to call requestSmaatoInterstitialAd on a game load, in order to have an ad preloaded from the start
  • There are multiple useful callbacks of Interstitial.loadAd that can be used depending on your use case
  • showInterstitial is supposed to be called as per the game lifecycle

Rewarded

Rewarded ads are visually similar to Interstitial ads as they typically occupy the whole screen. But workflow-wise they are completely different - such ads encourage users to interact with the ad content in exchange for in-app rewards, like extra lives, free coins or energy.

  1. public class AndroidLauncher extends AndroidApplication {
  2. private static final String PUBLISHER_ID = "1100042525";
  3. private static final String REWARDED_INTERSTITIAL_AD_SPACE_ID = "130626428";
  4. private RewardedInterstitialAd smaatoRewardedInterstitial;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. RelativeLayout layout = new RelativeLayout(this);
  9. layout.setLayoutParams(new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
  10. layout.addView(createGameView());
  11. createAd();
  12. setContentView(layout);
  13. }
  14. private void createAd() {
  15. SmaatoSdk.init(getApplication(), PUBLISHER_ID);
  16. requestSmaatoRewardedInterstitialAd();
  17. }
  18. public void showRewarded() {
  19. runOnUiThread(() -> {
  20. if (smaatoRewardedInterstitial == null) {
  21. requestSmaatoRewardedInterstitialAd();
  22. return;
  23. }
  24. smaatoRewardedInterstitial.showAd();
  25. requestSmaatoRewardedInterstitialAd();
  26. });
  27. }
  28. private void requestSmaatoRewardedInterstitialAd() {
  29. RewardedInterstitial.loadAd(REWARDED_INTERSTITIAL_AD_SPACE_ID, new EventListener() {
  30. @Override
  31. public void onAdLoaded(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
  32. smaatoRewardedInterstitial = rewardedInterstitialAd;
  33. }
  34. @Override
  35. public void onAdFailedToLoad(@NonNull RewardedRequestError rewardedRequestError) {
  36. }
  37. @Override
  38. public void onAdError(@NonNull RewardedInterstitialAd rewardedInterstitialAd, @NonNull RewardedError rewardedError) {
  39. }
  40. @Override
  41. public void onAdClosed(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
  42. }
  43. @Override
  44. public void onAdClicked(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
  45. }
  46. @Override
  47. public void onAdStarted(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
  48. }
  49. @Override
  50. public void onAdReward(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
  51. }
  52. @Override
  53. public void onAdTTLExpired(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
  54. }
  55. });
  56. }
  57. }

One more time, let me walk you through this code snippet:

  • RewardedInterstitialAd smaatoRewardedInterstitial is kept as an instance variable, so an ad can be shown whenever a user initiates a rewarded ad flow in your game
  • It is recommended to call requestSmaatoRewardedInterstitialAd on a game load, in order to have an ad preloaded from the start
  • There are multiple useful callbacks of RewardedInterstitial.loadAd that you can be used depending on your use case
  • showRewarded is supposed to be called whenever a user initiates a rewarded ad flow in your game

Test Ads

Adspace IDType
130626424Rich Media
130635694Static Image
130635706MRAID
130626426Rich Media / Video
130626427Video
130626428Rewarded

Please, use Publisher Id 1100042525 with each of those adspaces.

Don’t forget to change the values to the production ones before the release!