Summary
Introduction
Just for a reference, the latest documentation is available on Smaato official .
You will need:
- A libGDX Android Game Project
- Your Smaato account
Configuration
Add the following repository setup to your project’s main file:
Set the compile options to Java 8 and minSdkVersion to at least version 16, in android module build.gradle
file:
android {
defaultConfig {
minSdkVersion 16
...
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Add required dependencies to your application module build.gradle
file under project(“:android”) —> dependencies.
implementation 'com.smaato.android.sdk:smaato-sdk:21.5.3
-keep public class com.smaato.sdk.** { *; }
-keep public interface com.smaato.sdk.** { *; }
Add the following permissions to application AndroidManifest.xml file:
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:
<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:
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:3.5.4'
}
}
The gradle-wrapper.properties:
Recap
LibGDX Android game is created as a . 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:
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
layout.addView(createGameView());
createAdView(layout);
setContentView(layout);
}
private View createGameView() {
View gameView = initializeForView(new MyGame(this), new AndroidApplicationConfiguration());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
gameView.setLayoutParams(params);
return gameView;
}
}
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.
public class AndroidLauncher extends AndroidApplication {
private static final String PUBLISHER_ID = "1100042525";
private static final String BANNER_AD_SPACE_ID = "130635694";
private BannerView smaatoBanner;
@Override
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
layout.addView(createGameView());
createAdView(layout);
}
private void createAdView(RelativeLayout layout) {
SmaatoSdk.init(getApplication(), PUBLISHER_ID);
createSmaatoBanner();
layout.addView(smaatoBanner);
}
private void createSmaatoBanner() {
smaatoBanner = new BannerView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
smaatoBanner.setLayoutParams(params);
smaatoBanner.setBackgroundColor(Color.TRANSPARENT);
smaatoBanner.setVisibility(View.INVISIBLE);
}
@Override
public void onDestroy() {
if (smaatoBanner != null) smaatoBanner.destroy();
super.onDestroy();
}
public void showBannerAds() {
runOnUiThread(() -> {
smaatoBanner.setVisibility(View.VISIBLE);
smaatoBanner.setEnabled(true);
smaatoBanner.loadAd(BANNER_AD_SPACE_ID, BannerAdSize.XX_LARGE_320x50);
});
}
public void hideBannerAds() {
runOnUiThread(() -> {
smaatoBanner.setVisibility(View.GONE);
smaatoBanner.setEnabled(false);
});
}
}
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 lifecycleBannerView
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 lifecyclePUBLISHER_ID
andBANNER_AD_SPACE_ID
can be used during testing. Please, refer to 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.
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.
public class AndroidLauncher extends AndroidApplication {
private static final String PUBLISHER_ID = "1100042525";
private static final String REWARDED_INTERSTITIAL_AD_SPACE_ID = "130626428";
private RewardedInterstitialAd smaatoRewardedInterstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
RelativeLayout layout = new RelativeLayout(this);
layout.addView(createGameView());
createAd();
setContentView(layout);
}
private void createAd() {
SmaatoSdk.init(getApplication(), PUBLISHER_ID);
requestSmaatoRewardedInterstitialAd();
}
public void showRewarded() {
runOnUiThread(() -> {
if (smaatoRewardedInterstitial == null) {
requestSmaatoRewardedInterstitialAd();
return;
}
smaatoRewardedInterstitial.showAd();
requestSmaatoRewardedInterstitialAd();
});
}
private void requestSmaatoRewardedInterstitialAd() {
RewardedInterstitial.loadAd(REWARDED_INTERSTITIAL_AD_SPACE_ID, new EventListener() {
@Override
public void onAdLoaded(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
smaatoRewardedInterstitial = rewardedInterstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull RewardedRequestError rewardedRequestError) {
}
@Override
public void onAdError(@NonNull RewardedInterstitialAd rewardedInterstitialAd, @NonNull RewardedError rewardedError) {
}
@Override
public void onAdClosed(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
}
@Override
public void onAdClicked(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
}
@Override
public void onAdStarted(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
}
@Override
public void onAdReward(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
}
@Override
public void onAdTTLExpired(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
}
});
}
}
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
Test Ads
Don’t forget to change the values to the production ones before the release!