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?! 😱
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.
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
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
However, Android gives us the possibility to have different sources depending on the build variant:
- build type (
- flavors (e.g.
- a combination of them (e.g.
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:
AndroidManifest.xml is located in the
- Create a directory called
srcdirectory. The resulting path will be:
- Create a file named
AndroidManifest.xmlin that path.
- 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.
Merged Manifesttab at the bottom of the window.
- The applied AdMob app ID is displayed as in the screenshot below.
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 🎉
This guy was already using AdMob Test Ads.
I hope you found this article useful, thanks for reading!
If you found this article interesting, share it!