Michelle Studio 技術筆記

如何在 Android App 中整合 Google AdMob 廣告

發佈日期:2025-07-31

在 Android App 中加入 Google AdMob 廣告可以幫助你從免費應用程式中獲得收益。本篇文章將引導你完成從 AdMob 註冊到整合廣告 SDK 的完整流程。

步驟 1:建立 AdMob 帳戶

前往 admob.google.com 並註冊帳戶。完成帳號驗證與 App 建立。

步驟 2:在 Firebase 註冊 App 並啟用 Analytics

AdMob 廣告整合需搭配 Firebase 使用,請先前往 Firebase 控制台建立專案並加入你的 App。

步驟 3:加入 SDK

// build.gradle (project)
classpath 'com.google.gms:google-services:4.3.15'
// build.gradle (app)
implementation 'com.google.android.gms:play-services-ads:23.0.0'
apply plugin: 'com.google.gms.google-services'

步驟 4:初始化廣告 SDK

MobileAds.initialize(this, initializationStatus -> {});

步驟 5:插入 Banner 廣告

在 XML 中加入:

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-xxxxxxxxxxxxx/xxxxxxxxxx" />

並在 Java 檔中載入廣告:

AdView adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

完成基本的 AdMob 整合流程。

返回首頁

— Facebook 廣告 —