SDK 迁移

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

本文介绍了现行版本和以往版本的迁移。

迁移至 SDK v21

为避免 Google 移动广告 SDK 21.0.0 版(计划于 2022 年初发布)的潜在构建失败,请遵循本指南中介绍的检查和指南。

将 minSdkVersion 更新为 19 或更高版本

从版本 21.0.0 开始,Google 移动广告 SDK 要求所有应用程序的最低 Android API 级别为 19 才能运行。要调整 API 级别,请将应用程序构建文件中的minSdkVersion的值更改为 19 或更高。

强制执行严格的可空性

为了准备强制执行严格的可空性, @NonNull注释被添加到以前没有明确定义可空性的所有 API 中。

如果存在 null 安全违规并且之前未以安全方式处理null值,则此更改可能会破坏 Kotlin 应用程序和使用 Java null 检查框架的应用程序(请参阅有关 Null-safety 的 Kotlin 文档)。

删除 addNetworkExtras() 方法

将额外参数作为NetworkExtras实例传递给特定广告网络适配器的addNetworkExtras()方法在 SDK 版本 20.3.0 中已弃用,并将在版本 21.0.0 中删除。请改用同一AdRequest.Builder类中的addNetworkExtrasBundle()方法。

删除定位方法

以下定位方法将在 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 类/接口
自定义事件适配器
自定义事件横幅
自定义事件插页式
CustomEventNative
自定义事件横幅监听器适配器,中介AdLoadCallback
CustomEventInterstitialListener
自定义事件监听器
CustomEventNativeListener

迁移至 SDK v20

There are many breaking changes coming in version 20.0.0. Version 19.7.0 introduced many new APIs, and deprecated or renamed many classes in preparation for version 20.0.0. This guide highlights the major changes brought about in version 19.7.0 as well as what's coming for version 20.0.0 (early 2021).

Full-screen format API updates

Starting with version 20.0.0, interstitial, rewarded, rewarded interstitial, and App Open ad formats are standardized to follow a consistent API design.

All full-screen format APIs utilize the following principles:

  • A static load method
  • A similar load callback or handler mechanism
  • Reliance on the FullScreenContentCallback class for presentation callbacks

Legacy RewardedVideoAd API removal

The newer RewardedAd API was first introduced back in March 2019 and has been the preferred rewarded API for over 18 months. It has had more enhancements compared to the legacy RewardedVideoAd API, including the ability to load more than one rewarded ad at a time.

The legacy RewardedVideoAd API is removed in SDK version 20.0.0.

Smart banner deprecation in favor of adaptive banner

Smart banner ads are deprecated in favor of adaptive banner ads. Adaptive banners provide superior performance and more flexibility in setting ad width. If you prefer to continue using full-width banners, that can still be done using adaptive banner, as shown in the following code snippet:

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)
    }
}

Leave application callback removal

The onAdLeftApplication callback for all ad formats has been removed in favor of ProcessLifecycleOwner. Using an OS-level API notifies you whenever users leave your app, regardless of whether or not it is due to an ad interaction.

Note that the onAdLeftApplication callback was never intended to be an ad click handler, and relying on this callback to report clicks did not produce an accurate metric. For example, a click on the AdChoices icon that launched an external browser invoked the callback but did not count as a click.

Class renames

The table below lists specific class names that have changed or been removed. In summary:

  • Most classes related to UnifiedNativeAd have been renamed to NativeAd.
  • The MobileAds.Settings, NativeExpressAdView, NativeAppInstallAd, NativeContentAd, and InstreamAd classes and views have been removed.
  • All classes with the Publisher prefix have been replaced with an AdManager prefix.
  • The InterstitialAd package name has changed.
19.5.0 Class 20.0.0 Class
com.google.android.gms.ads. com.google.android.gms.ads.
reward.RewardedVideoAd rewarded.RewardedAd
reward.RewardedVideoAdListener rewarded.RewardedAdLoadCallback and 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 Removed
doubleclick.PublisherAdRequest admanager.AdManagerAdRequest
doubleclick.PublisherAdView admanager.AdManagerAdView
formats.PublisherAdViewOptions formats.AdManagerAdViewOptions
doubleclick.PublisherInterstitialAd admanager.AdManagerInterstitialAd
InterstitialAd interstitial.InterstitialAd
NativeExpressAdView Removed
instream.InstreamAd Removed
mediation.admob.AdMobExtras Removed
Correlator Removed
search.SearchAdRequest Removed
Interface AdRequest.TagForUnderAgeOfConsent Removed
Interface AdRequest.MaxAdContentRating Removed
formats.NativeAppInstallAd native.NativeAd
formats.NativeAppInstallAdView native.NativeAdView
mediation.NativeAppInstallAdMapper mediation.UnifiedNativeAdMapper
formats.NativeContentAd native.NativeAd
formats.NativeContentAdView native.NativeAdView
mediation.NativeContentAdMapper mediation.UnifiedNativeAdMapper

Methods removed/replaced

The table below lists the specific changes in version 20.0.0. In summary:

  • Outdated initialization methods have been removed from the MobileAds class.
  • Some methods from the AdRequest.Builder() class have either been removed or moved to the RequestConfiguration class.
  • RewardedAd and InterstitialAd APIs have been updated to align with all full-screen formats.
  • Correlator features have been removed.
Class v19.5.0 API v20.0.0 API Notes
AdSize getPortraitBannerAdSizeWithWidth() getPortraitAnchoredAdaptiveBannerAdSize()
getLandscapeBannerAdSizeWithWidth() getLandscapeAnchoredAdaptiveBannerAdSize()
getCurrentOrientationBannerAdSizeWithWidth() getCurrentOrientationAnchoredAdaptiveBannerAdSize()
MobileAds initialize(Context, String) MobileAds.initialize(Context, OnInitializationCompleteListener) The app ID is now set in the AndroidManifest.xml.
initialize(Context, String, MobileAds.Settings) initialize(Context, OnInitializationCompleteListener) The Settings class has been deprecated.
getRewardedVideoAdInstance() Removed Use the RewardedAd API instead.
AdListener onAdFailedToLoad(int) onAdFailedToLoad(LoadAdError)
onAdLeftApplication() Removed Full-screen ad formats use FullscreenContentCallback in place of AdListener, and there is no equivalent method on FullscreenContentCallback. The method has been removed from AdListener.
VideoController getAspectRatio() MediaContent.getAspectRatio()
AdRequest getGender() Removed
getBirthday() Removed
getNetworkExtras() Removed
setManualImpressionsEnabled() Removed
updateCorrelator() Removed See Ad content filtering.
AdRequest.Builder setBirthday() Removed
setGender() Removed
setIsDesignedForFamilies() Removed See guide.
addTestDevice() RequestConfiguration.Builder.setTestDeviceIds() See Enabling test ads.
tagForChildDirectedTreatment() RequestConfiguration.Builder.setTagForChildDirectedTreatment() See Ad content filtering.
setTagForUnderAgeOfConsent() RequestConfiguration.Builder.setTagForUnderAgeOfConsent()
setMaxAdContentRating() RequestConfiguration.Builder.setMaxAdContentRating()
AdView getMediationAdapterClassName() ResponseInfo.getMediationAdapterClassName() A ResponseInfo object is now available via the AdView method getResponseInfo().
NativeAdOptions setImageOrientation() setMediaAspectRatio()
getImageOrientation getMediaAspectRatio()
RewardedAd loadAd(AdRequest, RewardedAdLoadCallback) RewardedAd.load(Context, String, AdRequest, RewardedAdLoadCallback) RewardedAd now uses the same static load approach as other full-screen formats.
loadAd(PublisherAdRequest, RewardedAdLoadCallback) RewardedAd.load(Context, String, AdManagerAdRequest, RewardedAdLoadCallback)
isLoaded() Removed The callback from the static load method provides an ad that is already loaded.
show(Activity, RewardedAdCallback) show(Activity, OnUserEarnedRewardListener) This show method adheres to the broader full-screen format approach.
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 now uses the same static load approach as other full-screen formats.
setAdListener()
DynamicHeightSearchAdRequest getNetworkExtras() Removed The NetworkExtras class has been deprecated.
AdLoader forContentAd() Removed
forAppInstallAd() Removed
withCorrelator() Removed
getMediationAdapterClassName() Removed