Setup AdMob for Android With Firebase

August 20, 2019

NOTE: If you are not using Firebase, refer to standalone AdMob


  • Use Android Studio 3.2 or later
  • minSdkVersion 14 or later
  • compileSdkVersion 28 or later

Create AdMob Account

Create AdMob Account.

Register an App

  • Visit, click Apps -> Add Your First App
  • Have you published your app on Google Play or the App Store? I click No.
  • Enter App name and select platform (Android or iOS)
  • You shall have an App ID: ca-app-pub-8122************~**********
  • Create Ad Unit later.

Link AdMob App to Firebase

  • Apps -> Selecy Your App -> App Settings -> Link to Firebase.
  • Package name: We need the package name to continue the linking. Make sure the package name is correct. It cannot be changed later.
  • I selected Link to an existing Firebase project and existing Firebase app since I already started development using Firebase.
  • I skip Click here to start downloading the config file from the Firebase console.
  • Click DONE.

Setup Adndroid App


dependencies {
    implementation ''


        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->

NOTE: If you forgotten to copy it from earlier steps, visit and click Apps -> Selecy Your App -> App Settings

Initialize the SDK

Only need to be done once, probably at Application.

class LuaApp: Application() {

    override fun onCreate() {

        MobileAds.initialize(this, "[ADMOB_APP_ID]")

Choose ad format

I shall be using Interstitial.

Interstitials are full-screen ads that cover the interface of an app until closed by the user. They’re best used at natural pauses in the flow of an app’s execution, such as in between levels of a game or just after completing a task.

Goto, click Apps -> Your App -> Add Ad Unit.

  • Select ad format: Interstitial
  • Name
  • Create ad Unit
  • Copy Ad Unit ID

Load Ads

  • Need to loadAd earlier before the actually showing the ad
  • Call show to display actual ad
  • Call loadAd again if you need to reload it with a new ad
class TestAdFragment : Fragment() {
    private var ad: InterstitialAd? = null

    override fun onActivityCreated(savedInstanceState: Bundle?) {

        // initialize and load ad earlier
        val ad = InterstitialAd(context)
        ad.adUnitId = if (BuildConfig.DEBUG) {
            // special ad unit if for testing Interstitial Ad
        else {
            // replace with my ad unit id

        fun handleActionAfterShowAd() {
            // do something

        showAdButton.setOnClickListener {
            if (ad?.isLoaded == True) {
                ad?.let {ad ->
                    ad.adListener = object : AdListener() {
                        override fun onAdClosed() {
                            // not showing more ad after this
                            ad = null
                            // if you plan to show more ads later, reload new ad
                            // ad.loadAd(AdRequest.Builder().build())

                            // after ad is closed, perform next action
            else {
                // no ads to show, perform next action

Load Interstitial Ads


This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.