Integrate Google AdMob into Android

Integrate Google AdMob into Android

Google AdMob is a mobile advertising platform that can be integrated into Android application. It allows monetizing application by displaying ads alongside app content. Ads are automatically served from the AdMob Network and third-party networks.

The Google Mobile Ads SDK is a part of the Google Play Services. So, we need to add play-services-ads dependency in the module's build.gradle file.

app/build.gradle

dependencies {
    // Other dependencies
    // ...
    implementation 'com.google.android.gms:play-services-ads:19.3.0'
}

To use the Google AdMob, we need an app ID and ad unit ID.

  • An app ID is a unique identifier that is assigned to an application when it added to AdMob. ID is used to identify application.
  • An ad unit ID is a unique identifier that is assigned to the ad unit when it created in AdMob. ID is used to identify requests from the ad unit.

We can get these identifiers by following these steps:

  1. Login to the AdMob Account.
  2. On the left side of the dashboard, click "Apps".
  3. Click "ADD YOUR FIRST APP".
  4. Provide app name, choose Android platform, and then click "ADD".
  5. Copy app ID.
  6. Open the AndroidManifest.xml file and add the meta-data element which defines an app ID.

app/src/main/AndroidManifest.xml

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

    <application>
        <!-- ... -->
        
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXXXX"/>

        <activity android:name=".MainActivity">
            <!-- ... -->
        </activity>
    </application>

</manifest>
  1. In the AdMob dashboard, click "NEXT: CREATE AD UNIT".
  2. Select "BANER".
  3. Provide ad unit name, and then click "CREATE AD UNIT".
  4. Copy ad unit ID.
  5. In the layout XML file, add AdView element. By using the ads:adUnitId attribute, define the ad unit ID. The ads:adSize attribute defines ad size.

app/src/main/res/layout/activity_main.xml

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

    <com.google.android.gms.ads.AdView
        android:id="@+id/myAdView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXXXX" />

</RelativeLayout>
  1. On the left side of the AdMob dashboard, click "Payments".
  2. Provide required information. A payment method don't needed to add until earnings threshold has been reached.

The Mobile Ads SDK is initialized by using MobileAds.initialize() method. The loadAd() method is used to load an ad. The AdRequest is passed as parameter, which contains information about ad request.

app/src/main/java/com/example/app/MainActivity.kt

package com.example.app

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.gms.ads.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        MobileAds.initialize(this)

        val configuration = RequestConfiguration.Builder()
            .setTestDeviceIds(listOf("7C4136XXXXXXXXXXXXXXXXXXXXXXXXXX"))
            .build()
        MobileAds.setRequestConfiguration(configuration)

        val adRequest = AdRequest.Builder().build()
        myAdView.loadAd(adRequest)
    }
}

When building and testing an application, we need to configure the device as a test device, otherwise AdMob account can be suspended. We need to get the device ID. To do that, run the application and check the logcat output for a message:

I/Ads: Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("7C4136XXXXXXXXXXXXXXXXXXXXXXXXXX") to get test ads on this device.

Leave a Comment

Cancel reply

Your email address will not be published.