Setup AdMob for Android With Firebase

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

Prerequisites

  • 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 https://apps.admob.com/, 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

dependencies {
    implementation 'com.google.firebase:firebase-ads:18.1.1'
}

AndroidManifest.xml

<manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="[ADMOB_APP_ID]"/>
    </application>
</manifest>

NOTE: If you forgotten to copy it from earlier steps, visit https://apps.admob.com/ 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() {        super.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 https://apps.admob.com/, 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?) {        super.onActivityCreated(savedInstanceState)        // initialize and load ad earlier        val ad = InterstitialAd(context)        ad.adUnitId = if (BuildConfig.DEBUG) {            // special ad unit if for testing Interstitial Ad            "ca-app-pub-3940256099942544/1033173712"        }        else {            // replace with my ad unit id            "AD_UNIT_ID"        }        ad.loadAd(AdRequest.Builder().build())        fun handleActionAfterShowAd() {            // do something        }        showAdButton.setOnClickListener {            if (ad?.isLoaded == True) {                ad?.let {ad ->                    ad.adListener = object : AdListener() {                        override fun onAdClosed() {                            super.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                            handleActionAfterShowAd()                        }                    }                    ad.show()                }            }            else {                // no ads to show, perform next action                handleActionAfterShowAd()            }        }    }}

Load Interstitial Ads

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.