SDK 遷移

本頁說明目前與先前版本的遷移作業。

從 v22 遷移至 v23

最低 Android API 級別為 21

自 23.0.0 版起,Google Mobile Ads SDK 要求所有應用程式都必須在 Android API 級別 21 以上的執行,才能執行。如要調整 API 級別,請將應用程式層級 build.gradle 檔案中的 minSdkVersion 值變更為 21 以上。

從父類別繼承的 AdManagerAdRequest.Builder 方法可以鏈結

在 23.0.0 版中,從父項沿用的 AdManagerAdRequest.Builder 方法可鏈結在一起,使用單一呼叫建構 AdManagerAdRequest

Java

AdManagerAdRequest request = new AdManagerAdRequest.Builder()
    .addCustomTargeting("age", "25") // AdManagerAdRequest.Builder method.
    .setContentUrl("https://www.example.com") // Method inherited from parent.
    .build(); // Builds an AdManagerAdRequest.

Kotlin

var request = AdManagerAdRequest.Builder()
  .addCustomTargeting("age", "25") // AdManagerAdRequest.Builder method.
  .setContentUrl("https://www.example.com") // Method inherited from parent.
  .build() // Builds an AdManagerAdRequest.

已移除/取代已淘汰的方法

22.0.0 版 API 23.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 Mobile Ads SDK 版本

MobileAds.getVersionString() 方法已在 22.0.0 版中移除,並改用 MobileAds.getVersion()

新方法會傳回預期的外部版本號碼,例如 22.0.0。如要進一步瞭解這項變更,請參閱「使用新的 Google Mobile Ads SDK getVersion() 方法」。

移除已淘汰的 NativeCustomFormatAd.getVideoMediaView()

第 21 版中,Google Mobile Ads SDK 提供了 NativeCustomFormatAd.getVideoMediaView() 方法,讓您取得原生自訂廣告格式的媒體素材資源。

版本 22 開始,您可以建立自己的 MediaView 並設定其媒體內容,並使用 MediaContent.getVideoController() 取得影片控制項。

v22

// Called when a custom native ad loads.
@Override
public void onCustomFormatAdLoaded(NativeCustomFormatAd ad) {
    // Assumes you have a FrameLayout in your view hierarchy with the id media_placeholder.
    FrameLayout mediaPlaceholder = (FrameLayout) findViewById(R.id.media_placeholder);

    MediaContent mediaContent = ad.getMediaContent();
    if (mediaContent != null && mediaContent.hasVideoContent()) {
        // Create a MediaView and set its media content.
        MediaView mediaView = new MediaView(mediaPlaceholder.getContext());
        mediaView.setMediaContent(mediaContent);
        mediaPlaceholder.addView(mediaView);
    }
}

v21

// Called when a custom native ad loads.
@Override
public void onCustomFormatAdLoaded(NativeCustomFormatAd ad) {
    // Assumes you have a FrameLayout in your view hierarchy with the id media_placeholder.
    FrameLayout mediaPlaceholder = (FrameLayout) findViewById(R.id.media_placeholder);

    VideoController videoController = ad.getVideoController();
    if (videoController.hasVideoContent()) {
        // Add the media view provided by the native ad.
        mediaPlaceholder.addView(ad.getVideoMediaView());
    }
}

詳情請參閱自訂原生廣告指南。

移除或取代的方法

下表列出了 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 Adapter
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 Mobile Ads SDK 要求所有應用程式都必須至少 Android API 級別 19 才能執行。如要調整 API 級別,請將應用程式建構檔案中的 minSdkVersion 值變更為 19 以上。

強制執行嚴格是否可為空值

為了強制執行嚴格是否可為空值特性,@NonNull 註解會新增至先前未明確定義是否可為空值的所有 API。

如果發生空值安全性違規,且先前並未以安全的方式處理 null 值,這項變更可能會破壞使用 Java 空值檢查架構的 Kotlin 應用程式和應用程式 (請參閱 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 向第三方廣告聯播網提供資訊。

淘汰自訂事件介面

自訂事件可讓發布商使用中介服務 設定刊登序列廣告來源,但該來源不是 Ad Manager支援的廣告來源

所有自訂事件介面皆已淘汰,並改用 Adapter 類別和 MediationAdLoadCallback 介面。

下表列出自 21.0.0 版起,每個自訂事件介面應使用的對應類別或介面:

20.0.0 版介面 v21.0.0 版類別/ 介面
CustomEvent Adapter
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 都會遵循下列原則:

移除舊版 獎勵 VideoAd API

新版 RewardedAd API 是在 2019 年 3 月首次推出,一直是 18 個月以上的首選獎勵 API。與舊版 RewardedVideoAd API 相比,體驗更多功能,包括一次載入多個獎勵廣告。

舊版 RewardedVideoAd API 已從 SDK 20.0.0 版中移除。

淘汰智慧型橫幅廣告,改用自動調整橫幅廣告

智慧型橫幅廣告廣告已淘汰,並改用自動調整橫幅廣告。自動調整橫幅廣告能提供優異的效能,彈性地設定廣告寬度。如果您偏好繼續使用全寬橫幅廣告,仍然可以使用自動調整橫幅廣告來進行,如以下程式碼片段所示:

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。使用 OS 層級 API 時,無論使用者是否因為與廣告互動而離開應用程式,系統都會通知您。

請注意,onAdLeftApplication 回呼並非刻意為廣告點擊處理常式,且仰賴此回呼回報點擊並未產生準確的指標。舉例來說,當使用者點擊啟動外部瀏覽器的 AdChoices 圖示後,系統會叫用回呼,但不計為一次點擊。

重新命名類別

下表列出已變更或移除的特定類別名稱。簡單來說:

  • 大多數與 UnifiedNativeAd 相關的類別已重新命名為 NativeAd
  • 已移除 MobileAds.SettingsNativeExpressAdViewNativeAppInstallAdNativeContentAdInstreamAd 類別和檢視畫面。
  • 所有包含 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 response.獎勵 AdLoadCallback 和 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
format.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 類別。
  • 更新 獎勵 Ad 和 插頁式廣告 API,以符合所有全螢幕格式。
  • 已移除 Correlator 功能。
類別 19.5.0 版 API API 20.0.0 版 附註
AdSize getPortraitBannerAdSizeWithWidth() getPortraitAnchoredAdaptiveBannerAdSize()
getLandscapeBannerAdSizeWithWidth() getLandscapeAnchoredAdaptiveBannerAdSize()
getCurrentOrientationBannerAdSizeWithWidth() getCurrentOrientationAnchoredAdaptiveBannerAdSize()
MobileAds Initial(Context, String) MobileAds.initialize(Context, OnInitializationCompleteListener) 應用程式 ID 現在是在 AndroidManifest.xml 中設定。
Initial(Context, String, MobileAds.Settings) Initial(Context, OnInitializationCompleteListener) Settings 類別已淘汰。
getPromoVideoAdInstance() 已移除 請改用 獎勵 Ad API。
AdListener onAdFailedToLoad(int) onAdFailedToLoad(LoadAdError)
onAdLeftApplication() 已移除 全螢幕廣告格式使用 FullscreenContentCallback 來取代 AdListener,而 FullscreenContentCallback 中沒有對等的方法。此方法已從 AdListener 中移除。
VideoController getAspectRatio() MediaContent.getAspectRatio()
PublisherAdRequest getGender() 已移除
getBirthday() 已移除
getNetworkExtras() 已移除
setManualImpressionsEnabled() 已移除
updateCorrelator() 已移除 請參閱「廣告素材篩選」一文。
PublisherAdRequest.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, 獎勵 AdLoadCallback) 獎勵 Ad.load(Context, String, AdRequest, 獎勵 AdLoadCallback) 獎勵廣告現在採用與其他全螢幕格式相同的靜態載入方法。
loadAd(PublisherAdRequest, 獎勵 AdLoadCallback) 獎勵 Ad.load(Context, String, AdManagerAdRequest, 獎勵 AdLoadCallback)
isLoaded() 已移除 靜態載入方法的回呼會提供已載入的廣告。
show(Activity, 獎勵 AdCallback) show(Activity, OnUserMonetizeedRewardListener) 這個顯示方法遵循更寬的全螢幕格式方法。
RewardedAdLoadCallback on 獎勵 AdFailedToLoad(int) onAdFailedToLoad(LoadAdError)
on 獎勵 AdFailedToLoad(LoadAdError) onAdFailedToLoad(LoadAdError)
onRewardedAdLoaded() onAdLoaded(RewardedAd)
AppOpenAdLoadCallback onAppOpenAdFailedToLoad(int) onAdFailedToLoad(LoadAdError)
onAppOpenAdFailedToLoad(LoadAdError) onAdFailedToLoad(LoadAdError)
onAppOpenAd已載入(AppOpenAd) onAdLoaded(AppOpenAd)
獎勵插頁式 AdLoad回呼 on 獎勵插頁式廣告FailedToLoad(int) onAdFailedToLoad(LoadAdError)
on 獎勵插頁式廣告FailedToLoad(LoadAdError) onAdFailedToLoad(LoadAdError)
on 獎勵插頁式 AdLoaded(獎勵插頁式 Ad) onAdLoaded(獎勵插頁式廣告)
PublisherInterstitialAd 新 PublisherInterstitialAd() 導入插頁式廣告 插頁式廣告現在採用與其他全螢幕格式相同的靜態載入方式。
setAdListener()
DynamicHeightSearchAd請求 getNetworkExtras() 已移除 NetworkExtras 類別已淘汰。
AdLoader forContentAd() 已移除
forAppInstallAd() 已移除
withCorrelator() 已移除
getMediationAdapterClassName() 已移除