Getting Started

blogvinskiy edited this page Jun 28, 2016 · 73 revisions

This document details the process of integrating the MoPub SDK with your Android application. If you have any questions, don't hesitate to email us at support@mopub.com.

Here are the basic integration steps:

  1. Check out the files from our repository.
  2. Add our SDK (and dependencies) to your project.
  3. Create ad units in your app.

Download the MoPub Android SDK

The MoPub SDK is available via:

  1. jCenter AAR (Recommended)

    Download
    The MoPub SDK is available as an AAR via jCenter. To add the mopub-sdk dependency, open your project's build.gradle and update the repositories and dependencies blocks as follows:

    repositories {
        // ... other project repositories
        jcenter()
    }
    
    // ...
    
    dependencies {
        // ... other project dependencies
        compile('com.mopub:mopub-sdk:4.7.1@aar') {
            transitive = true
        }
    }

    To continue integration using the mopub-sdk AAR, please continue to Updating your Android Manifest section.

  2. Zipped Source

    The MoPub SDK is also distributed as zipped source code that you can include in your application. MoPub provides two prepackaged archives of source code:

    MoPub Android Full SDK.zip
    Includes everything you need to serve MoPub ads and built-in support for Millennial Media third party ad network - Millennial Media - including the required third party binaries.

    Note: Millennial Media has updated their minimum supported version of Android to 4.1 (API level 16+). The MoPub Android Full SDK manifest and build.gradle files have been updated accordingly.

    Note: Millennial Media has deprecated support for Ant/Eclipse; migrating users should use the MoPub Android base SDK and follow Millennial's Ant/Eclipse Integration Guide.

    MoPub Android Base SDK.zip
    Includes everything you need to serve MoPub ads. No third party ad networks are included.

  3. Cloned GitHub repository

    Alternatively, you can obtain the MoPub SDK source by cloning the git repository:

    git clone git://github.com/mopub/mopub-android-sdk.git

Requirements and Dependencies

  • Android 2.3.1 (API Version 9) and up
  • android-support-v4.jar, r22 (Updated in 3.7.0)
  • android-support-annotations.jar, r22 (Updated in 3.7.0)
  • android-support-v7-recyclerview.jar, r22 (Updated in 3.9.0)
  • MoPub Volley Library (mopub-volley-1.1.0.jar - available on JCenter) (Updated in 3.6.0)
  • Recommended Google Play Services 7.8.0

We strongly recommend compiling your app against the Google Play Services Library in order to use the Android Advertising ID instead of the device ID, as required by Google. Failing to correctly use the Android Advertising ID may result in your submission to the Play Store being rejected.

(Non-jCenter) Gradle Integration

Adding as Source

To include the MoPub SDK as source in your project, copy the SDK source into your project as a module. To do this on OS X and Linux, do the following:

$MY_PROJECT_DIR $ mkdir mopub-sdk
$MY_PROJECT_DIR $ cp -R $MOPUB_DIR/mopub-android-sdk/mopub-sdk mopub-sdk

Next, open your project's settings.gradle file and make sure the MoPub SDK is included as a module:

include ':app', ':mopub-sdk'

Open your project's build.gradle file and add jcenter as a repository and the MoPub SDK as a dependency:

repositories {
    jcenter()
}

// ...

dependencies {
    compile project(':mopub-sdk')
    // ...
}

This is required in order to resolve the MoPub SDK's compile time dependency on com.mopub.volley:mopub-volley:1.1.0.

Adding as 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 gradle mopub-sdk:build. This will build an AAR that bundles the mopub-volley-1.1.0.jar.

Copy the .aar to your library directory: cp build/outputs/aar/mopub-sdk.aar $MY_LIB_DIR where $MY_LIB_DIR is your default library directory.

Open your project's build.gradle file and add the below lines:

repositories {
  jcenter()
  flatDir {
    dirs '$MY_LIB_DIR'
  }
}

// ...

dependencies {
  compile(name:'mopub-sdk', ext:'aar')
  compile 'com.mopub.volley:mopub-volley:1.1.0:aar'
}

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 {
 multiDexEnable true
}

Maven Integration

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

$ mvn clean install -DskipTests=true

inside the mopub-sdk directory.

The mopub-sdk POM includes dependencies on android-support-v4.aar & 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:

  <dependency>
    <groupId>com.mopub.mobileads</groupId>
    <artifactId>mopub-sdk</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <type>aar</type>
  </dependency>

Remember that if you're using Maven you'll 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.

Eclipse Integration

The MoPub Android SDK archive contains a library project called mopub-sdk.

  1. Import the mopub-sdk archive into your workspace by clicking File > Import... and choosing Existing Projects into Workspace.

    image

  2. Click the Select Archive File button then click Browse...

    image

  3. Select the mopub-sdk archive file. Then, click Open.

    image

  4. ...then Finish.

    image

Adding Volley Dependency

  1. Download the mopub-volley-1.1.0.jar

  2. Go to the Eclipse Package Explorer, right-click your project folder and select Properties.

  3. Choose Java Build Path from the sidebar.

    image

  4. Click Add Jars and add the mopub-volley-1.1.0.jar from your download location.

Adding the SDK to Your Project

  1. Go to the Eclipse Package Explorer, right-click your project folder and select Properties.

  2. Choose Android from the sidebar.

    image

  3. Click Add... under the Library section, and select the mopub-sdk project.

    image

Adding the Support Libraries to Your Project

Follow the instructions on the Android Developer Site for setting up the two support libraries using Eclipse: https://developer.android.com/tools/support-library/setup.html

You will need to add both android-support-v4.jar & android-support-annotations.jar

Adding Google Play Services

Follow the instructions on the Android Developer Site for setting up Google Play Services. We recommend always using the latest version of Google Play Services. http://developer.android.com/google/play-services/setup.html

Adding Ad Network Libraries to Your Project

If you are using the full bundle you will need to add the Jar files for the supported ad networks to the build path of your project.

  1. Go to the Eclipse Package Explorer, right-click your project folder and select Properties.

  2. Choose Java Build Path from the sidebar.

    image

  3. Click Add Jars, open the mopub-sdk/libs section of the tree and add all of the files it contains.

    image

Updating your Android Manifest

Your AndroidManifest.xml will need to be updated in order to complete the SDK integration. Add the following permissions and activity declarations according to the bundle you are integrating.

Base Bundle / AAR:

Declare the following permissions:

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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.

Declare the following activities:

  <activity android:name="com.mopub.mobileads.MoPubActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.mobileads.MraidActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.common.MoPubBrowser" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>

Add this tag to your <application> to use Google Play Services:

  <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Important Notes

  1. If you're compiling against APIs below 13, you should exclude screenSize from the manifest entries.
  2. Make sure the com.mopub.mobileads.MraidVideoPlayerActivity is added to your manifest. Without it, video ads will not work correctly.

Full Bundle:

Declare the following permissions:

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
  <uses-permission android:name="android.permission.NFC"/>
  <uses-permission android:name="android.permission.BLUETOOTH"/>
  <uses-permission android:name="android.permission.VIBRATE"/>
  <uses-permission android:name="android.permission.RECORD_AUDIO"/>

Declare the following activities:

  <activity android:name="com.mopub.mobileads.MoPubActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.mobileads.MraidActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.common.MoPubBrowser" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

Add this tag to your <application> to use Google Play Services:

  <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Important Notes

  1. If you're compiling against APIs below 13, you should exclude screenSize from the manifest entries.
  2. Make sure the com.mopub.mobileads.MraidVideoPlayerActivity is added to your manifest. Without it, video ads will not work correctly.

Displaying Ads

Once you've completed the above steps, you can start displaying ads in your application by following the simple instructions on the Banner Ad or Interstitial Ad pages.