3 min read

Setup AdMob Test Ads for Debug Builds on Android

Prevent your AdMob account from being suspended

Let us say you want to display some ads in your app, but you don’t want your AdMob account to be suspended…

You have sweated blood to finish your app, put a lot of effort into getting it ready. After considering the different monetization options, you have decided to give AdMob Ads a chance.

So you are ready, your ads look great and you are looking forward to make some cents or millions! Then you get a warning by Google telling you that your AdMob account is at risk if you keep displaying ads in a non-release version. What is going on?! 😱

Cartoon character screaming

What is going on is that you are displaying or clicking on ads in your development build! Obviously, you are generating a lot of “fake views” and “fake clicks”. Advertizers are not happy about that. No potential customer has seen their ads, no potential customer has landed in their website and they still have to pay for that?

So you run to the AdMob documentation and see:

If you click on too many ads without being in test mode, you risk your account being flagged for invalid activity.

You keep digging and also find this and that:

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.

Alright, everything makes sense now 🤔 As we do not want the account to be suspended or flagged, we should be displaying our ads only on the release build and display Test Ads on the debug build. Understandably, we have to take care of this!

First things first

I will assume that if you are reading this, you already created an AdMob account, where you will get the AdMob app ID.

An app ID is a unique ID number assigned to your apps when they’re added to AdMob. The app ID is used to identify your apps.

If you don’t have an AdMob account yet, follow the link to create one, here you can find the necessary steps to create one.


Now let’s go to the code…

Google instructions are fairly descriptive about the basic setup necessary to display ads on an Android app. So we should follow the instructions from the official documentation to setup AdMob Ads.

This is how the AdMob app ID should be setup in Android Manifest

As explained in the documentation, to set up the AdMob app ID, we have to add it to the app’s AndroidManifest.xml file by adding the <meta-data> tag as shown below:

<!-- AndroidManifest.xml in /app/src/main folder -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application>
        <!-- AdMob App ID -->
	<meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="YOUR_APP_ID" />
    </application>

</manifest>

We can already see the real ads! But on the debug build… 🤔

By adding the AdMob app ID only in the AndroidManifest.xml, every time we launch the app then the ads will be loaded. That means that during development we would be loading real ads on the debug build.


How do we display different ads during development?

By default, Android projects have all the source code shared between all the build variants in the /main directory. However, Android gives us the possibility to have different sources depending on the build variant: 

  • build type (debug / release)
  • flavors (e.g. staging / prod) or
  • a combination of them (e.g. stagingDebug or prodRelease)

Hence, the idea is that we will be overriding the <meta-data> section that we declared previously in the AndroidManifest.xml and provide a specific one for the debug build type.

For the sake of simplicity we will assume that there are no flavors configured, therefore we only have two variant configurations: debug and release.

The main AndroidManifest.xml is located in the /app/src/main path.

  1. Create a directory called debug in src directory. The resulting path will be: /app/src/debug.
  2. Create a file named AndroidManifest.xml in that path.
  3. Add the <meta-data> section as displayed below. We will use the Sample app ID specified in the docs.

<!-- AndroidManifest.xml in /app/src/debug folder -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <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-3940256099942544~3347511713"
            tools:replace="android:value" />
    </application>

</manifest>


How can we check that everything is working?

We would like to make sure that debug configuration of the AndroidManifest.xml is applied correctly. We can do that easily on Android Studio.

  1. Open /app/src/main/AndroidManifest.xml file.
  2. Select Merged Manifest tab at the bottom of the window.
  3. The applied AdMob app ID is displayed as in the screenshot below.
Merged manifest shows sample AdMob app ID is used.

Merged manifest shows sample AdMob app ID is used.

Summary

That’s it! Now we have set up our Test Ads for our debug build and the production ads will only be displayed in the release build 🎉

Man pointing his head showing he is smart

This guy was already using AdMob Test Ads.

I hope you found this article useful, thanks for reading!

References

If you found this article interesting, share it!


Follow my journey

Get the latest updates in your inbox 📨

    No spam. Unsubscribe as your heart desires.