Android Add AdMob With Google Play Services

You might want to also check AdMob with Firebase Tutorial

Now we can integrate ads from AdMob using Google Play Services. There are some changes in the way ads must be implemented now, so this why I decided to make a tutorial about it and to explain how we have to add ads to our projects. So let’s start!

Tools Used:

  • Android Studio 2.2 Preview 3
  • Google Play Services 9.0.2
  • Genymotion Samsung Galaxy S3 as emulator

Create a new project and name your first activity, MainActivity and the associated xml, activity_main.xml. It’s not mandatory to name them this way, but I will use these names later in the tutorial.

1. Project setup for supporting ads

  • Adding of Google Play Services to dependencies
  • Internet Access
  • Meta-Data for play services
  • Declare activity for ads in Manifest (it is from Google Play Services)

1.1 Adding of Google Play Services

Now it’s quite simple to integrate AdMob to our project. You have to go to the build.gradle file of your project and add to dependencies the the following code:

compile 'com.google.android.gms:play-services:9.0.2'

add_services

Now sync the project with Gradle.

By adding it to dependencies, Gradle will take care of adding the play services  (which have support for AdMob, Anaytics, Maps, etc.), to our project, so this way we don’t have to search, download and integrate the AdMob library like we used to do before.  Like I said above now, it’s simple 🙂

1.2 Internet Access

In order to receive ads you need access to internet. So you have to add the code below in the AndroidManifest.xml, right above <aplication> element:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

1.3 Meta-Data for Google Play Services

In the AndroidManifest.xml add the code below, insisde <application>:

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

 1.4 Declare activity for ads in Manifest

In the AndroidManifest.xml file you should declare a new activity which will be used for ads when the user clicks on the ad. You just have to add the code below:

<activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

This is the final AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admob">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <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" />
    </application>

</manifest>

2. Add the Ads

2.1 Go to activity_main.xml file and add the following code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    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">

    <TextView
        android:text="Hello world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adMob"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="YOUR UNIT ID" />

</RelativeLayout>

In order to get YOUR UNIT ID you should do the following:

  • go to AdMob site, click on the Monetize New App button and add your app.

monetize

  • after you add your app, on the Monetize page, you should find your project on the left. Click on it and you will see your Unit ID. Unit IDs have the form ca-app-pub-XXXXXXXXXXXXXXXX/NNNNNNNNNN.

admob_monetize

test_unit

Now, just copy it from here and paste it into your activity_main.xml file.

2.2 Go to MainActivity and add this code:

package com.example.admob;

import android.app.Activity;
import android.os.Bundle;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdView adView = (AdView) this.findViewById(R.id.adMob);
        //request TEST ads to avoid being disabled for clicking your own ads
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)// This is for emulators
                 //test mode on DEVICE (this example code must be replaced with your device uniquq ID)
                .addTestDevice("2EAB96D84FE62876379A9C030AA6A0AC") // Nexus 5
                .build();
        adView.loadAd(adRequest);
    }
}

NOTE: It is very important to run the ads in test mode on your own devices, in order to avoid being disabled for clicking your own ads. So, to get your device ID, you have to run your application on the DEVICE in debug mode and wait for an ad to load. Then go to logcat output and search for the ID. It should be at Log INFO like in the picture below. You can add as many devices as you want.

android_adMob_ID

3. The result of our work 😛

admob_hello_world

Menu