Manual Download for MAX Mediation Networks
To receive release updates, subscribe to the AppLovin-MAX-SDK-Android GitHub repository.
Download the Latest Android SDK
Add the SDK to the Project
Unzip the downloaded file, then drag and drop the aar
file to the libs
folder in your project.
(If your project does not have a libs
folder, you can create one inside the app
folder.)
Gradle
Add the following to your build.gradle
file:
repositories { google() mavenCentral() flatDir { dirs 'libs' } ⋮}dependencies { implementation 'com.applovin:applovin-sdk:«x.y.z»@aar' ⋮}
repositories { google() mavenCentral() flatDir { dirs("libs") } ⋮}dependencies { implementation("com.applovin:applovin-sdk:«x.y.z»@aar") ⋮}
Add the SDK Key
Add the following line into your AndroidManifest.xml
.
This needs to go inside the application
tag:
<meta-data android:name="applovin.sdk.key" android:value="«your-SDK-key»"/>
You can find your SDK key in the Account > General > Keys section of the AppLovin dashboard.
Enable Ad Review
To enable the MAX Ad Review service, add the following to your build.gradle
files:
Additions to Root-Level build.gradle
File
buildscript { repositories { maven { url 'https://artifacts.applovin.com/android' } } dependencies { classpath "com.applovin.quality:AppLovinQualityServiceGradlePlugin:+" }}
buildscript { repositories { maven { url = uri("https://artifacts.applovin.com/android") } } dependencies { classpath ("com.applovin.quality:AppLovinQualityServiceGradlePlugin:+") }}
Additions to App-Level build.gradle
File
apply plugin: 'applovin-quality-service'applovin { apiKey "«your-ad-review-key»"}
plugins { id("applovin-quality-service")}applovin { apiKey = "«your-ad-review-key»"}
You can find your Ad Review Key in the Account > General > Keys section of the AppLovin dashboard.
Initialize the SDK
Create the SDK Initialization Configuration
Before you initialize the SDK, create an initialization configuration object for the SDK.
This object allows you to configure the properties that the SDK will initialize with.
These initialization properties are immutable, with the exception of AppLovinSdkSettings
which contains mutable properties that can be changed during the lifetime of the app.
// Create the initialization configurationAppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder( "«SDK-key»" ) .setMediationProvider( AppLovinMediationProvider.MAX )// Perform any additional configuration/setting changes .build();
// Create the initialization configurationval initConfig = AppLovinSdkInitializationConfiguration.builder("«SDK-key»") .setMediationProvider(AppLovinMediationProvider.MAX)// Perform any additional configuration/setting changes .build()
You can find your SDK key in the Account > General > Keys section of the AppLovin dashboard.
Initialize the SDK
Initialize the AppLovin SDK with the initialization configuration object as early as possible (for example, in the onCreate()
of the launch activity or Application class).
This maximizes how much time the SDK has to cache mediated networks’ ads, which results in a better user experience.
public class MainActivity extends Activity{ protected void onCreate(Bundle savedInstanceState) { // Create the initialization configuration AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder( "«SDK-key»" ) .setMediationProvider( AppLovinMediationProvider.MAX ) .build();
// Initialize the SDK with the configuration AppLovinSdk.getInstance( this ).initialize( initConfig, new AppLovinSdk.SdkInitializationListener() { @Override public void onSdkInitialized(final AppLovinSdkConfiguration sdkConfig) { // Start loading ads } } ); }}
class MainActivity : Activity(){ override fun onCreate(savedInstanceState: Bundle?) { // Create the initialization configuration val initConfig = AppLovinSdkInitializationConfiguration.builder("«SDK-key»") .setMediationProvider(AppLovinMediationProvider.MAX) .build()
// Initialize the SDK with the configuration AppLovinSdk.getInstance(this).initialize(initConfig) { sdkConfig -> // Start loading ads } }}
Example
Below is a sample integration.
// Create the initialization configurationAppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder( "«SDK-key»" ) .setMediationProvider( AppLovinMediationProvider.MAX ) .setSegmentCollection( MaxSegmentCollection.builder() .addSegment( new MaxSegment( 849, Arrays.asList( 1, 3 ) ) ) .build() ) .build();
// Configure the SDK settings if needed before or after SDK initialization.val settings = AppLovinSdk.getInstance( this ).getSettings();settings.setUserIdentifier( "«user-ID»" );settings.setExtraParameter( "uid2_token", "«token-value»" );settings.getTermsAndPrivacyPolicyFlowSettings().setEnabled( true );settings.getTermsAndPrivacyPolicyFlowSettings().setPrivacyPolicyUri( Uri.parse( "«https://your-company-name.com/privacy-policy»" ) );settings.getTermsAndPrivacyPolicyFlowSettings().setTermsOfServiceUri( Uri.parse( "«https://your-company-name.com/terms-of-service»" ) );
// Initialize the SDK with the configurationAppLovinSdk.getInstance( this ).initialize( initConfig, new AppLovinSdk.SdkInitializationListener(){ @Override public void onSdkInitialized(final AppLovinSdkConfiguration sdkConfig) { // Start loading ads }} );
// Create the initialization configurationval initConfig = AppLovinSdkInitializationConfiguration.builder("«SDK-key»") .setMediationProvider(AppLovinMediationProvider.MAX) .setSegmentCollection(MaxSegmentCollection.builder() .addSegment(MaxSegment(849, listOf(1, 3))) .build() ) .build()
// Configure the SDK settings if needed before or after SDK initialization.val settings = AppLovinSdk.getInstance(this).settingssettings.userIdentifier = "«user-ID»"settings.setExtraParameter("uid2_token", "«token-value»")settings.termsAndPrivacyPolicyFlowSettings.apply { isEnabled = true privacyPolicyUri = Uri.parse("«https://your-company-name.com/privacy-policy»") termsOfServiceUri = Uri.parse("«https://your-company-name.com/terms-of-service»")}
// Initialize the SDK with the configurationAppLovinSdk.getInstance(this).initialize(initConfig) { sdkConfig -> // Start loading ads}
Interstitial Ads
Loading an Interstitial Ad
To load an interstitial ad, instantiate a MaxInterstitialAd
object with your ad unit and call loadAd()
.
Implement MaxAdListener
so you can be notified when your ad is ready and of other ad-related events.
public class ExampleActivity extends Activity implements MaxAdListener{ private MaxInterstitialAd interstitialAd; private int retryAttempt;
void createInterstitialAd() { interstitialAd = new MaxInterstitialAd( "«ad-unit-ID»" ); interstitialAd.setListener( this );
// Load the first ad interstitialAd.loadAd(); }
// MAX Ad Listener @Override public void onAdLoaded(final MaxAd maxAd) { // Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
// Reset retry attempt retryAttempt = 0; }
@Override public void onAdLoadFailed(final String adUnitId, final MaxError error) { // Interstitial ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++; long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) );
new Handler().postDelayed( new Runnable() { @Override public void run() { interstitialAd.loadAd(); } }, delayMillis ); }
@Override public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) { // Interstitial ad failed to display. AppLovin recommends that you load the next ad interstitialAd.loadAd(); }
@Override public void onAdDisplayed(final MaxAd maxAd) {}
@Override public void onAdClicked(final MaxAd maxAd) {}
@Override public void onAdHidden(final MaxAd maxAd) { // Interstitial ad is hidden. Pre-load the next ad interstitialAd.loadAd(); }}
class ExampleActivity : Activity(), MaxAdListener{ private lateinit var interstitialAd: MaxInterstitialAd private var retryAttempt = 0.0
fun createInterstitialAd() { interstitialAd = MaxInterstitialAd( "«ad-unit-ID»", this ) interstitialAd.setListener( this )
// Load the first ad interstitialAd.loadAd() }
// MAX Ad Listener override fun onAdLoaded(maxAd: MaxAd) { // Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
// Reset retry attempt retryAttempt = 0.0 }
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) { // Interstitial ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++ val delayMillis = TimeUnit.SECONDS.toMillis( Math.pow( 2.0, Math.min( 6.0, retryAttempt ) ).toLong() )
Handler().postDelayed( { interstitialAd.loadAd() }, delayMillis ) }
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) { // Interstitial ad failed to display. AppLovin recommends that you load the next ad interstitialAd.loadAd() }
override fun onAdDisplayed(maxAd: MaxAd) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdHidden(maxAd: MaxAd) { // Interstitial ad is hidden. Pre-load the next ad interstitialAd.loadAd() }}
Showing an Interstitial Ad
To show an interstitial ad, call showAd()
on the MaxInterstitialAd
object created above.
if ( interstitialAd.isReady() ){ interstitialAd.showAd();}
if ( interstitialAd.isReady ){ interstitialAd.showAd()}
Rewarded Ads
Loading a Rewarded Ad
To load a rewarded ad, retrieve a MaxRewardedAd
object with your rewarded ad unit and call loadAd()
on it.
Implement MaxRewardedAdListener
so you can be notified when your ad is ready and of other ad-related events.
public class ExampleActivity extends Activity implements MaxRewardedAdListener{ private MaxRewardedAd rewardedAd; private int retryAttempt;
void createRewardedAd() { rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»" ); rewardedAd.setListener( this );
rewardedAd.loadAd(); }
// MAX Ad Listener @Override public void onAdLoaded(final MaxAd maxAd) { // Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt retryAttempt = 0; }
@Override public void onAdLoadFailed(final String adUnitId, final int errorCode) { // Rewarded ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++; long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) );
new Handler().postDelayed( new Runnable() { @Override public void run() { rewardedAd.loadAd(); } }, delayMillis ); }
@Override public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) { // Rewarded ad failed to display. AppLovin recommends that you load the next ad rewardedAd.loadAd(); }
@Override public void onAdDisplayed(final MaxAd maxAd) {}
@Override public void onAdClicked(final MaxAd maxAd) {}
@Override public void onAdHidden(final MaxAd maxAd) { // rewarded ad is hidden. Pre-load the next ad rewardedAd.loadAd(); }
@Override public void onUserRewarded(final MaxAd maxAd, final MaxReward maxReward) { // Rewarded ad was displayed and user should receive the reward }}
class ExampleActivity : Activity(), MaxRewardedAdListener{ private lateinit var rewardedAd: MaxRewardedAd private var retryAttempt = 0.0
fun createRewardedAd() { rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»" ) rewardedAd.setListener( this )
rewardedAd.loadAd() }
// MAX Ad Listener override fun onAdLoaded(maxAd: MaxAd) { // Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt retryAttempt = 0.0 }
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) { // Rewarded ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++ val delayMillis = TimeUnit.SECONDS.toMillis( Math.pow( 2.0, Math.min( 6.0, retryAttempt ) ).toLong() )
Handler().postDelayed( { rewardedAd.loadAd() }, delayMillis ) }
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) { // Rewarded ad failed to display. AppLovin recommends that you load the next ad rewardedAd.loadAd() }
override fun onAdDisplayed(maxAd: MaxAd) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdHidden(maxAd: MaxAd) { // rewarded ad is hidden. Pre-load the next ad rewardedAd.loadAd() }
override fun onUserRewarded(maxAd: MaxAd, maxReward: MaxReward) { // Rewarded ad was displayed and user should receive the reward }}
Showing a Rewarded Ad
To show a rewarded ad, call showAd()
on the MaxRewardedAd
object you created above.
if ( rewardedAd.isReady() ){ rewardedAd.showAd();}
if ( rewardedAd.isReady() ){ rewardedAd.showAd();}
Banners & MRECs
Loading and Showing Banners & MRECs Programmatically
To load a banner ad or MREC, create a MaxAdView
object with your ad unit and call loadAd()
.
To show, add the MaxAdView
object as a subview of your view hierarchy.
Implement MaxAdViewAdListener
so you can be notified when your ad is ready and of other ad-related events.
public class ExampleActivity extends Activity implements MaxAdViewAdListener{ private MaxAdView adView;
void createBannerAd() { adView = new MaxAdView( "«ad-unit-ID»" ); adView.setListener( this );
// Stretch to the width of the screen for banners to be fully functional int width = ViewGroup.LayoutParams.MATCH_PARENT;
// Banner height on phones and tablets is 50 and 90, respectively int heightPx = getResources().getDimensionPixelSize( R.dimen.banner_height );
adView.setLayoutParams( new FrameLayout.LayoutParams( width, heightPx ) );
// Set background or background color for banners to be fully functional adView.setBackgroundColor( ... );
ViewGroup rootView = findViewById( android.R.id.content ); rootView.addView( adView );
// Load the ad adView.loadAd(); }
// MAX Ad Listener @Override public void onAdLoaded(final MaxAd maxAd) {}
@Override public void onAdLoadFailed(final String adUnitId, final int errorCode) {}
@Override public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override public void onAdClicked(final MaxAd maxAd) {}
@Override public void onAdExpanded(final MaxAd maxAd) {}
@Override public void onAdCollapsed(final MaxAd maxAd) {}
@Override public void onAdDisplayed(final MaxAd maxAd) { /* use this for impression tracking */ }
@Override public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }}
class ExampleActivity : Activity(), MaxAdViewAdListener{ private var adView: MaxAdView? = null
fun createBannerAd() { adView = MaxAdView("«ad-unit-ID»") adView?.setListener(this)
// Stretch to the width of the screen for banners to be fully functional val width = ViewGroup.LayoutParams.MATCH_PARENT
// Banner height on phones and tablets is 50 and 90, respectively val heightPx = resources.getDimensionPixelSize(R.dimen.banner_height)
adView?.layoutParams = FrameLayout.LayoutParams(width, heightPx)
// Set background or background color for banners to be fully functional adView?.setBackgroundColor(...)
val rootView = findViewById<ViewGroup>(android.R.id.content) rootView.addView(adView)
// Load the ad adView?.loadAd() }
// MAX Ad Listener override fun onAdLoaded(maxAd: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdExpanded(maxAd: MaxAd) {}
override fun onAdCollapsed(maxAd: MaxAd) {}
override fun onAdDisplayed(maxAd: MaxAd) { /* use this for impression tracking */ }
override fun onAdHidden(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }}
public class ExampleActivity extends Activity implements MaxAdViewAdListener{ private MaxAdView adView;
void createMrecAd { adView = new MaxAdView( "«ad-unit-ID»", MaxAdFormat.MREC ); adView.setListener( this );
// MREC width and height are 300 and 250 respectively, on phones and tablets int widthPx = AppLovinSdkUtils.dpToPx( this, 300 ); int heightPx = AppLovinSdkUtils.dpToPx( this, 250 );
adView.setLayoutParams( new FrameLayout.LayoutParams( widthPx, heightPx ) );
// Set background or background color for MRECs to be fully functional adView.setBackgroundColor( ... );
ViewGroup rootView = findViewById( android.R.id.content ); rootView.addView( adView );
// Load the ad adView.loadAd(); }
// MAX Ad Listener @Override public void onAdLoaded(final MaxAd maxAd) {}
@Override public void onAdLoadFailed(final String adUnitId, final int errorCode) {}
@Override public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override public void onAdClicked(final MaxAd maxAd) {}
@Override public void onAdExpanded(final MaxAd maxAd) {}
@Override public void onAdCollapsed(final MaxAd maxAd) {}
@Override public void onAdDisplayed(final MaxAd maxAd) { /* use this for impression tracking */ }
@Override public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }}
class ExampleActivity : Activity(), MaxAdViewAdListener{ private var adView: MaxAdView? = null
fun createMrecAd { adView = MaxAdView("«ad-unit-ID»", MaxAdFormat.MREC) adView?.setListener(this)
// MREC width and height are 300 and 250 respectively, on phones and tablets val widthPx = AppLovinSdkUtils.dpToPx(this, 300) val heightPx = AppLovinSdkUtils.dpToPx(this, 250)
adView?.layoutParams = FrameLayout.LayoutParams(widthPx, heightPx)
// Set background or background color for MRECs to be fully functional adView?.setBackgroundColor(...)
val rootView = findViewById<ViewGroup>(android.R.id.content) rootView.addView(adView)
// Load the ad adView?.loadAd() }
// MAX Ad Listener override fun onAdLoaded(maxAd: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdExpanded(maxAd: MaxAd) {}
override fun onAdCollapsed(maxAd: MaxAd) {}
override fun onAdDisplayed(maxAd: MaxAd) { /* use this for impression tracking */ }
override fun onAdHidden(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }}
Loading and Showing Banners or MRECs in Layout Editor
Alternatively, you can add MAX banners or MRECs to your view layout XML.
Ensure that your ads are fully functional by setting a background or background color (android:background
).
In the case of banners, stretch the width (android:layout_width
) to the width of the screen.
In the case of MRECs, set the android:adFormat
accordingly:
<com.applovin.mediation.ads.MaxAdView xmlns:maxads="http://schemas.applovin.com/android/1.0" maxads:adUnitId="«ad-unit-ID»" android:background="@color/banner_background_color" android:layout_width="match_parent" android:layout_height="@dimen/banner_height" />
<com.applovin.mediation.ads.MaxAdView xmlns:maxads="http://schemas.applovin.com/android/1.0" maxads:adUnitId="«ad-unit-ID»" maxads:adFormat="MREC" android:background="@color/mrec_background_color" android:layout_width="300dp" android:layout_height="250dp" />
Declare the base banner height of 50dp in res/values/attrs.xml
:
<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="banner_height">50dp</dimen></resources>
Declare the tablet banner height of 90dp in res/values-sw600dp/attrs.xml
:
<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="banner_height">90dp</dimen></resources>
To hide a banner or MREC ad, call the following:
adView.setVisibility( View.GONE );adView.stopAutoRefresh();
adView.visibility = View.GONEadView.stopAutoRefresh()
To show a banner or MREC ad, call the following:
adView.setVisibility( View.VISIBLE );adView.startAutoRefresh();
adView.visibility = View.VISIBLEadView.startAutoRefresh()
Preparing Mediated Networks
Select the ad networks to integrate. Then follow the specific instructions.