Android AdMob With Firebase

About Firebase

First of all, you have to know that using AdMob with Firebase is optional, at least for now, and you can still integrate AdMob by importing compile ‘’ (like in this tutorial). But even if it is optional, it is recommended, as it brings all services like AdMob, Analytics, crash reporting and other services together in just one place.


Versions used:

  • Android Studio 2.2 Preview 3
  • classpath google-services 3.0.0 dependency
  • firebase-ads 9.0.2 dependency

Project setup for supporting ads

  • google-services.json file from Firebase Console
  • In Project-level build.gradle add Google Services to dependencies
  • In App-level build.gradle  add Firebase Ads to dependencies and apply Google Services plugin
  • Internet Access

Before we start, create a new project with an Empty Activity template. After the project is created you should have class and activity_main.xml file in res folder. We will refer to these later in the code.

1. Download google-services.json

This file is mandatory for your project to compile. If google-services.json file is not added to your project, you will get the following error when trying to sync App-level build.gradle.

So, in order to download it, you have to register your project in Firebase Console. Below are the steps:

Register project in Firebase Console
  1. Open Firebase Console and click on the Create New Project button
  2. Type your app name and click on Create Project button
  3. Now the following page should openAdd Firebase
  4. Tap on the button like in the above image
  5. In the new page that will open, type your project’s package name (you can find it in your AndroidManifest.xml file or in activities above imports)Firebase Add Details
  6. Now, after you press Add App button, the google-services.json will be automatically downloaded
  7. Copy the file that has just been downloaded and add it to your project to the application’s root folder, YourAppName/app/Firebase Config File
  8. Click Continue and then Finish
2. Project-level build.gradle

Project Level Build Gradle

In the build.gradle file which is outside app module, you have to add the following dependency:

Now, tap on Sync Now to sync gradle.

3. App-level build.gradle 

Inside the app directory you can see another build.gradle file. In this file you have to add 2 lines:

Below is how the final code should look like:

Tap on Sync Now to sync gradle.

4. Internet access

In order to receive ads you need access to internet. So you have to add the code below in the AndroidManifest.xml, right above <aplication> element:

Add the ads

  1. Go to activity_main.xml file and add the following code

    Note: Notice that in order to use ads: properties we have to add to the root layout

    In order to get YOUR UNIT ID you should do the following:

    • go to AdMob site, click on the Monetize New App button and add your app.
    • If you want to can link the app to Firebase from here. But you can skip an link it later like shown in this tutorial.


    • after you add your app, on the Monetize page, you should find your project on the left. Click on it and you will see your Unit ID. Unit IDs have the form ca-app-pub-XXXXXXXXXXXXXXXX/NNNNNNNNNN.



    Now, just copy it from here and paste it into your activity_main.xml file.

  2. Go to MainActivity and add this code:



YOUR APP ID is different from Ad Unit Id. Application IDs have the form ca-app-pub-XXXXXXXXXXXXXXXX~NNNNNNNNNN and can be found on your AdMob account page by clicking the Settings icon – App Management.


It is very important to run the ads in debug mode on your own devices, in order to avoid being disabled for clicking your own ads. So, to get your device ID, you have to run your application on the DEVICE in debug mode and wait for an ad to load. Then go to logcat output and search for the ID. It should be at Log INFO like in the picture below. You can add as many devices as you want.


Link your app from AdMob to Firebase

If you want to see Firebase Analytics information in AdMob you have to link your app from AdMob account with Firebase project we have just configured earlier.

  1. From AdMob account, tap again on the Settings icon – App management
  2. Search your app and tap on the Link to FirebaseLink to Firebase
  3. Add the package name of your app you (in our example we have to use the package name we used when we configured our app directly in Firebase)AdMob Package
  4. Click Continue button and you should see the following screenLink to existing
  5. In our example we let the first option to be enabled (Link to existing…) as we got an error that we cannot create a new Firebase project due to limit exceeded (even though we only have this project). Click the Continue button and done.

Successfully linked

See your linked apps in Firebase Console

View Linked Apps

Android AdMob Banner XML

In the preview tutorial I explained how to add AdMob to your project, but we created the AdView in Java. So in this tutorial I will show you how to set it up in XML. Actually it’s more simple this way, at least in my opinion 🙂

1. Create a new project (or use an existing one if you want to)
2. Go to res – layout – main.xml and change the LinearLayout to RelativeLayout (for simplicity…I used RelativeLayout and it’s better for you to do the same so as not to become confused). This time I made the ad to be displayed at the bottom of the screen.
3. In the RelativeLayout add this code just after before the android:layout_width:

4. Now you have to introduce this code just before the RelativeLayout tag is closed:

Your entire main.xml should look like this:

5. Now in the the activity class you must have this:

6. To get your device ID you have to run  your application on the DEVICE in test mode for emulator and then go to logcat output and search for the ID. It should be at Log INFO like in the picture below:

Now the result should be like this:


How to Add AdMob to Android [Deprecated]

NOTE 1: This tutorial is now deprecated. See this one instead!

NOTE 2:  This project is made in Intellij. For those who use Eclipse or other IDE the integration of AdMob library will differ.

You can use AdMob lib only on at least Android v3.2 (Api Level 13), so you must compile your project on this version (at least). But if you want your application to work on older version set in your AndroidManifest.xml

1. First you have to download the AdMob SDK library from here
2. Now go to where you have downloaded the library and copy the jar (inside the zip) file GoogleAdMobAdsSdk-6.0.1 and paste it in the libs folder from Intellij

3. Now you have to install the jar. To do this you must go to the Project Structure in Intellij (the shortcut CTRL + ALT + SHIFT + S). Here go to Modules – Dependencies and click on the “+” button from the right and select Library – Java. It will open a window and you will have to select the .jar from libs from your project. Click OK on all windows that open.

4. Now create a new project called AdMobExample with the main class called MyActivity. Now go to the AndroidManifest.xml and introduce the following code just before the application tag closes.

5. We must put the Internet permissions in the manifest file just before manifest tag closes.

6.Now go to the res – layout – main.xml and put an id to the layout called main_layout:

7. At this last step you have to go to MyActivity class and introduce the following code:

NOTE: To get your device ID you have to run  your application on the DEVICE in test mode and wait for an ad to load.  Then go to logcat output and search for the ID.  It should be at Log INFO like in the picture below. You can add as many devices as you want.

Maybe you wonder how to obtain the AdMob ID publisher. Don’t worry, I will explain this too.
1. You have to go to AdMob site and log in with your google account or you can create an AdMob account.
2. After you have logged in, go to Sites & Apps and click on Add Site/App:

3. Now you must introduce your application details or you can use the details from the below picture for testing.

4. After you click continue it should see a screen with your Publisher ID. But you can find it too if you click on Add Sites/Apps

5. Down on this page you should find the application you just added. To get to the Publisher ID click on the Manage Settings button.

6.Now you can get your Publisher ID


AND this should be the result of our tutorial: