版本 6 迁移指南

其他平台

重要

请确保将您使用的 Yandex 聚合 适配器和 第三方聚合网络适配器 更新到最新版本。旧版本可能会导致适配器集成错误,从而导致您的广告无法投放。

手动初始化

手动 SDK 初始化流程已更改。现在,您需要在初始化应用时将回调传递给 MobileAds.initialize() 方法。

MobileAds.initialize(this) {
    // 现在您可以使用广告了
}
MobileAds.initialize(this, () -> {
    // 现在您可以使用广告了
});

横幅

将 AdSize 类重命名为 BannerAdSize。删除了 fixedSize(int width, int height)、flexibleSize(int width, int maxHeight) 和 stickySize(int width) 方法。

根据广告类型使用方法:

使用 BannerAdSize.stickySize(Context context, int width) 方法创建自适应粘性横幅。

这是一个自动更新的小广告,放置在应用屏幕的顶部或底部。

广告与主要应用内容不重叠,常用于游戏应用中。

粘性横幅的高度是自动确定的,可根据设备的屏幕尺寸调整,并且不占用超过屏幕高度的 15%。

重要

版本 6.0.0 添加了对自适应粘性横幅的自动刷新支持。

如果您之前已为粘性横幅实施了自动刷新,请禁用它。

要创建自适应内联横幅,请使用 BannerAdSize.inlineSize(Context context, int width, int maxHeight) 方法。

自适应内联横幅是一种灵活的横幅广告格式,通过优化每个设备上的广告尺寸,实现最大效率。

横幅的高度会自动调整,可能会达到设备屏幕的高度。

通常,这种格式在基于内容来源的应用或允许主要关注广告的上下文环境中使用。

为了向后兼容,我们添加了 fixedSize(Context context, int width, int height) 方法,以替代 fixedSize(int width, int height) 方法,但我们不建议使用该方法。请改用 inlineSize(Context context, int width, int maxHeight) 或 stickySize(Context context, int width)。在 SDK 6.0.0 中,横幅的高度是自动计算的,并且在发送广告请求之前已知。您可以在屏幕布局中使用此高度。

激励广告

我们改变了创建和加载广告的方法。我们现在提供 RewardedAdLoader 加载器对象,用于加载广告,以及从 RewardedAdLoadListener 加载器回调方法检索的 RewardedAd 广告对象。

要了解有关新 API 的更多信息,请参阅 SDK 参考。

广告加载

SDK 5

用于广告加载和呈现的单个对象:

val rewardedAd = RewardedAd(this@Activity).apply {
    setAdUnitId("your-ad-unit-id")
    setAdEventListener(this)
    loadAd(adRequest)
}
final RewardedAd rewardedAd = new RewardedAd(this);
rewardedAd.setAdUnitId("your-ad-unit-id");
rewardedAd.setAdEventListener(this);
rewardedAd.loadAd(adRequest);

SDK 6

用于加载多个广告的 RewardedAdLoader:您不需要传递活动上下文来加载广告。

广告加载后(通过调用 onAdLoaded(rewardedAd: RewardedAd) 方法),在展示完成之前保存加载的 RewardedAd 的链接。

val loader = RewardedAdLoader(context).apply {
    setAdLoadListener(object : RewardedAdLoadListener {
        override fun onAdLoaded(rewardedAd: RewardedAd) {
            // 将 rewardedAd 保存到本地变量
            // 现在 rewardedAd 已准备好展示
        }

        override fun onAdFailedToLoad(adRequestError: AdRequestError) {}
    })
}
loader.loadAd(AdRequestConfiguration.Builder("your-ad-unit-id").build())
final RewardedAdLoader loader = new RewardedAdLoader(this);
loader.setAdLoadListener(new RewardedAdLoadListener() {
    @Override
    public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
        // 将 rewardedAd 保存到本地变量
        // 现在 rewardedAd 已准备好展示
    }

    @Override
    public void onAdFailedToLoad(@NonNull AdRequestError adRequestError) {}
});
loader.loadAd(new AdRequestConfiguration.Builder("your-ad-unit-id").build());

广告呈现

SDK 5

在展示广告之前,请查看广告是否已加载。

if (rewardedAd.isLoaded()) {
    rewardedAd.show()
}
if (rewardedAd.isLoaded()) {
    rewardedAd.show();
}

SDK 6

您无需检查广告是否已加载。

广告加载器的 onAdLoaded 回调方法返回 RewardedAd 实例后,广告就准备好呈现了。

要呈现广告,您需要传递活动:

rewardedAd.show(activity)
rewardedAd.show(activity);

RewardedAd 广告对象

SDK 5

结合广告加载和呈现的类。

SDK 6

已加载广告的界面。添加了 getInfo() 方法来获取有关广告的信息。

interface RewardedAd {
    fun setAdEventListener(rewardedAdEventListener: RewardedAdEventListener)

    fun show(activity: Activity)

    fun getInfo(): AdInfo
}
interface RewardedAd {
    void setAdEventListener(@Nullable RewardedAdEventListener rewardedAdEventListener);

    void show(@NonNull Activity activity);

    @NonNull
    AdInfo getInfo();
}

回调方法

SDK 5

结合事件加载和呈现的单一回调方法接口:

interface RewardedAdEventListener {

    fun onLoaded()

    fun onRewarded(reward: Reward)

    fun onAdFailedToLoad(adRequestError: AdRequestError)

    fun onAdShown()

    fun onAdDismissed()

    fun onLeftApplication()

    fun onReturnedToApplication()
}
interface RewardedAdEventListener {

    public void onLoaded();

    public void onRewarded(@NonNull Reward reward);

    public void onAdFailedToLoad(@NonNull AdRequestError adRequestError);

    public void onAdShown();

    public void onAdDismissed();

    public void onLeftApplication();

    public void onReturnedToApplication();
}

SDK 6

RewardedAdEventListener 接口分为两部分:加载 RewardedAdLoadListener 和呈现 RewardedAdEventListener 的回调方法。

删除了 onLeftApplication() 和 onReturnedToApplication() 方法。

添加了 onAdFailedToShow(AdError) 方法。

interface RewardedAdLoadListener {
    fun onAdLoaded(rewardedAd: RewardedAd)

    fun onAdFailedToLoad(adRequestError: AdRequestError)
}

interface RewardedAdEventListener {
    fun onAdShown()

    fun onAdFailedToShow(adError: AdError)

    fun onAdDismissed()

    fun onAdClicked()

    fun onAdImpression(impressionData: ImpressionData)

    fun onRewarded(reward: Reward)
}
interface RewardedAdLoadListener {
    void onAdLoaded(@NonNull RewardedAd rewardedAd);

    void onAdFailedToLoad(@NonNull AdRequestError adRequestError);
}

interface RewardedAdEventListener {
    void onAdShown();

    void onAdFailedToShow(@NonNull AdError adError);

    void onAdDismissed();

    void onAdClicked();

    void onAdImpression(@Nullable ImpressionData impressionData);

    void onRewarded(@NonNull Reward reward);
}

清理 RewardedAd 广告对象

SDK 5

调用 onDestroy() 方法并停止保留广告对象引用。

SDK 6

onDestroy() 方法已被移除。

要清理广告对象,请将 null传递给为 rewardedAd.setAdEventListener 回调方法设置监听器的函数,并停止保留广告对象引用:

private fun destroyRewardedAd() {
    rewardedAd?.setAdEventListener(null)
    rewardedAd = null
}
private void destroyRewardedAd() {
    if (mRewardedAd != null) {
        mRewardedAd.setAdEventListener(null);
        mRewardedAd = null;
    }
}

插屏广告

我们改变了创建和加载广告的方法。我们现在提供 InterstitialAdLoader 加载器对象,用于加载广告,以及从 InterstitialAdLoadListener 加载器回调方法检索的 InterstitialAd 广告对象。

有关新 API 的更多详细信息,请参阅 SDK 参考。

广告加载

SDK 5

用于广告加载和呈现的单个对象:

val interstitialAd = InterstitialAd(this@Activity).apply {
setAdUnitId("your-ad-unit-id")
setAdEventListener(this)
loadAd(adRequest)
}
final InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("your-ad-unit-id");
interstitialAd.setAdEventListener(this);
interstitialAd.loadAd(adRequest);

SDK 6

用于加载多个广告的 InterstitialAdLoader。您不需要传递活动上下文来加载广告。

广告加载后(通过调用 onAdLoaded(interstitialAd: InterstitialAd)方法),在展示完成之前保存加载的 InterstitialAd 的链接。

val loader = InterstitialAdLoader(context).apply {
    setAdLoadListener(object : InterstitialAdLoadListener {
        override fun onAdLoaded(interstitialAd: InterstitialAd) {
            // 将 interstitialAd 保存到本地变量
            // 现在 interstitialAd 已准备好展示
        }

        override fun onAdFailedToLoad(adRequestError: AdRequestError) {}
    })
}
loader.loadAd(AdRequestConfiguration.Builder("your-ad-unit-id").build())
final InterstitialAdLoader loader = new InterstitialAdLoader(this);
loader.setAdLoadListener(new InterstitialAdLoadListener() {
    @Override
    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        // 将 interstitialAd 保存到本地变量
        // 现在 interstitialAd 已准备好展示
    }

    @Override
    public void onAdFailedToLoad(@NonNull AdRequestError adRequestError) {}
});
loader.loadAd(new AdRequestConfiguration.Builder("your-ad-unit-id").build());

广告呈现

SDK 5

if (interstitialAd.isLoaded()) {
    interstitialAd.show()
}
if (interstitialAd.isLoaded()) {
    interstitialAd.show();
}

SDK 6

您无需检查广告是否已加载。

广告加载器的 onAdLoaded 回调方法返回 InterstitialAd 实例后,广告就准备好呈现了。

要呈现广告,您需要传递活动:

interstitialAd.show(activity)
interstitialAd.show(activity);

InterstitialAd 广告对象

SDK 5

结合了广告加载和呈现的类。

SDK 6

已加载广告的界面。添加了 getInfo() 方法来获取有关广告的信息。

interface InterstitialAd {
    fun setAdEventListener(interstitialAdEventListener: InterstitialAdEventListener)

    fun show(activity: Activity)

    fun getInfo(): AdInfo
}
interface InterstitialAd {
    void setAdEventListener(@Nullable InterstitialAdEventListener interstitialAdEventListener);

    void show(@NonNull Activity activity);

    @NonNull
    AdInfo getInfo();
}

回调方法

SDK 5

结合事件加载和呈现的单一回调方法接口:

interface InterstitialAdEventListener {

    fun onLoaded()

    fun onAdFailedToLoad(adRequestError: AdRequestError)

    fun onAdShown()

    fun onAdDismissed()

    fun onLeftApplication()

    fun onReturnedToApplication()
}
interface InterstitialAdEventListener {

    public void onLoaded();

    public void onAdFailedToLoad(@NonNull AdRequestError adRequestError);

    public void onAdShown();

    public void onAdDismissed();

    public void onLeftApplication();

    public void onReturnedToApplication();
}

SDK 6

InterstitialAdEventListener 接口分为两部分:InterstitialAdLoadListener的加载回调方法和InterstitialAdEventListener` 的呈现方法。

删除了 onLeftApplication() 和 onReturnedToApplication() 方法。

添加了 onAdFailedToShow(AdError) 方法。

interface InterstitialAdLoadListener {
    fun onAdLoaded(interstitialAd: InterstitialAd)

    fun onAdFailedToLoad(adRequestError: AdRequestError)
}

interface InterstitialAdEventListener {
    fun onAdShown()

    fun onAdFailedToShow(adError: AdError)

    fun onAdDismissed()

    fun onAdClicked()

    fun onAdImpression(impressionData: ImpressionData)
}
interface InterstitialAdLoadListener {
    void onAdLoaded(@NonNull InterstitialAd interstitialAd);

    void onAdFailedToLoad(@NonNull AdRequestError adRequestError);
}

interface InterstitialAdEventListener {
    void onAdShown();

    void onAdFailedToShow(@NonNull AdError adError);

    void onAdDismissed();

    void onAdClicked();

    void onAdImpression(@Nullable ImpressionData impressionData);
}

清理 InterstitialAd 广告对象

SDK 5

调用 onDestroy() 方法并停止保留广告对象引用。

SDK 6

onDestroy() 方法已被移除。

要清理广告对象,请将 null 传递给为 interstitialAd.setAdEventListener 回调方法设置监听器的函数,并停止保留广告对象引用:

private fun destroyInterstitialAd() {
    interstitialAd?.setAdEventListener(null)
    interstitialAd = null
}
private void destroyInterstitialAd() {
    if (mInterstitialAd != null) {
        mInterstitialAd.setAdEventListener(null);
        mInterstitialAd = null;
    }
}

开屏广告

添加了新的广告格式:开屏广告。如需了解更多信息,请参阅 开屏广告。

Yandex 聚合

重要

请确保将您使用的 Yandex 聚合 适配器和 第三方聚合网络适配器 更新到最新版本。旧版本可能会导致适配器集成错误,从而导致您的广告无法投放。

新适配器版本

Yandex 聚合适配器的当前版本(截至 SDK 6.0.1 版本起):

"com.yandex.ads.mediation:mobileads-adcolony": "4.8.0.6",
"com.yandex.ads.mediation:mobileads-applovin": "11.11.2.0",
"com.yandex.ads.mediation:mobileads-chartboost": "9.3.1.0",
"com.yandex.ads.mediation:mobileads-google": "22.2.0.0",
"com.yandex.ads.mediation:mobileads-inmobi": "10.5.5.0",
"com.yandex.ads.mediation:mobileads-ironsource": "7.4.0.0",
"com.yandex.ads.mediation:mobileads-mintegral": "16.4.71.1",
"com.yandex.ads.mediation:mobileads-mytarget": "5.18.0.0",
"com.yandex.ads.mediation:mobileads-pangle": "5.3.0.4.1",
"com.yandex.ads.mediation:mobileads-startapp": "4.11.0.2",
"com.yandex.ads.mediation:mobileads-tapjoy": "13.1.2.0",
"com.yandex.ads.mediation:mobileads-unityads": "4.8.0.0",
"com.yandex.ads.mediation:mobileads-vungle": "6.12.1.3",

适用于第三方广告聚合平台的适配器的当前版本(截至 SDK 6.0.1 版本):

"com.yandex.ads.adapter:admob-mobileads": "6.0.1.0",
"com.yandex.ads.adapter:ironsource-mobileads": "6.0.1.0"

重命名 Google AdMob 适配器

将 com.yandex.ads.mediation:mobileads-admob 构件重命名为 com.yandex.ads.mediation:mobileads-google。

如果您使用标准化的 Yandex 聚合构建,则无需执行任何操作。如果您手动添加适配器,请替换项目中的 gradle 依赖项。

SDK 5

implementation 'com.yandex.ads.mediation:mobileads-admob:<version>'

SDK 6

implementation 'com.yandex.ads.mediation:mobileads-google:<version>'

您可以在此处查找完整的集成示例: