Android Guidelines

开始适配安卓SDK

首先进行如下操作:

The latest version of InMobi SDK supports Android OS versions 4.0.3 (API level 15) and later.

Version 7.1.0
Size (.APK Inflation) 370 KB
SDK Links

将SDK添加到你的项目

函数1: 通过jCenter提取最新版本的SDK

如果你正使用Gradle创建安卓应用程序,那么你可以从jCenter提取最新版本的SDK。

方法如下:

  1. 把下列内容列入你的顶级build.gradle文件中。

    allprojects {
        repositories {
            jcenter()
        }
    }
    
    
  2. 将下列代码添加到你应用模块build.gradle中的依赖元素素内。

    compile 'com.inmobi.monetization:inmobi-ads:6.X.X'
    
    
  3. 同步你的Gradle项目,确保构建系统已下载依赖关系。

函数2: 在你的应用程序项目中添加SDK库

你可以选择下载在 这里 载InMobi最新版本的SDK,并将库拷贝到你的应用模块中的libs/目录。

为把库添加到你项目的依赖关系中,将以下这行代码添加到你应用模块 build.gradle中的依赖元素素内。

compile fileTree(dir: 'libs', include: ['*.jar'])

添加并验证依赖关系

为利用InMobi广告SDK获利,你必须添加以下依赖关系:

  •  谷歌播放服务(Google Play Services)

    适配安卓系统的InMobi SDK需要使用谷歌播放服务库通过谷歌播放广告ID更好地确定目标。此外,在用户许可收集定位的前提下,SDK还需利用谷歌播放服务获取准确定位。

    在你的应用程序中添加谷歌播放服务客户库:

    1. 将以下这行代码添加到应用模块的依赖元素素中。

      compile 'com.google.android.gms:play-services-base:11.8.0'    
          compile 'com.google.android.gms:play-services-ads:11.8.0'
          compile 'com.google.android.gms:play-services-location:11.8.0'
          compile 'com.google.android.gms:play-services-plus:11.8.0'
      
      
    2. 同步你的Gradle项目,确保依赖关系已列入。
    3. 将下列元素添加到你的应用清单中。

      <meta-data android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />
      
      
  • Picasso(毕加索)

    适配安卓系统的InMobi SDK采用普及的Picasso库加载广告资源。

    在你的应用程序中添加Picasso库:

    1. 将以下这行代码添加到Gradle构建脚本中应用模块的依赖元素素内。

      compile 'com.squareup.picasso:picasso:2.5.2'
      
      
    2. 同步你的Gradle项目,确保依赖关系已列入。

    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)

    对于原生故事板广告形式,你必须添加 support-v4库。

    如需如此,请将以下这行代码添加到Gradle构建脚本中应用模块的依赖元素素内。

    compile 'com.android.support:support-v4:27.1.0'
    
    
  • 循环视窗(RecyclerView)

    为支持桌面图片或广告自由滚动,你必须使用 RecyclerView

    如需如此,将以下这行代码添加到Gradle构建脚本中应用模块的依赖元素素内。

    compile '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)

    有时,包含InMobi SDK可能导致被打包在安卓dex文件中的函数超过64K限制。例如,如果你的app包含诸多用户特性,并使用了大量代码以实现这些特性,就可能发生这种情况。如果这种情况发生了,你可以使用multidex支持库以确保正确创建你的app。

    方法如下:

    1. 修改defaultConfig,将你的应用程序标记为multidex enabled(启用):

      defaultConfig {
      applicationId "com.inmobi.samples"
      minSdkVersion 15
      targetSdkVersion 24
      versionCode 1
      versionName "1.0.0"
      multiDexEnabled true // add this to enable multi-dex
      }
      
      
    2. 将以下这行代码添加到Gradle构建脚本中应用模块的依赖元素素内。

      compile 'com.android.support:multidex:1.0.1'
      
      

修改清单文件

授予权限

你的应用程序清单中必须包含下列命令权限。

注意: 若未能包含这些权限,将影响你利用应用程序在InMobi网络上盈利。

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

除了以上命令权限外,强烈建议你列入 ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION,以更好地锁定广告目标。

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

为进一步准确地锁定广告目标,你可以在清单中添加 ACCESS_WIFI_STATECHANGE_WIFI_STATE 许可。

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

如果你想采用富媒体广告,你可以添加下列权限。

<uses-permission android:name="android.permission.READ_CALENDAR">
<uses-permission android:name="android.permission.WRITE_CALENDAR">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
		</uses-permission></uses-permission></uses-permission>

App安装属性

Mandatory permission:

对于app安装属性,在清单文件中添加下列内容:

<receiver android:enabled="true" android:exported="true" android:name="com.inmobi.commons.core.utilities.uid.ImIdShareBroadCastReceiver" tools:ignore="ExportedReceiver">
          <intent-filter>
              <action android:name="com.inmobi.share.id">
          </action></intent-filter>
 </receiver>

硬件加速

硬件加速允许你播放HTML5视频广告。如需如此,请在 application标签中添加hardwareAccelerated:true元素。

混淆器配置

混淆器帮助移除不用的符号,并减少最终应用程序占用。需要在你的应用程序的混淆器配置文件中添加下列混淆器指令。

-keepattributes SourceFile,LineNumberTable
-keep class com.inmobi.** { *; }
-dontwarn 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.picasso.**
-dontwarn com.squareup.okhttp.**
# skip Moat classes
-keep class com.moat.** {*;}
-dontwarn com.moat.**
# skip AVID classes
-keep class com.integralads.avid.library.* {*;}

使您的应用程序适配Android N系统

适配安卓系统的SDK 6.0.0与最新版安卓平台Android N(API等级24)兼容。你仅需对应用程序清单做一些小改动,便可开始在Android N上盈利。

如果你的app以API等级24或更高为目标,你需要这样在你的应用程序中声明 InMobiAdActivity元素。

<activity android:configchanges="keyboardHidden|orientation|keyboard|smallestScreenSize|screenSize|screenLayout" android:hardwareaccelerated="true" android:name="com.inmobi.rendering.InMobiAdActivity" android:resizeableactivity="false" android:theme="@android:style/Theme.NoTitleBar" tools:ignore="UnusedAttribute">
		</activity>

特别地,注意 android:resizeableActivity=”false”属性,并为screenLayout改变配置这个Activity。

初始化SDK

  1. 通过验证 Activity context(上下文)及你的InMobi账户ID,在你的启动栏Activity初始化SDK,如下所示:
    InMobiSdk.init(MainActivity.this, "Insert InMobi Account ID here");
    
    

    Find your account id from your account interface as shown in the image below.

    注意: 你必须在UI线程上调用这个函数。否则初始化将失败,并影响应用程序通过InMobi盈利的能力。

  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”);
    
    
  3. Pass demographic signals to allow better ad targeting.

    You can supply the InMobi SDK with the gender and age:

    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
    
    

Note

Pass location signals to allow better ad targeting.

开始采取这些广告形式利用你的安卓app盈利:

此页面有用吗?

在本页面

上次更新时间: 18 Sep, 2020