AdMob setting procedure for Android application (December 2016)

I made a note so that I wouldn't forget the procedure because the specifications changed from time to time and I had to copy and paste each time.

Get google-services.json

--Log in to Firebase Console and link the app to Firebase. --Put the google-services.json obtained above in / app root / app.

Fix build.gradle

--Add classpath in dependencies of root level build.gradle.

```groovy:build.gradle(Root)
buildscript {

    ...

    dependencies {

        ...

        //Add the following
        classpath 'com.google.gms:google-services:3.0.0'
    }
}
```

--Edit module level build.gradle --Add dependencies within dependencies. --Apply the plugin. (In principle, write at the bottom of build.gradle) --Change the version as appropriate.

```groovy:build.gradle(Module)
apply plugin: 'com.android.application'

android {

    ...

}

dependencies {

    ...

    //Add the following
    compile 'com.google.firebase:firebase-core:9.6.1'
    compile 'com.google.firebase:firebase-ads:9.6.1'
}

//Add the following
apply plugin: 'com.google.gms.google-services'
```

Modification of AndroidManifest.xml

--Add two uses-permission tags, one meta-data tag, and one activity tag.

```xml: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" >

    <!--Add these two permissions-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <!--Add these two permissions-->

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!--Add this meta information-->
        <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
        <!--Add this meta information-->

        <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>

        <!--Add this 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" />
        <!--Add this activity-->

    </application>

</manifest>
```

Modify strings.xml

--Add ad unit ID. (The following is the test unit ID)

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

Fix acitivity_main

--Add a namespace to the top layout. --Add View for ads

```xml:activity_main.xml
<!--Xmlns on top layout:ads=Add-->
<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 on top layout:ads=Add-->

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--Add this 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>
    <!--Add this View-->

</RelativeLayout>
```

Modification of MainAcitivity.java

--Import AdRequest and AdView. --Initialize in onCreate, get ad view, generate request, load ad.
* Initialization can be done even in a class that inherits Application.

```java:MainActivity.java
package ...
import ...
import ...

//Added the following two lines
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);

        //Added the following 4 lines
        //TODO:The application ID below is a sample. Mobile Ads.initialize()Please modify the second argument of by yourself.
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }

    ...

}
```

Other (Optional)

--Implement onPause (), onResume (), onDestroy () for view lifecycle management. --There is a field to enter SHA-1 fingerprint as an optional input item when generating google-services.json, but it can be omitted (probably) if you are only using AdMob.
However, it may be necessary to use other Firebase functions later, so if it is troublesome to get the json again at that time, you may enter the SHA-1 fingerprint in advance. ..
* I would like to say that you should refer to the firebase manual for how to get the fingerprint, but if you try to follow the manual, it will probably fail, so I will create a separate article.

Recommended Posts

AdMob setting procedure for Android application (December 2016)
Notes for Android application development beginners
A memorandum for android application development beginners
Android automatic build setting procedure using Azure DevOps
Links for creating Android apps (for beginners)
AdMob setting procedure for Android application (December 2016)
Beginner-friendly Android app publishing procedure, struggling for the first release⁈
default value for android: exported
Build Spring for Android 2.0.0 environment
Procedure for publishing an application using AWS (6) Introduce Nginx
[Note] Challenge to develop Android application for business use
Android application development preparation 7/15
Procedure for publishing an application using AWS (4) Creating a database
Beginner-friendly Android app publishing procedure, struggling for the first release⁈