Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

GMA SDK android Never renders Ad for react native

13 views
Skip to first unread message

Anchal Gupta

unread,
May 5, 2025, 6:54:57 PMMay 5
to Google Mobile Ads SDK Developers

Hi team,

I’m working on a custom integration of the Google Mobile Ads SDK (GMA SDK) into a React Native application (without using any third-party wrapper like react-native-google-mobile-ads). While integrating the SDK for Android, I’ve encountered a blocker where the ad successfully loads, but never renders on the UI.

Details:

  • I have confirmed that:

    • MobileAds.initialize(context) is called.

    • The ad load completes successfully (onAdLoaded() is triggered).

    • Ad response, size, and dimensions are logged correctly.

    • There are no errors in the logs (like onAdFailedToLoad()).

    • The view being used to host the ad is valid and renders correctly with text/images.

  • However, the actual ad content never appears on the screen.

To further debug:

  • I created a fresh React Native app.

  • Added a minimal native module with a BannerAdView (via SimpleViewManager).

  • Used the same logic that works perfectly in a standalone native Android app.

  • Still, ads load but do not render only when the app is built using React Native as the entry point.

Environment:

  • React Native version: Tested on 0.73.0 and 0.79.1

  • AGP: 8.8.2

  • JavaScript side uses a Native UI Component exposed via a ViewManager.

What I’ve tried:

  • Rendering static elements (text/image) inside the same native view: ✅ works fine.

  • Validated view dimensions and layout: ✅ correct.

  • Isolated the GMA logic to a native-only app: ✅ renders correctly

    BannerAd.kt file - 

    ```

    package com.bannerbridge

    import android.content.Context
    import android.widget.FrameLayout
    import android.view.ViewGroup.LayoutParams
    import com.facebook.react.bridge.ReactContext
    import com.facebook.react.uimanager.SimpleViewManager
    import com.facebook.react.uimanager.ThemedReactContext
    import com.facebook.react.uimanager.annotations.ReactProp
    import com.google.android.gms.ads.AdRequest
    import com.google.android.gms.ads.AdSize
    import com.google.android.gms.ads.AdView
    import com.google.android.gms.ads.AdListener
    import com.google.android.gms.ads.LoadAdError
    import com.google.android.gms.ads.MobileAds
    import android.util.Log
    import android.view.Gravity
    import android.graphics.Color
    import android.util.DisplayMetrics

    class BannerAd(context: Context) : FrameLayout(context) {
    private var adView: AdView? = null
    companion object {
    private const val TAG = "BannerAd"
    private const val TEST_BANNER_ID = "ca-app-pub-3940256099942544/6300978111"
    }
    init {
    Log.d(TAG, "Initializing BannerAd")
    setupBannerAd()
    }

    private fun setupBannerAd() {
    Log.d(TAG, "Setting up banner ad")
    MobileAds.initialize(context) { initializationStatus ->
    val statusMap = initializationStatus.adapterStatusMap
    statusMap.forEach { (adapter, status) ->
    Log.d(TAG, "Adapter: $adapter Status: ${status.initializationState}")
    }
    Log.d(TAG, "Mobile Ads SDK initialized, creating AdView")
    createAndLoadAd()
    }
    }

    private fun createAndLoadAd() {
    Log.d(TAG, "Creating new AdView")
    // Clean up existing AdView
    adView?.destroy()
    adView = null
    removeAllViews()

    // Create new AdView
    adView = AdView(context).apply {
    adUnitId = TEST_BANNER_ID
    setAdSize(AdSize.MEDIUM_RECTANGLE)
    // Use WRAP_CONTENT to let the ad determine its size
    layoutParams = LayoutParams(
    LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT
    ).apply {
    gravity = Gravity.CENTER
    }

    adListener = object : AdListener() {
    override fun onAdLoaded() {
    super.onAdLoaded()
    Log.d(TAG, "Ad loaded successfully!")
    Log.d(TAG, "AdView size: ${width}x${height}")
    Log.d(TAG, "AdSize: ${adSize?.width}x${adSize?.height}")
    // Update the container size after ad is loaded
    post {
    val params = layoutParams
    params.width = width
    params.height = height
    layoutParams = params
    }
    }

    override fun onAdFailedToLoad(error: LoadAdError) {
    super.onAdFailedToLoad(error)
    Log.e(TAG, "Ad failed to load. Error code: ${error.code}")
    Log.e(TAG, "Error message: ${error.message}")
    Log.e(TAG, "Error domain: ${error.domain}")
    }
    }
    }

    // Set container to wrap content
    layoutParams = LayoutParams(
    LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT
    ).apply {
    gravity = Gravity.CENTER
    }
    // Add AdView to layout
    addView(adView)
    // Load the ad
    val adRequest = AdRequest.Builder().build()
    Log.d(TAG, "Requesting ad load")
    adView?.loadAd(adRequest)
    }

    override fun onDetachedFromWindow() {
    super.onDetachedFromWindow()
    Log.d(TAG, "BannerAd detached from window")
    adView?.destroy()
    adView = null
    }
    }

    class BannerAdManager : SimpleViewManager<BannerAd>() {
    override fun getName() = "BannerAd"

    override fun createViewInstance(reactContext: ThemedReactContext): BannerAd {
    Log.d("BannerAdManager", "Creating new BannerAd instance")
    return BannerAd(reactContext)
    }
    }

Reply all
Reply to author
Forward
0 new messages