Initialize the MoPub SDK for Android

After you have integrated the MoPub SDK and created an ad unit, you must call MoPub.initializeSdk() before you send any ad requests. Note: Starting with the 5.2.0 SDK release, requesting ads without and prior to SDK initialization completion is a no-op.

You will need to call the MoPub.initializeSdk() with any valid ad unit once per app’s lifecycle, typically on app launch. Initialization is required for several functions, including:

v5.0.0 to v5.4.1:

  • GDPR consent mechanisms
  • Rewarded video pre-initialization

v5.5.0 and above:

  • GDPR consent mechanisms
  • Mediation SDKs initialization
  • Logging
  • Advanced Bidding
  • Legitimate Interest

If you have been using MoPubRewardedVideos.initializeRewardedVideo(), its function is now included in the new initialization, so make sure to remove all the MoPubRewardedVideos.initializeRewardedVideo() calls from your code.

Initialization Instructions

To initialize the MoPub SDK:

  1. In your Activity’s onCreate() (or equivalent), instantiate an SdkConfiguration via a Builder with any valid ad unit ID from your app, (optional) your mediation settings, (optional) log level, (optional) map of configurations for networks, and (optional) a list of adapter configuration class names to initialize.
  2. Call MoPub.initializeSdk() and pass in a Context (we recommend the Activity type), the SdkConfiguration object, and an SdkInitializationListener object.

Network SDK initialization

SDK initialization will automatically attempt to initialize all known networks. However, publishers will be given the option to specify additional networks via the SDK configuration object as shown in the example below. .withMediationSettings API enable you to pass in third-party network specific settings. Note you can use .withMediatedNetworkConfiguration() to pass network configurations required for pre-initialization. Otherwise expect your first attempt to pre-initialize mediated networks to fail because no network data is cached at this point. All your subsequent attempts to pre-initialize should succeed for the duration of the app’s life.

Logging Information

The MoPub 5.5.0 SDK enhances the logging experience to include more detailed information such as adapter version, SDK version and ad life cycle events and operations. You can enable these debug logs using the API withLogLevel() during initialization.

Legitimate Interest

The MoPub SDK 5.5.0 now enables mediated networks to collect your user’s personal data based on legitimate interest basis when publishers opt-in. For more details read here. You can enable the legitimate interest flag using the API withLegitimateInterestAllowed() during initialization.

Initializing custom ad networks not listed on Supported Mediation Partners

In order to initialize the custom network SDKs, follow the steps below:

  • Subclass BaseAdapterConfiguration. For instructions on how to extend that abstract class, reference this guide.
  • Pass your adapter configuration custom event class and configurations via the .withMediatedNetworkConfiguration(String, Map<String, String>) to the SdkConfiguration.Builder instance. This adds mediated network configuration keyed by the AdapterConfiguration class.

Example

// configurations required to initialize
Map<String, String> mediatedNetworkConfiguration1 = new HashMap<>();
mediatedNetworkConfiguration1.put("<custom-adapter-class-data-key>", "<custom-adapter-class-data-value>");
Map<String, String> mediatedNetworkConfiguration2 = new HashMap<>();
mediatedNetworkConfiguration2.put("<custom-adapter-class-data-key>", "<custom-adapter-class-data-value>");

SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder("AD_UNIT_ID")
                                                    .withMediationSettings("MEDIATION_SETTINGS")
                                                    .withAdditionalNetworks(CustomAdapterConfiguration.class.toString())
                                                    .withMediatedNetworkConfiguration(CustomAdapterConfiguration1.class.toString(), mediatedNetworkConfiguration)
                                                    .withMediatedNetworkConfiguration(CustomAdapterConfiguration2.class.toString(), mediatedNetworkConfiguration)
                                                    .withMediatedNetworkConfiguration(CustomAdapterConfiguration1.class.toString(), mediatedNetworkConfiguration1)
                                                    .withMediatedNetworkConfiguration(CustomAdapterConfiguration2.class.toString(), mediatedNetworkConfiguration2)
                                                    .withLogLevel(LogLevel.Debug)
                                                    .withLegitimateInterestAllowed(false)
                                                    .build();

MoPub.initializeSdk(this, sdkConfiguration, initSdkListener());

private SdkInitializationListener initSdkListener() {
   return new SdkInitializationListener() {
       @Override
       public void onInitializationFinished() {
           /* MoPub SDK initialized.
           Check if you should show the consent dialog here, and make your ad requests. */
       }
   };
}

Last updated March 07, 2019

TWITTER, MOPUB, and the Bird logo are trademarks of Twitter, Inc. or its affiliates. All third party logos and trademarks included are the property of their respective owners.

© 2019 MoPub (a division of Twitter, Inc.)