Jetpack Compose Admob 广告不显示问题排查

文章目录

    尝试在 Jetpack Compose 中添加 Admob 广告,参考的是下面链接里的第一种方案

    https://stackoverflow.com/questions/68855709/how-to-show-a-banner-ad-in-jetpack-compose

    但是,无论是在 Android 模拟器里,还是在真机上调试,都无法显示广告。

    日志

    2022-01-04 22:37:55.551 6242-6353/com.sunzhongwei.shelflife E/chromium: [ERROR:cookie_manager.cc(137)] Strict Secure Cookie policy does not allow setting a secure cookie for http://googleads.g.doubleclick.net/ for apps targeting >= R. Please either use the 'https:' scheme for this URL or omit the 'Secure' directive in the cookie value.
    2022-01-04 22:37:55.554 6242-6242/com.sunzhongwei.shelflife I/Ads: Ad failed to load : 3
    2022-01-04 22:38:55.557 6242-6242/com.sunzhongwei.shelflife I/Ads: This request is sent from a test device.
    

    只能针对日志里的错误提示,逐一排查。

    Strict Secure Cookie policy does not allow setting a secure cookie for

    开始以为是系统版本的问题,试了几个 Android 版本都有这个错误。

    于是,我用之前 XML layout 的项目测试了一下,虽然同样有这个错误,但是 test 广告是可以正常展示的。说明这个错误与广告是否展示无关。大概率是 jetpack compose 的问题。

    Ads: Ad failed to load : 3

    通过错误码来判断是一个比较严谨的做法。这个错误码 3,代表代码正确,但是没有足够的广告填充。

    然后,我故意将 unit ad 写错,返回的错误码则是:

    I/Ads: Ad failed to load : 0

    至此,我没有思路了,而且不想再浪费时间继续排查下去了。

    换个实现思路

    我觉得第一个链接里的,第二种方案可能是更理想的方案。即外层框架继续使用 XML layout,整个界面分两部分:

    • 一部分是 compose 写的界面
    • 一部分是 xml admob 组件

    即,不在 jetpack compose 中使用 admob,还是在原有的 xml 中使用。我觉得这个方案可以避开 compose 的坑。

    activity_main.xml

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.screens.MainActivity">
        
        <androidx.compose.ui.platform.ComposeView
            android:id="@+id/composeView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/adView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            app:adSize="BANNER"
            app:adUnitId="ca-app-pub-xxxxxxxxxxxxx/xxxxxxxxxx"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    为何别人可以正常展示

    这个视频里看,compose 和 admob 是可以完美结合的。我也不想深究具体哪里出了问题了。

    https://www.youtube.com/watch?v=YkoEe6YZvq0

    里面通过菜单快捷键初始化 Admob,涨知识了。

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式