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 行動服務 廣告 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 變壓器
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 (先前未明確定義是否可為空值)。

這項變更可能會導致 Kotlin 應用程式和使用 Java 空值檢查的應用程式無法運作 出現空值違反安全問題且先前未處理的架構 如何保護 null 值的安全 (請參閱 空值安全 )。

移除 addNetworkExtras() 方法

addNetworkExtras() 方法可將額外參數做為 SDK 中已淘汰指定廣告聯播網轉接程式的 NetworkExtras 執行個體 20.3.0 版,並將在 21.0.0 版中移除。使用 addNetworkExtrasBundle()敬上 方法來自相同的 AdRequest.Builder 類別。

移除定位方式

21.0.0 版將移除下列定位方法:

  • AdRequest.Builder 類別中的 setLocation(Location location) 方法 設定使用者的位置,以便進行中介服務指定目標。
  • AdRequest 類別中的 getLocation() 方法,會傳回使用者的 先前傳遞至 setLocation(Location location) 方法的指定地區資訊。
  • MediationAdConfiguration 類別中的 getLocation() 方法,該方法會指定 如果 AdRequest 定義了使用者的位置,則會傳回使用者的位置。

Google 不會使用位置資料指定廣告目標。應使用 用來向第三方廣告聯播網提供資訊的第三方 API 這通常代表交易 不會十分要求關聯語意

淘汰自訂事件介面

自訂事件可讓發布商 中介服務 來設定未放送刊登序列廣告來源 以下任一項: Ad Manager 支援的廣告來源

所有自訂 活動 介面已淘汰,並改用 Adapter敬上 類別和 MediationAdLoadCallback 存取 API

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

20.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 都會遵循下列原則:

移除舊版 獎勵 VideoAd API

更新 RewardedAd敬上 API 是在 2019 年 3 月首次推出,也是使用者首選的獎勵廣告 擁有超過 18 個月。強化項目比舊版更多 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 回呼的用途並非廣告 並且依賴這個回呼回報點擊,因此不會產生 準確的指標舉例來說,使用者按一下啟動 外部瀏覽器叫用回呼,但未計為一次點擊。

重新命名類別

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

  • 大多數與 UnifiedNativeAd 相關的類別已重新命名為 NativeAd
  • MobileAds.SettingsNativeExpressAdViewNativeAppInstallAd、 已移除 NativeContentAdInstreamAd 個類別和檢視畫面。
  • 所有包含 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
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 類別
  • 獎勵廣告 API 和 插頁式廣告 API 已更新,與所有 API 保持一致 全螢幕格式
  • 已移除 Correlator 功能。
類別 19.5.0 版 API API 20.0.0 版 附註
AdSize getPortraitBannerAdSizeWithWidth() getPortraitAnchoredAdaptiveBannerAdSize()
getLandscapeBannerAdSizeWithWidth() getLandscapeAnchoredAdaptiveBannerAdSize()
getCurrentOrientationBannerAdSizeWithWidth() getCurrentOrientationAnchoredAdaptiveBannerAdSize()
MobileAds Initial(情境, 字串) MobileAds.initialize(Context, OnInitializationCompleteListener) 應用程式 ID 現在是在 AndroidManifest.xml 中設定。
Initial(Context, String, MobileAds.Settings) Initial(Context, OnInitializationCompleteListener) Settings 類別已淘汰。
getRewardedVideoAdInstance() 已移除 請改用 獎勵 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(活動, 獎勵 AdCallback) show(Activity、OnUserMonetizeedRewardListener) 這種顯示方法遵循更寬的全螢幕格式方法。
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)
PublisherInterstitialAd new PublisherInterstitialAd() 導入插頁式廣告 插頁式廣告現在使用與 則為其他全螢幕格式
setAdListener()
DynamicHeightSearchAdRequest getNetworkExtras() 已移除 NetworkExtras 類別已淘汰。
AdLoader forContentAd() 已移除
forAppInstallAd() 已移除
withCorrelator() 已移除
getMediationAdapterClassName() 已移除