Android Guidelines |

Getting Started with Android SDK Integration

Integrating and monetizing with InMobi SDK with is easy. This Android SDK integration guide will help you with the following:

To begin, download our latest SDK (ver 901) here or from JCenter.

Note:
1. The latest version of InMobi SDK supports Android 4.1 (API level 16) and later.

2. If you are interested in using our SDK for Google Open Bidding, then kindly use the older version of our SDK (730) that is available for download here.

Step 1: Adding the InMobi SDK to your Project

Option 1: Pulling the Latest SDK via JCenter

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-ads:9.0.1'
    		
  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 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 <strong>build.gradle</strong>:

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

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 line 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'
        				
      2. Sync your Gradle project to ensure that the dependencies are included.

    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 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:28.0.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 <strong>defaultConfig</strong> to 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.
      implementation 'com.android.support:multidex:1.0.3'
      			

Step 2A: Additional Configurations

Granting Permissions

It is highly recommended that you include <strong>ACCESS_FINE_LOCATION</strong> 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.

<application
    ...
    android:hardwareAccelerated="true"
    ...
>
	

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 to upgrade 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: Initializing the SDK

Step 3.1

Initialize the SDK using one of the two init APIs provided below in your launcher Activity by passing the context and your InMobi account ID (both of which are detailed below).

Remember: You must call this method on the UI thread. Not doing so will cause the initialization to fail and affect your application’s ability to monetize with InMobi.

JSONObject consentObject = new JSONObject();
try {
   // Provide correct consent value to sdk which is obtained by User
   consentObject.put(InMobiSdk.IM_GDPR_CONSENT_AVAILABLE, true);
   // Provide 0 if GDPR is not applicable and 1 if applicable 
   consentObject.put("gdpr", "0");
   // Provide user consent in IAB format
   consentObject.put(InMobiSdk.IM_GDPR_CONSENT_IAB, “<<consent in IAB format>>”);
} catch (JSONException e) {
   e.printStackTrace();
}
InMobiSdk.init(this, "Insert InMobi Account ID here", consentObject);
	

Let’s understand a few things here before you proceed to Step 3.2:

What is a consentObject? A consentObject is a JSONObject representation of all kind of consent provided by the publisher to the SDK. The key is mandatory if you wish to monetize traffic from European Economic Area; InMobi relies on publishers to obtain user consent to comply with the regulations. You can read further on GDPR regulations here

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.


Important Note on consentObject:

  1. You need to provide consentObject in every session. The SDK does not persist consent, it only keeps the consentObject in memory. If the app is relaunched or crashes - SDK will lose the consentObject.
  2. Within a session, you can update the consent this way:
    InMobiSdk.updateGDPRConsent(JSONObject consentObject)
    		
  3. As part of the General Data Protection Regulation (“GDPR”) publishers who collect data on their apps, are required to have a legal basis for collecting and processing the personal data of users in the European Economic Area (“EEA”). Please ensure that you obtain appropriate consent from the user before making ad requests to InMobi for Europe and indicate the same by following our recommended SDK implementation. Please do not pass any demographics information of a user; if you do not have user consent from such user in Europe.

Note: You can locate your account id from your account interface as shown in the image below:

Okay, let’s now proceed to Step 3.2.

Step 3.2 | Pass location signals 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, you can pass the location signals as follows:

InMobiSdk.setLocation(locationObj);
	

Alternately, you can set the city, state and country as well:

InMobiSdk.setLocationWithCityStateCountry(“city”,“state”,“country”);
	

Now that this is complete, let’s talk about demographic signals in Step 3.3.

Step 3.3 | Pass demographic signals to allow better ad targeting

InMobi relies on the publishers to obtain explicit consent from user for continuing business activities in EU as per GDPR. You can supply the InMobi SDK with the gender and age as follows:

InMobiSdk.setGender(InMobiSdk.Gender.MALE); // or InMobiSdk.Gender.FEMALE
InMobiSdk.setAge(age); 
InMobiSdk.setAgeGroup(InMobiSdk.AgeGroup.BELOW_18); 
   // other enums: BETWEEN_18_AND_20, BETWEEN_21_AND_24, BETWEEN_25_AND_34 
   // BETWEEN_35_AND_5, ABOVE_55
	

Remember: Passing location signals helps better ad targeting and ensures the ads served on your app are contextual and highly relevant to your users.

You are all set now! Let’s now have a look at how you can set up different ad formats and begin monetizing. Ready?