Admob広告ユニットIDをデバッグとリリースで使い分ける

2023年1月23日

開発環境
OS:Windows 10
SDK:Android Studio Dolphin | 2021.3.1

概要

Admobから与えられたアプリID、広告ユニットIDをデバッグモードで使用してしまうとAdmobから警告を来て、収益に影響が出てしまいます。
そうならないためにもリリースとデバッグモードで広告ユニットIDを分けて定義しておくと便利です。

Admobを表示するときに必要となるID

Admobをアプリに表示させるにはアプリIDと広告ユニットID、少なくとも2つのIDを定義する必要があります。

アプリID、広告ユニットIDはAdmobで作成することができますが、

アプリのデバッグ時に使用することはできません。

代わりにデバッグ時にはサンプルIDを使用します。

【 サンプルアプリID 】

ca-app-pub-3940256099942544~3347511713

サンプル広告ユニットID

広告フォーマットサンプル広告ユニットID
アプリ起動ca-app-pub-3940256099942544/3419835294
バナーca-app-pub-3940256099942544/6300978111
インタースティシャルca-app-pub-3940256099942544/1033173712
インタースティシャル動画ca-app-pub-3940256099942544/8691691433
報酬型ca-app-pub-3940256099942544/5224354917
報酬型インタースティシャルca-app-pub-3940256099942544/5354046379
ネイティブアドバンスca-app-pub-3940256099942544/5354046379
ネイティブアドバンス動画ca-app-pub-3940256099942544/1044960115
サンプル広告ユニットID一覧

デバッグ用定義とリリース用定義

アプリIDと広告ユニットIDはstring.xmlに定義して管理します。

まずリリース用のアプリID、広告ユニットIDをstring.xmlに定義します。
(下3行がID定義となります。)

<resources>
    <string name="app_name">Look Look</string>
  ・・・
  ・・・
  ・・・
    <string name="ts_adview">Advertisement will be displayed from now on.</string>
    <string name="ad_attribution">Ad</string>
    <!-- admob -->
    <string name="admob_id_manifest">ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx</string>
    <string name="admob_id_bottom_main">ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx</string>
    <string name="admob_id_bottom_map">ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx</string>
</resources>

次にデバッグ用のアプリID、広告ユニットIDをstring.xmlに定義します。

デバッグ用のstring.xmlは最初は存在しないため、新たにデバッグ用フォルダ内に作成します。

【 デバッグ用フォルダの場所 】

(プロジェクトフォルダ)/app/src/debug/res/values/string.xml

※フォルダがない場合はフォルダも作成します。

※多言語化対応している場合は言語毎のvaluesフォルダ(例えば日本語の場合、values-ja
 フォルダ)にstring.xmlを格納します。
 string.xmlの定義内容は全て同じで問題ありません。

デバッグ用フォルダ
<resources>
    <!-- admob -->
    <string name="admob_id_manifest">ca-app-pub-3940256099942544~3347511713</string>
    <string name="admob_id_bottom_main">ca-app-pub-3940256099942544/6300978111</string>
    <string name="admob_id_bottom_map">ca-app-pub-3940256099942544/6300978111</string>
</resources>

string.xmlには、AdmobのサンプルIDだけ定義すればOKです。

アプリID、広告ユニットIDを参照する

あとはstring定義を参照するだけです。

(AdroidManifestの定義例)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  ・・・
    <application
  ・・・
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/admob_id_manifest" />
  ・・・
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/AppTheme.NoTitleMain">
  ・・・
       </activity>
  ・・・  
    </application>
</manifest>

(レイアウトXMLの定義例)

<?xml version="1.0" encoding="utf-8"?>
<ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity">


    <com.google.android.gms.ads.AdView
        android:id="@+id/adViewMap"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/admob_id_bottom_map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="0dp">
    </com.google.android.gms.ads.AdView>
</androidx.constraintlayout.widget.ConstraintLayoutt>

これでデバッグとリリースの使い分けができるようになりました。

いつも通りにデバッグを行えばサンプルIDが参照され、リリースアプリの作成を行えば、リリース用IDが参照されるようになります。