Use these instructions to integrate the MoPub SDK with your Android app. If you are upgrading to our version 5.0.0 or higher of our SDK, refer to the SDK Initialization and GDPR sections for additional steps.

Requirements

Note: If you or other SDKs in your project depend on AndroidX, please note that MoPub is still testing AndroidX internally.

Note: We strongly recommend compiling your app against the ads dependencies from Google Play services in order to use the Android Advertising ID instead of the device ID, as required by Google. Those dependencies are com.google.android.gms:play-services-ads-identifier and com.google.android.gms:play-services-base.

Step 1. Download the MoPub Android SDK

Option 1. jCenter AAR (Recommended)

The MoPub SDK is available as an AAR via jCenter. To add the mopub-sdk dependency, open your project and update the app module’s build.gradle to have the following repositories and dependencies:

repositories{// ... other project repositoriesjcenter()// includes the MoPub SDK and AVID librarymaven{url"https://s3.amazonaws.com/moat-sdk-builds"}}// ...dependencies{// ... other project dependencies// This will get the latest available version of the MoPub SDK. // Alternatively, you may specify a specific version from the changelog to download. implementation('com.mopub:mopub-sdk:+@aar'){transitive=true}}

SDK Modularization

With the modular SDK, you can choose to include specific formats to decrease overall SDK footprint in your app. To do so, include the line for any combination of components that you want in your build.gradle file as follows:

repositories{// ... other project repositoriesjcenter()// includes the MoPub SDK and AVID librarymaven{url"https://s3.amazonaws.com/moat-sdk-builds"}}// ...dependencies{// ... other project dependencies// For bannersimplementation('com.mopub:mopub-sdk-banner:+@aar'){transitive=true}// For interstitialsimplementation('com.mopub:mopub-sdk-interstitial:+@aar'){transitive=true}// For rewarded videos. This will automatically also include interstitialsimplementation('com.mopub:mopub-sdk-rewardedvideo:+@aar'){transitive=true}// For native static (images).implementation('com.mopub:mopub-sdk-native-static:+@aar'){transitive=true}// For native video. This will automatically also include native staticimplementation('com.mopub:mopub-sdk-native-video:+@aar'){transitive=true}}

If you are integrating with MoPub SDK v5.6.0 and later, specify the sourceCompatibility and targetCompatibility as below to prevent compilation errors with ExoPlayer 2.9.5 and later:

Open your project’s build.gradle file and add jcenter as a repository and whichever ad formats you want as a dependency:

repositories{jcenter()// includes the MoPub SDK and AVID librarymaven{url"https://s3.amazonaws.com/moat-sdk-builds"}}// ...dependencies{// ... other project dependencies// For bannersimplementationproject(':mopub-sdk:mopub-sdk-banner')// For interstitialsimplementationproject(':mopub-sdk:mopub-sdk-interstitial')// For rewarded videos. This will automatically also include interstitialsimplementationproject(':mopub-sdk:mopub-sdk-rewardedvideo')// For native static (images).implementationproject(':mopub-sdk:mopub-sdk-native-static')// For native video. This will automatically also include native staticimplementationproject(':mopub-sdk:mopub-sdk-native-video')}

Adding the MoPub SDK as a Compiled .aar

To add the MoPub SDK as an .aar in your project:

Navigate to the MoPub SDK in your terminal and run the following command:

./gradlew mopub-sdk:build

This will build an AAR that bundles the mopub-sdk.

Copy the .aar to your library directory:

cp mopub-sdk/build/outputs/aar/mopub-sdk.aar $MY_LIB_DIR

where $MY_LIB_DIR is your default library directory.

Note: If adding third party network SDKs and adapters, if you receive a dex error, you may need to enable multidexing in your build.gradle file. Please see the Android documentation here.

defaulConfig{multiDexEnabletrue}

Open your project’s build.gradle file and add the following lines:

repositories{jcenter()// includes the MoPub SDK and AVID librarymaven{url"https://s3.amazonaws.com/moat-sdk-builds"}flatDir{dirs'$MY_LIB_DIR'}}// ...dependencies{implementation(name:'mopub-sdk',ext:'aar')implementation'com.mopub.volley:mopub-volley:2.1.0'}

Note: If adding third-party network SDKs and adapters, if you receive a dex error, you may need to enable multidexing in your build.gradle file. Please see the Android documentation here.

defaulConfig{multiDexEnabletrue}

Option 2. Maven Integration

The MoPub Android SDK comes with a full set of POM files. For depending on these files in Maven:

Run the following command in the mopub-sdk directory:

$ mvn clean install-DskipTests=true

The mopub-sdk POM includes dependencies on android-support-v4.aar and android-support-annotations.jar. It relies on the presence of the ANDROID_HOME environment variable. You should have this variable set to the root directory of your Android SDK installation.

On OS X & Linux this often looks like:

export ANDROID_HOME "/Users/user/android-sdk-macosx"

in your .bashrc file.

On Windows you can add:

ANDROID_HOME, C:\<installation location>\android-sdk-windows

to your environment variables.

Once you’ve installed the MoPub SDK in your local maven repository, you can depend on the project by adding this dependency declaration:

Remember that if you’re using Maven, you also need to have your Android platform libraries installed in your local Maven repository. The Maven Android SDK Deployer is one popular tool for using Maven to build Android projects.

Step 3. Update Your Android Manifest

Upate your AndroidManifest.xml in order to complete the SDK integration. Add the following permissions and activity declarations according to the bundle you are integrating.

Note:ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION are only needed if you want the device to automatically send the user’s location for targeting. WRITE_EXTERNAL_STORAGE is optional and only required for MRAID 2.0 storePicture ads.

Step 5. Optionally Use ProGuard

MoPub includes a proguard.cfg file in both the mopub-sdk and mopub-sample directories. The contents of these identical files should be included in your Proguard config when using the MoPub SDK in a Proguarded project.

Step 6. Configure Ad Units in Your App

Once you’ve completed the above steps, you can start displaying ads in your application by configuring the ad units as shown in the link below for your ad format:

Legal Disclosure

MoPub SDK v4.16 and above integrates technology from our partners Integral Ad Science, Inc. (“IAS”) and Moat, Inc. (“Moat”) in order to support viewability measurement and other proprietary reporting that IAS and Moat provide to their advertiser and publisher clients. You have the option to remove or disable this technology by following the opt-out instructions.

If you do not remove or disable IAS’s and/or Moat’s technology in accordance with these instructions, you agree that IAS’s privacy policy and license and Moat’s privacy policy, terms, and license, respectively, apply to your integration of these partners’ technologies into your application.

Last updated August 26, 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.