恢复 AdMob 奖励视频广告时崩溃

Crash when resume an AdMob RewardedVideoAd

使用最新的 AdMob 版本 15.0.0,我在尝试从另一个 Activity.

恢复 RewardedVideoAdafter return 时遇到崩溃
 java.lang.ClassCastException: com.google.ads.mediation.admob.AdMobAdapter cannot be cast to com.google.android.gms.ads.mediation.m
     at com.google.android.gms.ads.internal.mediation.client.y.a(:com.google.android.gms.dynamite_dynamitemodulesa@12529004@12.5.29 (000308-192802242):262)
     at com.google.android.gms.ads.internal.reward.c.b(:com.google.android.gms.dynamite_dynamitemodulesa@12529004@12.5.29 (000308-192802242):54)
     at com.google.android.gms.ads.internal.reward.client.e.onTransact(:com.google.android.gms.dynamite_dynamitemodulesa@12529004@12.5.29 (000308-192802242):57)
     at android.os.Binder.transact(Binder.java:361)
     at com.google.android.gms.internal.zzev.zzb(Unknown Source)
     at com.google.android.gms.internal.zzaez.zzc(Unknown Source)
     at com.google.android.gms.internal.zzafk.resume(Unknown Source)
     at com.byteghoul.grimdefender.GhoulAdsAndroid3.onResume(GhoulAdsAndroid3.java:411)
     at com.byteghoul.grimdefender.AndroidLauncher.onResume(AndroidLauncher.java:437)
     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1198)
     at android.app.Activity.performResume(Activity.java:5530)
     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3047)
     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3086)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5586)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
     at dalvik.system.NativeStart.main(Native Method)

集成似乎没问题,我只是按照 AdMob 文档做任何奇怪的事情。

我检查了 Google AdMob github 样本,我得到了同样的错误。

有关更多信息,这是我的 RewardedVideo 包装器:

class VideoAd @Inject constructor(@Named(BaseActivity.BaseModule.ACTIVITY_CONTEXT) private val context: Context) {

private var isAlreadyPrepared: Boolean = false
private lateinit var rewardedVideoAd: RewardedVideoAd
private val customListener: CustomVideoListener = CustomVideoListener()
private var adsListener: AdsListener = object : AdsListener {}
val isVideoLoaded: Boolean
    get() = rewardedVideoAd.isLoaded

fun prepare(listener: AdsListener) {
    adsListener = listener
    if (isAlreadyPrepared) {
        return
    }
    rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(context)
    rewardedVideoAd.rewardedVideoAdListener = customListener
    val adRequest: AdRequest = AdRequest.Builder()
        .addTestDevice(ADS_ID_TEST_DEVICE)
        .addTestDevice(ADS_ID_TEST_DEVICE_2)
        .build()
    rewardedVideoAd.loadAd(VIDEO_ADS_ID, adRequest)
    isAlreadyPrepared = true
}

fun show() {
    if (isVideoLoaded) {
        rewardedVideoAd.show()
    } else {
        logNonFatalException("Video Ad not loaded successfully before show it")
        adsListener.onVideoAdNotLoaded()
    }
}

fun onPauseActivity() {
    rewardedVideoAd.pause(context)
}

fun onResumeActivity() {
    rewardedVideoAd.resume(context)
}

fun onDestroyActivity() {
    rewardedVideoAd.destroy(context)
}

private inner class CustomVideoListener : RewardedVideoAdListener {
    override fun onRewardedVideoAdClosed() {
        adsListener.onAdsFinishedSuccessfully()
    }

    override fun onRewardedVideoAdLeftApplication() {
    }

    override fun onRewardedVideoAdLoaded() {
    }

    override fun onRewardedVideoAdOpened() {
    }

    override fun onRewardedVideoCompleted() {
    }

    override fun onRewarded(reward: RewardItem?) {
        adsListener.onAdsFinishedSuccessfully()
    }

    override fun onRewardedVideoStarted() {
    }

    override fun onRewardedVideoAdFailedToLoad(p0: Int) {
    }
}

}

在google bug tracking里搜索好像是15.0.0版本的bug:

https://groups.google.com/forum/#!topic/google-admob-ads-sdk/XtVZFQ-nl98

可能的解决方案是:

  • 将版本降级到以前的稳定版本。
  • 等到 google 启动修复程序。

我遇到了同样的问题,我将 AdMob 版本 15.0.0 更改为 15.0.1,效果很好。