Migración de SDK

本页面介绍了当前版本和之前版本的迁移。

从 v22 迁移到 v23

最低 Android API 级别为 21

从版本 23.0.0 开始,Google 移动广告 SDK 要求所有应用都必须 Android API 级别 21 或更高级别才能运行。要调整 API 级别,请将 将应用级 build.gradle 文件中 minSdkVersion 的值设置为 21 或更高。

已移除/替换的已弃用方法

v22.0.0 中的 API v23.0.0 中的 API
AdRequest.Builder.addTestDevice() RequestConfiguration.Builder.setTestDeviceIds()
AdRequest.Builder.tagForChildDirectedTreatment() RequestConfiguration.Builder.setTagForChildDirectedTreatment()
AdRequest.Builder.setIsDesignedForFamilies() RequestConfiguration.Builder.setMaxAdContentRating()
AdFormat.UNKNOWN 无替代项
AdLoader.Builder.forUnifiedNativeAd() AdLoader.Builder.forNativeAd()
AdLoader.Builder.forCustomTemplateAd() AdLoader.Builder.forCustomFormatAd()
AdLoader.Builder.withNativeAdOptions(com.google.android.gms.ads.formats.NativeAdOptions) AdLoader.Builder.withNativeAdOptions(com.google.android.gms.ads.nativead.NativeAdOptions)
void MobileAds.setSameAppKeyEnabled() boolean MobileAds.putPublisherFirstPartyIdEnabled()

从 v21 迁移到 v22

使用 MobileAds.getVersion() 获取 Google 移动广告 SDK 版本

版本 22.0.0 中移除了 MobileAds.getVersionString() 方法,取而代之的是 / MobileAds.getVersion()

新方法会返回预期的外部版本号,例如, 22.0.0。有关此变化的更多信息,请参见使用新的 Google 移动 广告 SDK getVersion() 方法

已移除或替换的方法

下表列出了版本 22.0.0 中的具体更改。

v21.0.0 v22.0.0
MobileAds.getVersionString() MobileAds.getVersion()
NativeCustomFormatAd.getVideoMediaView() NativeCustomFormatAd.getMediaContent()
NativeCustomFormatAd.getVideoController() NativeCustomFormatAd.getMediaContent().getVideoController()
AdRequest.Builder.setAdInfo() AdRequest.Builder.setAdString()
MediationRewardedVideoAdAdapter 适配器
MediationRewardedVideoAdListener
com.google.android.gms.ads.mediation.VersionInfo com.google.android.gms.ads.VersionInfo
com.google.android.gms.ads.doubleclick.AppEventListener com.google.android.gms.ads.admanager.AppEventListener

从 v20 迁移到 v21

将 minSdkVersion 更新为 19 或更高版本

从版本 21.0.0 开始,Google 移动广告 SDK 要求所有应用都必须 在最低 Android API 级别 19 上运行。要调整 API 级别,请将 minSdkVersion 值设置为 19 或更高版本。

强制执行严格的可为 null 性

为准备强制执行严格的可为 null 性,@NonNull 注解 添加了之前未明确定义可为 null 性的所有 API。

此变更可能会破坏 Kotlin 应用和使用 Java null 检查的应用 框架(如果存在 null 安全违规行为且之前未处理过) 安全地使用 null 值(请参阅 Kotlin 文档,了解 null 安全 )。

移除了 addNetworkExtras() 方法

addNetworkExtras() 方法,用于将额外参数作为 SDK 中已废弃针对特定广告联盟适配器的 NetworkExtras 实例 将在版本 21.0.0 中移除。使用 addNetworkExtrasBundle() 方法。AdRequest.Builder

移除了地理位置方法

以下地理位置方法将在版本 21.0.0 中移除:

  • AdRequest.Builder 类中的 setLocation(Location location) 方法 用于为中介定位设置用户所在的地理位置。
  • AdRequest 类中的 getLocation() 方法,该方法会返回用户的 地理位置定位信息之前传递给 setLocation(Location location) 方法。
  • MediationAdConfiguration 类中的 getLocation() 方法, 如果由 AdRequest 定义,则返回用户所在位置。

Google 不会使用位置数据来定位广告。您应该使用 第三方 API 在发生以下情况时向第三方广告联盟提供信息: 必填字段。

弃用自定义事件接口

借助自定义事件,发布商可以 AdMob 中介 来设置不 AdMob的其中一个 支持的广告来源

所有自定义 事件 弃用了这些界面,取而代之的是 Adapter 类和 MediationAdLoadCallback 界面。

下表列出了应使用的相应类或接口 针对从版本 21.0.0 开始的每个自定义事件接口:

v20.0.0 中的接口 v21.0.0 类/ 接口
CustomEvent 适配器
CustomEventBanner
CustomEventInterstitial
CustomEventNative
CustomEventBannerListener Adapter 中, MediationAdLoadCallback
CustomEventInterstitialListener
CustomEventListener
CustomEventNativeListener

从 v19 迁移到 v20

我们在版本 20.0.0 中进行了多项重大更改。19.7.0 版推出 许多新的 API,并废弃或重命名了许多类,以便为版本做好准备 20.0.0.本指南重点介绍了版本 20.0.0 中的主要更改。

全屏广告格式 API 更新

从版本 20.0.0 开始,插页式广告、激励广告、插页式激励广告和 开屏广告格式已经过标准化处理,以遵循一致的 API 设计。

所有全屏广告格式 API 均遵循以下原则:

移除旧版 RewardedVideoAd API

较新的 RewardedAd API 于 2019 年 3 月首次推出,已成为 API 使用超过 18 个月。与旧版相比,新版 SDK 具备更多增强功能 RewardedVideoAd API,包括能够加载多个激励广告 。

SDK 版本 20.0.0 中移除了旧版 RewardedVideoAd API。

弃用智能横幅广告,取而代之的是自适应横幅广告

智能横幅广告已被弃用 弃用自适应横幅广告 广告。自适应横幅广告可提供卓越的效果和更大的灵活性, 设置广告宽度。如果您希望继续使用全宽横幅广告 仍使用自适应横幅广告,如以下代码段所示:

Java

public class MyActivity extends AppCompatActivity {
  ...
  private AdSize getFullWidthAdaptiveSize() {
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    float widthPixels = outMetrics.widthPixels;
    float density = outMetrics.density;

    int adWidth = (int) (widthPixels / density);
    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
  }
}

Kotlin

class MyActivity : AppCompatActivity() {
  ...
  private val adaptiveAdSize: AdSize
    get() {
      val display = windowManager.defaultDisplay
      val outMetrics = DisplayMetrics()
      display.getMetrics(outMetrics)

      val density = outMetrics.density

      var adWidthPixels = ad_view_container.width.toFloat()
      if (adWidthPixels == 0f) {
        adWidthPixels = outMetrics.widthPixels.toFloat()
      }

      val adWidth = (adWidthPixels / density).toInt()
      return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
    }
}

移除退出应用回调

已移除适用于所有广告格式的 onAdLeftApplication 回调函数 / ProcessLifecycleOwner。 如果使用操作系统级 API,无论用户何时离开您的应用,无论他们 是否属于广告互动

请注意,onAdLeftApplication 回调本应不是广告 并且依靠此回调来报告点击次数不会产生 准确的指标例如,点击“广告选项”图标会启动 外部浏览器调用了回调函数,但未计为一次点击。

重命名类

下表列出了已更改或移除的具体类名称。步骤简述:

  • UnifiedNativeAd 相关的大多数类均已重命名为 NativeAd
  • MobileAds.SettingsNativeExpressAdViewNativeAppInstallAd 移除了 NativeContentAd 以及 InstreamAd 类和视图。
  • 所有带 Publisher 前缀的类均已替换为 AdManager 前缀。
  • InterstitialAd 软件包名称已更改。
19.5.0 中的类 20.0.0 中的类
com.google.android.gms.ads. com.google.android.gms.ads.
reward.RewardedVideoAd rewarded.RewardedAd
reward.RewardedVideoAdListener 激励广告.RewardedAdLoadCallback 和 FullScreenContentCallback
reward.RewardItem rewarded.RewardItem
rewarded.RewardedAdCallback OnUserEarnedRewardListener
formats.UnifiedNativeAdView nativead.NativeAdView
formats.UnifiedNativeAd nativead.NativeAd
formats.UnifiedNativeAdAssetNames nativead.NativeAdAssetNames
formats.UnifiedNativeAd.OnUnifiedNativeAdLoadedListener nativead.NativeAd.OnNativeAdLoadedListener
formats.AdChoicesView nativead.AdChoicesView
formats.NativeAd.AdChoicesInfo nativead.NativeAd.AdChoicesInfo
formats.MediaView nativead.MediaView
formats.NativeAdViewHolder nativead.NativeAdViewHolder
formats.NativeAdOptions nativead.NativeAdOptions
formats.NativeCustomTemplateAd nativead.NativeCustomFormatAd
formats.NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener nativead.NativeCustomFormatAd.OnCustomFormatAdLoadedListener
MobileAds.Settings 已移除
doubleclick.PublisherAdRequest admanager.AdManagerAdRequest
doubleclick.PublisherAdView admanager.AdManagerAdView
formats.PublisherAdViewOptions formats.AdManagerAdViewOptions
doubleclick.PublisherInterstitialAd admanager.AdManagerInterstitialAd
InterstitialAd interstitial.InterstitialAd
NativeExpressAdView 已移除
instream.InstreamAd 已移除
mediation.admob.AdMobExtras 已移除
Correlator 已移除
search.SearchAdRequest 已移除
AdRequest.TagForUnderAgeOfConsent 接口 已移除
AdRequest.MaxAdContentRating 接口 已移除
formats.NativeAppInstallAd native.NativeAd
formats.NativeAppInstallAdView native.NativeAdView
mediation.NativeAppInstallAdMapper mediation.UnifiedNativeAdMapper
formats.NativeContentAd native.NativeAd
formats.NativeContentAdView native.NativeAdView
mediation.NativeContentAdMapper mediation.UnifiedNativeAdMapper

移除/替换的方法

下表列出了版本 20.0.0 中的具体更改。总结:

  • 从 MobileAds 类中移除了过时的初始化方法。
  • 已移除 AdRequest.Builder() 类中的某些方法 或移到了 RequestConfiguration 类。
  • 更新了 RewardedAd 和 InterstitialAd API,以与 全屏格式。
  • 移除了 Correlator 功能。
v19.5.0 中的 API v20.0.0 中的 API 备注
AdSize getPortraitBannerAdSizeWithWidth() getPortraitAnchoredAdaptiveBannerAdSize()
getLandscapeBannerAdSizeWithWidth() getLandscapeAnchoredAdaptiveBannerAdSize()
getCurrentOrientationBannerAdSizeWithWidth() getCurrentOrientationAnchoredAdaptiveBannerAdSize()
MobileAds initialize(Context, String) MobileAds.initialize(Context, OnInitializationComplete监听器) 现在,在 AndroidManifest.xml 中设置应用 ID。
initialize(Context, String, MobileAds.Settings) initialize(Context, OnInitializationCompleteListener) Settings 类已弃用。
getRewardedVideoAdInstance() 已移除 请改用 RewardedAd API。
AdListener onAdFailedToLoad(int) onAdFailedToLoad(LoadAdError)
onAdLeftApplication() 已移除 全屏广告格式使用 FullscreenContentCallback 代替 AdListener,并且 FullscreenContentCallback 上没有等效方法。通过 方法已从 AdListener 删除。
VideoController getAspectRatio() MediaContent.getAspectRatio()
AdRequest getGender() 已移除
getBirthday() 已移除
getNetworkExtras() 已移除
setManualImpressionsEnabled() 已移除
updateCorrelator() 已移除 请参阅 广告内容过滤
AdRequest.Builder setBirthday() 已移除
setGender() 已移除
setIsDesignedForFamilies() 已移除 请参阅指南
addTestDevice() RequestConfiguration.Builder.setTestDeviceIds() 请参阅启用测试广告
tagForChildDirectedTreatment() RequestConfiguration.Builder.setTagForChildDirectedTreatment() 请参阅 广告内容过滤
setTagForUnderAgeOfConsent() RequestConfiguration.Builder.setTagForUnderAgeOfConsent()
setMaxAdContentRating() RequestConfiguration.Builder.setMaxAdContentRating()
AdView getMediationAdapterClassName() ResponseInfo.getMediationAdapterClassName() 现在可通过 AdView 方法 getResponseInfo() 获取 ResponseInfo 对象。
NativeAdOptions setImageOrientation() setMediaAspectRatio()
getImageOrientation getMediaAspectRatio()
RewardedAd loadAd(AdRequest, RewardedAdLoadCallback) RewardedAd.load(Context, String, AdRequest, RewardedAdLoadCallback) RewardedAd 现在使用与其他广告相同的静态加载方法 全屏格式。
loadAd(PublisherAdRequest, RewardedAdLoadCallback) RewardedAd.load(Context, String, AdManagerAdRequest, RewardedAdLoadCallback)
isLoaded() 已移除 静态加载方法中的回调会提供已加载的广告。
show(Activity, RewardedAdCallback) show(Activity, OnUserGainedRewardListener) 此 show 方法遵循更广泛的全屏广告格式方法。
RewardedAdLoadCallback onRewardedAdFailedToLoad(int) onAdFailedToLoad(LoadAdError)
onRewardedAdFailedToLoad(LoadAdError) onAdFailedToLoad(LoadAdError)
onRewardedAdLoaded() onAdLoaded(RewardedAd)
AppOpenAdLoadCallback onAppOpenAdFailedToLoad(int) onAdFailedToLoad(LoadAdError)
onAppOpenAdFailedToLoad(LoadAdError) onAdFailedToLoad(LoadAdError)
onAppOpenAdLoaded(AppOpenAd) onAdLoaded(AppOpenAd)
RewardedInterstitialAdLoadCallback onRewardedInterstitialAdFailedToLoad(int) onAdFailedToLoad(LoadAdError)
onRewardedInterstitialAdFailedToLoad(LoadAdError) onAdFailedToLoad(LoadAdError)
onRewardedInterstitialAdLoaded(RewardedInterstitialAd) onAdLoaded(RewardedInterstitialAd)
InterstitialAd new InterstitialAd() InterstitialAd.load(Context, String, AdRequest, InterstitialAdLoadCallback) InterstitialAd 现在使用与 其他全屏广告格式。
setAdListener()
DynamicHeightSearchAdRequest getNetworkExtras() 已移除 NetworkExtras 类已废弃。
AdLoader forContentAd() 已移除
forAppInstallAd() 已移除
withCorrelator() 已移除
getMediationAdapterClassName() 已移除