Click here to see your recently viewed pages and most viewed pages.
Hide
AdMob Android Guides

Google Play Services Migration

The following table summarizes implementation differences between the Android SDK and Google Play services and can be used as a checklist when migrating. See also the Google Mobile Ads reference docs for more details.

  1. Bundling the library
  2. Importing library classes
  3. Declaring an AdActivity in AndroidManifest.xml
  4. Creating an AdView
  5. Creating an AdRequest
  6. AdListener object
  7. Setting AdListener
  8. Creating AdView in XML and loading an ad
  9. AdView lifecycle methods
  10. Passing extras to AdMob
  11. Notes
Concept Android SDK Google Play services
Bundling the library Place GoogleAdMobAdsSdk-x.x.x.jar inside libs/ folder of your application project. Set up Google Play services library project, and have your application reference the Google Play library (step-by-step instructions).
Importing library classes
import com.google.ads.*
import com.google.android.gms.ads.*
Declaring an AdActivity in AndroidManifest.xml
<activity android:name="com.google.ads.AdActivity"…/>
<activity android:name="com.google.android.gms.ads.AdActivity"…/>
<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>
More
Creating an AdView
AdView adView = new AdView(activity, AdSize.BANNER,
    "MY_AD_UNIT_ID");
AdView adView = new AdView(activity);
adView.setAdUnitId("MY_AD_UNIT_ID");
adView.setAdSize(AdSize.BANNER);
Creating an AdRequest
AdRequest request = new AdRequest()
    .addTestDevice(AdRequest.TEST_EMULATOR);
AdRequest request = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .build();
AdListener object
public interface AdListener {
  public void onReceiveAd(Ad ad);
  public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error);
  public void onPresentScreen(Ad ad);
  public void onDismissScreen(Ad ad);
  public void onLeaveApplication(Ad ad);
}
public abstract class AdListener {
  public void onAdLoaded() {}
  public void onAdFailedToLoad(int errorCode) {}
  public void onAdOpened() {}
  public void onAdClosed() {}
  public void onAdLeftApplication() {}
}
Setting AdListener
class MyListener implements AdListener {
  public void onReceiveAd(Ad ad) {}
  public void onFailedToReceiveAd(Ad ad, ErrorCode error) {}
  public void onPresentScreen(Ad ad) {}
  public void onDismissScreen(Ad ad) {}
  public void onLeaveApplication(Ad ad) {}
}

adView.setAdListener(new MyListener());
adView.setAdListener(new AdListener() {
  public void onAdLoaded() {}
  public void onAdFailedToLoad(int errorcode) {}
  // Only implement methods you need.
});
Creating AdView in XML and loading an ad
<com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adUnitId="MY_AD_UNIT_ID"
    ads:adSize="BANNER"
    ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
    ads:loadAdOnCreate="true"/>

// No Java code required.
<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adUnitId="MY_AD_UNIT_ID"
    ads:adSize="BANNER"/>

// Java code required.
// testDevices and loadAdOnCreate attributes are
// no longer available.
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("TEST_DEVICE_ID")
    .build();
adView.loadAd(adRequest);
AdView lifecycle methods
@Override
public void onDestroy() {
  adView.destroy();
  super.onDestroy();
}
@Override
public void onPause() {
  adView.pause();
  super.onPause();
}

@Override
public void onResume() {
  super.onResume();
  adView.resume();
}

@Override
public void onDestroy() {
  adView.destroy();
  super.onDestroy();
}
Passing extras to AdMob
AdRequest adRequest = new AdRequest();
AdMobAdapterExtras extras = new AdMobAdapterExtras()
    .addExtra("color_bg", "AAAAFF")
    .addExtra("color_text", "808080");
adRequest.setNetworkExtras(extras);
Bundle bundle = new Bundle();
bundle.putString("color_bg", "AAAAFF");
bundle.putString("color_text", "808080");
AdMobExtras extras = new AdMobExtras(bundle);
AdRequest adRequest = new AdRequest.Builder()
    .addNetworkExtras(extras)
    .build();

Notes

  • Existing mediation adapters will continue to work with the Google Play services library.
  • Even if you haven't upgraded to the new AdMob console on apps.admob.com, you can still migrate to Google Play services as the library supports both the legacy and new publisher IDs.