發佈日期:2025-07-31
在 Android App 中加入 Google AdMob 廣告可以幫助你從免費應用程式中獲得收益。本篇文章將引導你完成從 AdMob 註冊到整合廣告 SDK 的完整流程。
前往 admob.google.com 並註冊帳戶。完成帳號驗證與 App 建立。
AdMob 廣告整合需搭配 Firebase 使用,請先前往 Firebase 控制台建立專案並加入你的 App。
// 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'
MobileAds.initialize(this, initializationStatus -> {});
在 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 整合流程。