MoPub | Android

Integrating and monetizing InMobi SDK with Mopub is easy. Please refer to this guide for integration instructions.

You have access to the following ad types from InMobi via Mopub mediation.

  • Banner
  • Medium - Banner
  • Fullscreen (Interstitial, Full-screen Video Ads)
  • Rewarded Video
  • Native

Before you begin setting up InMobi as an ad source on Mopub dashboard, it’s important that you create an InMobi account, register your app, configure placements and complete the payment information. You can learn more about it here.

If you have completed the above steps, then you are all good to begin setting up InMobi as an ad source on Mopub. Let’s begin!

Step 1: Configuring InMobi as an Ad Source on MoPub’s Dashboard

Log on to your MoPub account and identify the ad units that you wish to monetize via InMobi. Then, navigate to the Networks tab.

  1. To monetize via InMobi, select Custom SDK Network. It allows you to integrate InMobi via MoPub with plug-n-play “Custom Events”.

    Note: You need both the InMobi SDK and the custom event for successful integration.

  2. Navigate to the specific ad unit that you want to monetize via InMobi.

  3. Enter the name of InMobi custom event class specific for that particular ad format as Custom Event Class value.
    • In case you're on-boarding / using the InMobiMoPubPlugin AAR, please use the following CustomEvent class names strictly while setting up.

      Ad Unit

      Class Name

      Banner

      com.mopub.mobileads.InMobiBannerCustomEvent

      Medium (300x250 banner)

      com.mopub.mobileads.InMobiBannerCustomEvent

      Fullscreen

      com.mopub.mobileads.InMobiInterstitialCustomEvent

      Rewarded Video

      com.mopub.mobileads.InMobiRewardedCustomEvent

      Native

      com.mopub.nativeads.InMobiNativeCustomEvent

    • In case you were already using the old plugin (Copying from Project/ZIP) : For example, Interstitial Ad - the custom event is called:
      “com.inmobi.showcase.InMobiInterstitialCustomEvent”. Use fully qualified Custom Event class names. Here is the complete grid for custom events by ad unit type:

      Ad Unit

      Class Name

      Banner

      <your_package_name>.InMobiBannerCustomEvent

      Medium (300x250 banner)

      <your_package_name>.InMobiBannerCustomEvent

      Fullscreen

      <your_package_name>.InMobiInterstitialCustomEvent

      Rewarded Video

      <your_package_name>.InMobiRewardedCustomEvent

      Native

      <your_package_name>.InMobiNativeCustomEvent/

      <your_package_name>.InMobiNativeStrandCustomEvent

      In addition to these, if you are using custom packaging then you also need to follow the below mentioned guidelines in order to complete the process.

      Note:In case you have used your own CustomEvent class then please note that the package name <your_package_name> should be replaced with the actual package name in which the custom event classes are placed in the app.

  4. Your account ID is available in your InMobi account as illustrated in the screenshot below.

  5. Placement ID is present under the Placements section as illustrated in the screenshot below.

  6. Enter the Account ID and the Placement ID in the following format in the placeholder for Custom Event Class Data:
    {
        "placementid":"",
        "accountid":""
    }            
                

    A sample input will look like this:

    {
        "placementid":"1468850508657",
        "accountid":"2072131215e04a5ea20e25ffec1a7171"
    }
                

Step 2: Adding the SDK and Custom Event to your Project

Option 1: Pulling the Latest SDK via JCenter (Recommended)

If you are using Gradle to build your Android applications, you can pull the latest version of the SDK from JCenter as described below:

  1. Include JCenter in your top-level build.gradle file
    allprojects {
        repositories {
            jcenter()
        }
    }
    		    
  2. Add the following line to the dependencies element in your application module’s build.gradle.
    implementation 'com.inmobi.monetization:inmobi-mopub-plugin:9.0.6.0'
    		    
  3. Sync your Gradle project to ensure that the dependency is downloaded by the build system.

Option 2: Adding the SDK Library to your Application Project

Alternately, you can download the latest version of InMobi’s SDK here and plugin dependency from here and copy the library to your application module’s libs/ directory.

To add the library to your project’s dependencies, add this line to the dependencies element in your module’s build.gradle:

implementation fileTree(dir: 'libs', include: ['*.aar'])
	

Step 3: Adding and Verifying the Dependencies

To monetize with the InMobi Ad SDK, you must add the following dependencies to your application module build.gradle:

Google Play Services

The InMobi SDK for Android needs the Google Play Services library to enable better ad targeting via the Google Play Advertising ID. Additionally, the SDK also uses the Google Play Services for getting a location fix, should the user consent to collecting their location.

To add the Google Play Services client library to your application:

  1. Add the following lines to the application module’s dependency element:
    implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'//optional dependency for better targeting
    		    
  2. Sync your Gradle project to ensure that the dependencies are included.
Chrome Custom Tab

This is required to redirect the users to URLs outside InMobi WebView. To add the Chrome Custom library to your application:

  1. Add the following line to the application module's dependency element in the Gradle build script:
    implementation 'com.android.support:customtabs:28.0.0'
    		    
  2. Sync your Gradle project to ensure dependencies are included.

    Note: Failure to include Chrome Custom Tab dependency in your application gradle scripts will cause ad requests to fail, thus affecting monetization of your app with the InMobi SDK.

Picasso Library

The InMobi SDK for Android uses the popular Picasso library for loading the ad assets. To add the Picasso library to your application:

  1. Add the following line to the application module’s dependency element in the gradle build script.
    implementation 'com.squareup.picasso:picasso:2.71828'
    		    

Note: Failure to include Picasso dependency in your application gradle scripts will cause interstitial ad requests to fail, thus affecting monetization of your app with the InMobi SDK.

Support Library

For supporting paged scroll of a deck of images or ads, you must use support-v4 library.

To do so, add the following line to the application module’s dependency element in the Gradle build script.

implementation 'com.android.support:support-v4:28.0.0'
	
RecyclerView

For supporting free scroll of a deck of images or ads, you must use RecyclerView.

To do so, add the following line to the application module’s dependency element in the Gradle build script.

implementation 'com.android.support:recyclerview-v7:27.1.0'
	

Note: Failure to include RecyclerView dependency in your application gradle scripts will cause interstitial ad requests to fail, thus affecting monetization of your app with the InMobi SDK.

Miscellaneous

At times, including the InMobi SDK may cause the 64K limit on methods that can be packaged in an Android dex file to be breached. This can happen if, for example, your app packs a lot of features for your users and includes substantive code to realize this.

If this happens, you can use the multidex support library to enable building your app correctly. To do this:

  1. Modify the defaultConfig mark your application as multidex enabled:
    defaultConfig {
        applicationId "com.inmobi.samples"
        minSdkVersion 15
        targetSdkVersion 29 
        versionCode 1
        versionName "1.0.0"
        multiDexEnabled true // add this to enable multi-dex
    }
    		    
  2. Add the following line to the dependencies element in your application module’s build script.
    compile 'com.android.support:multidex:1.0.3'
    		

Note: Now that you have added the dependencies, your app's build.gradle at this point will look like as shown below:

android {
    defaultConfig { 
    }
    buildTypes {
        ...
    }
    sourceSets {
        main {
        }
    }
}
repositories {
     ...
}
dependencies {
    implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'//optional dependency for better targeting
    implementation 'com.android.support:customtabs:28.0.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:recyclerview-v7:27.1.0'
}
	

Step 3.1: Additional Configurations

Granting Permissions

It is highly recommended that you include ACCESS_FINE_LOCATION to enable better ad targeting. This is not a mandatory permission; however, including it will enable accurate ad targeting.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
	

For further improved targeting, you can add the ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions to the manifest.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
	
Hardware Acceleration

Hardware acceleration allows you to display HTML5 video ads.

To do this, add hardwareAccelerated:true element to the application tag.

Proguard Configuration

Proguarding helps to remove unused symbols and reduce the final application footprint. The following proguard directives should be added to your application’s proguard configuration file.

-keepattributes SourceFile,LineNumberTable
-keep class com.inmobi.** { *; }
-keep public class com.google.android.gms.**
-dontwarn com.google.android.gms.**
-dontwarn com.squareup.picasso.**
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient{
     public *;
}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info{
     public *;
}
# skip the Picasso library classes
-keep class com.squareup.picasso.** {*;}
-dontwarn com.squareup.okhttp.**
# skip Moat classes
-keep class com.moat.** {*;}
-dontwarn com.moat.**
# skip IAB classes
-keep class com.iab.** {*;}
-dontwarn com.iab.**
	

Important Prerequisites:

Let’s pause for some checks before we begin initializing the SDK:

Checkpoint 1: Preparing Your App for Android 10

InMobi SDK is compatible with Android 10 (API level 29), the latest version of the Android platform.

On Android P onwards, apps make connections over HTTPS by default. Though InMobi supports the adoption of HTTPS, the current setup requires our demand partners to support HTTPS as well. The publisher can change their settings to HTTP if it has an impact on the revenue potential.

Checkpoint 2: Preparing Your App for AndroidX

InMobi SDK is compatible with AndroidX (JetPack) library. In case your app uses the AndroidX library, then follow the standard guidelines by Google for migration by adding both the flags in build file.

android.useAndroidX=true
android.enableJetifier=true
	

It is highly recommended upgrading Android Gradle plugin version to 4.1 or greater to avoid known issues in the jetifier which is fixed in the latest Gradle plugin version. To upgrade Android gradle plugin version please refer to Android developer documentation.

Step 3.2: Code Steps

The code steps remain the same as they are for regular MoPub integration for fetching and loading an ad. Use the regular MoPub “ad unit id” to request and fetch an ad. In case you don't have any additional code to be implemented in the CustomEvent classes, you can ignore this section as using the InMobiMoPubPlugin AAR should be sufficient.

Migration from project to AAR

In case you're migrating to plugin version 9.0.5+, then there're few changes required from your side to enable monetization. Please follow the below mentioned process :

Bannner

package ;

// Your imports
import com.mopub.mobileads.InMobiBannerCustomEvent;

public class  extends InMobiBannerCustomEvent {
    // NOTE : The implementation of the class must be empty, but in case any custom code needs to be
    // implemented you must ensure to call super, for example as mentioned below.
    @Override
    protected void loadBanner(Context context, CustomEventBannerListener customEventBannerListener,
                              Map localExtras, Map serverExtras) {
        super.loadBanner(context, customEventBannerListener, localExtras, serverExtras);
        // Your code
    }
}
    

Interstitial

package ;

// Your imports
import com.mopub.mobileads.InMobiInterstitialCustomEvent;

public class  extends InMobiInterstitialCustomEvent {
    // NOTE : The implementation of the class must be empty, but in case any custom code needs to be
    // implemented you must ensure to call super, for example as mentioned below.
    @Override
    protected void loadInterstitial(Context context, CustomEventInterstitialListener interstitialListener,
                                    Map localExtras, Map serverExtras) {
        super.loadInterstitial(context, interstitialListener, localExtras, serverExtras);
        // Your code
    }
}
    

Rewarded

package ;

// Your imports
import com.mopub.mobileads.InMobiRewardedCustomEvent;

public class  extends InMobiRewardedCustomEvent {
    // NOTE : The implementation of the class must be empty, but in case any custom code needs to be
    // implemented you must ensure to call super, for example as mentioned below.
    @Override
    protected void loadWithSdkInitialized(@NonNull Activity activity, @NonNull Map localExtras,
                                          @NonNull Map serverExtras) {
        super.loadWithSdkInitialized(activity, localExtras, serverExtras);
        // Your code
    }
}
    

Native

package ;

// Your imports
import com.mopub.nativeads.InMobiNativeCustomEvent;

public class  extends InMobiNativeCustomEvent {
    // NOTE : The implementation of the class must be empty, but in case any custom code needs to be
    // implemented you must ensure to call super, for example as mentioned below.
    @Override
    protected void loadNativeAd(@NonNull Context context, @NonNull CustomEventNativeListener customEventNativeListener,
                                @NonNull Map localExtras, @NonNull Map serverExtras) {
        super.loadNativeAd(context, customEventNativeListener, localExtras, serverExtras);
        // Your code
    }
}
    

Step 3.3: Passing user consent information to InMobi SDK

The publishers monetizing in EEA region will have to pass user consent to InMobi SDK. The publishers can use consent gathering mechanism of their choice, and they will have to pass on this consent information to InMobi.

consentObject

A consentObject is a dictionary representation of all kinds of consent provided by the publisher to the SDK. The key is mandatory if you wish to monetize traffic from EEA region. You can read further on GDPR regulations here.

  • The publisher has to copy InMobiGDPR class into the project.
  • Use InmobiGDPR.setGDPRConsentDictionary() API to pass on user consent using the JSONObject parameter. Publisher will have to grant or revoke consent based on the consent provided by the user. It should be called once every session. In case setGDPRConsentDictionary() is not called, we will presume that consent is not available for users belonging to EEA+UK+Switzerland region.

The keys that needs to be populated while setting GDPR consent can be accessed via the following string constants from InMobiSdk class:

Key Type Inference
gdpr_consent String A consent string is a series of numbers, which identifies the consent status of an Ad tech Vendor.

The string must follow the IAB contracts as mentioned here.

The key, gdpr_consent can be accessed via string constant IM_GDPR_CONSENT_IAB.

gdpr_consent_available boolean

true: Publisher has provided consent to collect and use user’s data.

false: - Publisher has not provided consent to collect and use user’s data.

Any value other than “true” and “false” is invalid and will be treated as value not provided by publisher, i.e. empty value.

The key, gdpr_consent_available can be accessed via string constant IM_GDPR_CONSENT_AVAILABLE.

gdpr String Whether or not the request is subjected to GDPR regulations, deviation from the set values (0 = No, 1 = Yes ), indicate an unknown entity.

More Information

You can see the complete sample code and example here The latest documentation and code samples for the MoPub SDK are available here.

Step 4: Testing the Integration

To ensure MoPub SDK relays the ad request to InMobi for testing:

  1. Set manual Ad network optimization for every network including MoPub.
  2. Set manual eCPM floors for all networks keeping the eCPM floor for InMobi higher than other networks, this will ensure that the ad request gets relayed to InMobi for testing. For example for InMobi set the floor to $5 while MoPub marketplace is set to $0.05.


Step 4.1 Enabling Key SDK Logs

You can enable key SDK logs by adding the following line to your Activity while integrating the ad-units.

InMobiSdk.setLogLevel(LogLevel.DEBUG);
	

You can also leverage feedback available on the diagnostics tab on the ad unit and ad request if you have enabled “Test Mode” on InMobi portal.

Step 5: Advanced Configurations

Pass the location information to allow better ad targeting.

Recommended: If your app collects location from the user, we recommend passing it up, as impressions with location signal have higher revenue potential. InMobi SDK will automatically pass the location signals if available in the app. If you use location in your app, but would like to disable passing location signals to InMobi, then TURN OFF the “Location Automation” for your property on the InMobi dashboard.

Otherwise, modify the Custom Event class for your ad-unit to pass on the data using the InMobi SDK API. Please see sample code for more details.

InMobi supports the following parameters:

Method Name

Value

Description

setAgeGroup

AgeGroup.BELOW_18

AgeGroup.BETWEEN_18_AND_20

AgeGroup.BETWEEN_21_AND_24

AgeGroup.BETWEEN_25_AND_34

AgeGroup.BETWEEN_35_AND_54

AgeGroup.ABOVE_55

The age group of the user

setEducation

Education.HIGH_SCHOOL_OR_LESS

Education.COLLEGE_OR_GRADUATE

Education.POST_GRADUATE_OR_ABOVE

The education level

  • High school, or less
  • College, or graduate
  • Postgraduate, or above

setAge

Integer

The age of the user

setInterests

Any String

Any additional relevant description of the user, or their preferences, separated by commas. Valid acceptable values are mentioned below

setPostalCode

Any String

The postal code (usually a 5 digit number)

setAreaCode

Any String

The area code (part of the telephone number)

setLanguage

Any String

The native language of the user (if known)

setLocationWithCityStateCountry

Any String

Sets location with City, State, Country being passed

setKeywords

Any String

Keywords to be passed in the ad request

Here is a sample for setting up the InMobi SDK Demographic params. The publishers need to set the values of params as they want.

InMobiSdk.setAreaCode("080");
InMobiSdk.setEducation(Education.HIGH_SCHOOL_OR_LESS);
InMobiSdk.setGender(Gender.MALE);
InMobiSdk.setAge(23);
InMobiSdk.setPostalCode("122009");
InMobiSdk.setLogLevel(LogLevel.DEBUG);
InMobiSdk.setLocationWithCityStateCountry("blore", "kar", "india");
InMobiSdk.setLanguage("ENG");
InMobiSdk.setInterests("dance");
InMobiSdk.setYearOfBirth(1980);