Adding Ads to Android App using Google Admob

Making apps are always hard work. This includes days/months of testing and feature iterations. Implementing ads can be one solution where your work finally gets paid off. Google's Admob is an efficient SDK when it comes to app monetisation. Let us see how to implement Admob ads into our android application.

Download full source code here

Let us break this into two modules :

1. Setting up ad units in the admob dashboard.
2. Implementing the created ad units in our app.

1. Setting up ad units in the admob dashboard.

  • Step 1 : head over to and sign in. After proper login, you would see an option named Apps in the left sidebar, click it. Click 'add app' option on the popup that follows.

  • Step 2 : After setting up the app as instructed in previous step, select the created app from the above menu and click on Ad Units from the left sidebar menu as shown below

  •  Now click on "ADD AD UNIT" from the next screen. This will take you to the screen where you can select the ad type : banner, interstetial or rewarded ad type. In this tutorial we will be covering the banner ad type. Banner ads are rectangular strip ads that looks like the following

  • Select banner type and complete the ad unit creation by entering the details. You will reach this screen finally

  • In the above image, the id given in the first step  with a "~" somewhere in between the id is called the app ID and the id given in second step with a "/" somewhere in between the id is the ad unit id. Now make note of these two ids. We will require them while we setup the code.

2. Implementing the created ad units in our app.

Now create a new Android Studio Project and follow these steps :
  • Goto your project level build.gradle file and add the maven repo inside allprojects>> repositories  like this
allprojects {
      repositories {
           maven {
           url ""

  • Goto your app level build.gradle file and add the dependency like this
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation ''
implementation ''

  • Now we need to add the app id that we have obtained from our admob dashboard previously(one with the ~ in between) to our AndroidManifest.xml like shown below.Here I'm adding test app id provided by admob(you may use this for testing but change it to your app id from dashboard while going live)

adding the above meta tag is a mandatory step from play-services-ads version 17.0.0 onwards. Also the meta tag should be added within your <application> tag as shown above

  • Now you may perform a gradle sync.
You are Now all set to implement the ads inside your app. Now go to your add the following code to initialise the admob ads sdk

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");    }

You should add the app id(the one with "~") here as well. For testing purpose, above id is fine(admob will serve test ads for this value)

Now go to layouts>> activity_main.xml and add the following code

<RelativeLayout xmlns:android=""

    <TextView android:text="@string/hello_world"
        android:layout_height="wrap_content" />



In the above declared adview, you need to place the ad unit id from the admob dashboard when going live or you can use the above test value for testing (the one with "/" in between). We have created this earlier along with app id

Note : Never use your production values of ad unit id, app id for testing purposes(doing so may get your account deactivated, although doing so few times for checking is not a problem). It is recommended that you use these test values : ca-app-pub-3940256099942544~3347511713 (test app id),  ca-app-pub-3940256099942544/6300978111(test ad unit id). You may change this to the production values(the one you created in admob dashboard) while going live

Now go to your  file and then initialise and make an ad request like this following

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

    protected void onCreate(Bundle savedInstanceState) {


        mAdView = findViewById(;
        AdRequest adRequest = new AdRequest.Builder().build();

Now run your app and you could see test ads being displayed like below

Now change the test credentials to the ids mentioned in admob dashboard and see if live ads are appearing. Live ads will be similar to the test ads but it wont be having a test ad label like the one above. Don't worry if the ads not appearing when you added your live credentials, admob might take a few hours after ad unit to setup the ads for you. It will start showing ads from then on.

You can download the full source code here
In the downloaded code replace the test credentials with the one received from admob dashboard to see live ads or otherwise it will be serving test ads

No comments:

Post a Comment