LoginSignup
3
6

More than 5 years have passed since last update.

AndroidアプリへのAdMob設定手順(2016年12月)

Last updated at Posted at 2016-12-14

仕様がころころ変わるのと、毎回コピペで済ませていたため手順を忘れないためにメモ。

google-services.jsonの取得

  • Firebase ConsoleにログインしてアプリをFirebaseに連携する。
  • 上記で取得したgoogle-services.jsonを /アプリルート/app 内に置く。

build.gradleの修正

  • ルートレベルのbuild.gradleのdependencies内にclasspathを追加する。

    build.gradle(Root)
    buildscript {
    
        ...
    
        dependencies {
    
            ...
    
            // 以下を追加
            classpath 'com.google.gms:google-services:3.0.0'
        }
    }
    
  • モジュールレベルのbuild.gradleを編集する

    • dependencies内に依存関係を追加する。
    • プラグインを適用する。(原則としてbuild.gradleの最下部に記述する)
    • バージョンは適宜変更する。
    build.gradle(Module)
    apply plugin: 'com.android.application'
    
    android {
    
        ...
    
    }
    
    dependencies {
    
        ...
    
        //以下を追加
        compile 'com.google.firebase:firebase-core:9.6.1'
        compile 'com.google.firebase:firebase-ads:9.6.1'
    }
    
    // 以下を追加
    apply plugin: 'com.google.gms.google-services'
    

AndroidManifest.xmlの修正

  • uses-permissionタグ2つ、meta-dataタグ1つ、activityタグ1つを追加する。

    AndroidManifest.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.google.android.gms.example.bannerexample" >
    
        <!-- この2つのパーミッションを追加する -->
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <!-- この2つのパーミッションを追加する -->
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
            <!--このメタ情報を追加する-->
            <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
            <!--このメタ情報を追加する-->
    
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <!-- このアクティビティを追加する -->
            <activity
                android:name="com.google.android.gms.ads.AdActivity"
                android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
                android:theme="@android:style/Theme.Translucent" />
            <!-- このアクティビティを追加する -->
    
        </application>
    
    </manifest>
    

strings.xmlの修正

  • 広告ユニットIDを追加する。(下記はテスト用ユニットID)

    strings.xml
    <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
    

acitivity_mainの修正

  • 最上位レイアウトに名前空間を追加する。
  • 広告のViewを追加する

    activity_main.xml
    <!-- 最上位レイアウトにxmlns:ads=~を追加する -->
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
    <!-- 最上位レイアウトにxmlns:ads=~を追加する -->
    
        <TextView
            android:text="@string/hello_world"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <!-- このViewを追加する -->
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id">
        </com.google.android.gms.ads.AdView>
        <!-- このViewを追加する -->
    
    </RelativeLayout>
    

MainAcitivity.javaの修正

  • AdRequestとAdViewをimportする。
  • onCreate内で初期化、広告ビューの取得、リクエストの生成、広告の読み込みを行う。
    ※初期化はApplicationを継承したクラスの中でも行える。

    MainActivity.java
    package ...
    import ...
    import ...
    
    //以下の2行を追加
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    
    public class MainActivity extends ActionBarActivity {
    
        ...
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //以下の4行を追加
            //TODO:以下のアプリケーションIDはサンプルです。MobileAds.initialize()の第二引数は各自で修正してください。
            MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
            AdView mAdView = (AdView) findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
        }
    
        ...
    
    }
    

その他 (Optional)

  • ビューのライフサイクル管理のためonPause()、onResume()、onDestroy()を実装する。
  • google-services.json生成時にオプションの入力項目としてSHA-1 fingerprintを入力する欄があるが、AdMobの利用のみであれば(多分)省略可。
    ただし、後々Firebaseの他の機能を利用する際に必要となる場合があるので、その時に再度jsonを取得し直すのが面倒であれば、予めSHA-1 fingerprintは入力してもよい。
    ※fingerprintの取得方法などはfirebaseのマニュアルを参照、と言いたいところですが、マニュアル通りにやろうとすると多分失敗するので別途記事を作成します。
3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6