AdMob

  • Requires a valid Admob account
  • Read the admob policy. Make sure you do not do anything that can get your admob account suspended

Implementing

Note: This example requires a valid Admob account and valid Admob ad code.

Build.gradle on app level

Change to the latest version if existing:

compile 'com.google.firebase:firebase-ads:10.2.1'

Manifest

Internet permission is required to access the ad data. Note that this permission does not have to be requested (using API 23+) as it is a normal permission and not dangerous:

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

XML

The following XML example shows a banner ad:

<com.google.android.gms.ads.AdView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/adView"
    ads:adSize="BANNER"
    ads:adUnitId="@string/main_screen_ad" />

For the code of other types, refer to the Google AdMob Help.

Java

The following code is for the integration of banner ads. Note that other ad types may require different integration:

// Alternative for faster initialization.
// MobileAds.initialize(getApplicationContext(), "AD_UNIT_ID");

AdView mAdView = (AdView) findViewById(R.id.adView);
// Add your device test ID if you are doing testing before releasing.
// The device test ID can be found in the admob stacktrace.
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

Add the AdView life cycle methods in the onResume(), onPause(), and onDestroy() methods of your activity:

@Override
public void onPause() {
    if (mAdView != null) {
        mAdView.pause();
    }
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
    if (mAdView != null) {
        mAdView.resume();
    }
}

@Override
public void onDestroy() {
    if (mAdView != null) {
        mAdView.destroy();
    }
    super.onDestroy();
}