0

Why do AdMob test ads work in my Android app but NEVER when I try to use my own? Does anyone know what could be going wrong here because I'm very puzzled.

Sample ad unit

enter image description here

My ad unit

enter image description here

Things I tried

  1. Made sure my payment account is verified
  2. Gradle file (Modular level), I used this dependency

    implementation 'com.google.android.gms:play-services-ads:18.3.0'

  3. Used the correct ad unit ID & app ID

  4. Used the correct information in my manifest file
  5. Initialised MobileAds

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.apptacularapps.myad">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/>
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-XXXXXXXXXXXXXXXX~XXXXXXXXXX"/>

        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>

</manifest>

XML

<?xml version="1.0" encoding="utf-8"?>
<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">

    <RelativeLayout
        android:id="@+id/ad_view_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="Google AdMob"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id" />
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin

class MainActivity : AppCompatActivity() {
    private lateinit var adView: AdView

    private val adSize: AdSize
        get() {
            val display = windowManager.defaultDisplay
            val outMetrics = DisplayMetrics()
            display.getMetrics(outMetrics)

            val density = outMetrics.density

            var adWidthPixels = ad_view_container.width.toFloat()
            if (adWidthPixels == 0f) {
                adWidthPixels = outMetrics.widthPixels.toFloat()
            }

            val adWidth = (adWidthPixels / density).toInt()
            return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        MobileAds.initialize(this) { }

        adView = AdView(this)
        ad_view_container.addView(adView)
        loadBanner()
    }

    private fun loadBanner() {
        adView.adUnitId = AD_UNIT_ID

        adView.adSize = adSize

        val adRequest = AdRequest
            .Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()

        adView.loadAd(adRequest)
    }

    companion object {
        private val AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111"
    }
}
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
wbk727
  • 8,017
  • 12
  • 61
  • 125
  • where are you trying your app? is it already deployed to stores? – Leo Jebran Jan 17 '20 at 09:59
  • I'm trying it on my smartphone. No, it is not deployed to stores yet. – wbk727 Jan 17 '20 at 10:00
  • 1
    Have you tried on any other device than your testing device? Also make sure u run application on release mode not in debug mode. – Habeeb Rahman Jan 17 '20 at 10:02
  • It might be this cause the code looks fine. The AdMob doesn't work in a development environment (emulator, or a debug device) – Leo Jebran Jan 17 '20 at 10:04
  • @HabeebRahman What's the official corrct way to run an app in release mode? I've not seen any doucmentation from Google + answers on Stack Overflow are always different. – wbk727 Jan 17 '20 at 10:17
  • @MacaronLover Just create a signed apk of your application from Build > Generate Signed APK/Bundle. And try it in any other device. – Habeeb Rahman Jan 17 '20 at 10:21
  • @MacaronLover https://developer.android.com/studio/publish/app-signing check this out – Habeeb Rahman Jan 17 '20 at 10:22
  • I'm still a bit lost. I enrolled in Google Play App Signing to Secure my App Keys but is there a way to use them so that I can deploy the release apk to my decive whenever I click the Run button? – wbk727 Jan 20 '20 at 15:28
  • You have to generate a signed apk and transfer the apk yo your mobile device and install on your mobile device. – Habeeb Rahman Jan 23 '20 at 09:21

3 Answers3

0

You should add listeners to your implementation to get output why loading a banner fails.

mAdView.adListener = object: AdListener() {
override fun onAdLoaded() {
    // Code to be executed when an ad finishes loading.
}

override fun onAdFailedToLoad(errorCode : Int) {
    Log.d("AdView", "onAdFailedToLoad. But why? "+errorCode)
    // Code to be executed when an ad request fails.
}

override fun onAdOpened() {
    // Code to be executed when an ad opens an overlay that
    // covers the screen.
}

override fun onAdClicked() {
    // Code to be executed when the user clicks on an ad.
}

override fun onAdLeftApplication() {
    // Code to be executed when the user has left the app.
}

override fun onAdClosed() {
    // Code to be executed when the user is about to return
    // to the app after tapping on an ad.
}
}
S. Gissel
  • 1,788
  • 2
  • 15
  • 32
0

usually it take some time to AdMob analyze your app traffic and showing ad

you can see it here: LINK

hamid_c
  • 849
  • 3
  • 11
  • 29
0

First peek: your manifest should show your APP ID and not "ca-app-pub-XXXXXXXXXXXXXXXX~XXXXXXXXXX" in the meta-data tag.

You can find out your App ID at Abmob Webpage, Login -> Apps -> Show all Apps

Furthermore, ads do not necessarily get displayed. Check out this: https://stackoverflow.com/a/33712905/2797847

josewe
  • 51
  • 6