androidxmlkotlinandroid-jetpack-composeadmob

Unable to show Admob Native Ad in Jetpack Compose


I am trying to show admob native ad using the xml format and AndroidViewBinding in jetpack compose. Ad does not show at all and but I do not get any useful error message.

Inside onAdFailedToLoad I just get "Internal error." exception.

And for XML itself, when I validate the file I get "Error:(5, 42) cvc-elt.1.a: Cannot find the declaration of element 'com.google.android.gms.ads.nativead.NativeAdView'." .

I enabled viewBinding inside build.gradle file. All necessary dependencies have been added.

This is composable function:

@Composable
fun NativeAdView(adUnitId: String) {
    Box(modifier = Modifier.defaultMinSize(minHeight = 200.dp)) {
        AndroidViewBinding(
            factory = { inflater, parent, attachToParent ->
                val binding = NativeAdViewBinding.inflate(inflater, parent, attachToParent)
                val adView = binding.root.also { adView ->
                    adView.headlineView = binding.adHeadline
                    adView.iconView = binding.adAppIcon
                }
                try {
                    val adLoader = AdLoader.Builder(
                        adView.context,
                        adUnitId,
                    )
                        .forNativeAd { nativeAd ->
                            nativeAd.icon?.let {
                                binding.adAppIcon.setImageDrawable(it.drawable)
                                binding.adAppIcon.isVisible = true
                            }
                            nativeAd.headline?.let {
                                if (it.isNotBlank()) {
                                    binding.adHeadline.text = it
                                    binding.adHeadline.isVisible = true
                                }
                            }

                            adView.setNativeAd(nativeAd)
                        }
                        .withAdListener(
                            object : AdListener() {
                                override fun onAdFailedToLoad(error: LoadAdError) {
                                    super.onAdFailedToLoad(error)

                                }
                            },
                        )
                        .withNativeAdOptions(NativeAdOptions.Builder().build())
                        .build()
                    adLoader.loadAd(AdRequest.Builder().build())
                } catch (e: Exception) {
                    Log.e("Exception", e.message.toString())
                }
                binding
            },
        )
    }
}

This is UI:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test ad" />

        <ImageView
            android:id="@+id/ad_app_icon"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:adjustViewBounds="true"
            android:contentDescription="ad_icon" />

        <TextView
            android:id="@+id/ad_headline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp" />
    </LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
 

Solution

  • Few minutes ago i was solving same issue. I am still in development, didn't tested it in production, but i was able to load native ads. For me this worked:

    1. Make sure you added metadata APPLICATION_ID

        <manifest>
          <application>
          <!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
          <meta-data
              android:name="com.google.android.gms.ads.APPLICATION_ID"
              android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
              ...
    
          </application>
        </manifest>
    

    2. Make sure ads sdk is initialized before loading any ad

       MobileAds.initialize(context) {
            //Now you can load ad
            adLoader.loadAd(AdRequest.Builder().build())
        }
    

    3. Use "/6499/example/native" adUnitId

    Now i can't really explain why but for debugging it's working with this ad unit id "/6499/example/native". I found it in this example project, it contains working implementation how to load and display native ad.