Google have announced AdMob will now be available from with the Google Play Services library. The issue being they are stopping delivering ads to the AdMob sdk jar from August 2014. I describe here some simple steps to help you migrate. This post assumes you originally were using AdMob sdk version 4 or lower. With this SDK all you needed to do was add your AdView to your XML file, add the AdMob activity to the manifest and you where done! Like so:
// xml layout file <com.google.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="yourId" ads:loadAdOnCreate="true" /> // AndroidManifest.xml <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
The new GooglePlayServices AdMob is a little more complicated, so hopefully the below should ease your transition!
Step 1, update your build.gradle
dependencies { compile 'com.google.android.gms:play-services:5.0.77' }
Step 2, update your manifest
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.google.android.gms.ads.AdActivity" android:theme="@android:style/Theme.Translucent" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
Step 3, update your XML AdViews
<com.google.android.gms.ads.AdView xmlns:ads="https://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" ads:adUnitId="yourId" ads:adSize="SMART_BANNER"/>
So those steps where a replacement however the next code is new! With the new sdk you have to inform your AdView of the activity lifecycle, this means adding callbacks in onResume, onPause etc. However this gets a bit repetitive if you have a lot of activities. If you use the helper class I show below you can just use a one liner every time. Like so:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AdMobPoker.track(findViewById(R.id.adView), savedInstanceState); } }
The poker takes care of looking after the callbacks for the lifecycle and means you don’t have to plug it in everytime!
package com.blundell.facebookrehab.ui.phone; import android.app.Activity; import android.app.Application; import android.os.Bundle; import android.view.View; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class AdMobPoker implements Application.ActivityLifecycleCallbacks { private final AdView adView; public static void track(View adView, Bundle savedInstanceState) { if (!(adView instanceof AdView)) { throw new IllegalStateException("The view you pass must be an AdMob view."); } AdMobPoker adMobPoker = new AdMobPoker((AdView) adView); Activity activity = (Activity) adView.getContext(); activity.getApplication().registerActivityLifecycleCallbacks(adMobPoker); adMobPoker.onActivityCreated(activity, savedInstanceState); } private AdMobPoker(AdView adView) { this.adView = adView; } @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); adView.loadAd(adRequest); } @Override public void onActivityStarted(Activity activity) { // not used } @Override public void onActivityResumed(Activity activity) { adView.resume(); } @Override public void onActivityPaused(Activity activity) { adView.pause(); } @Override public void onActivityStopped(Activity activity) { // not used } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { // not used } @Override public void onActivityDestroyed(Activity activity) { adView.destroy(); activity.getApplication().unregisterActivityLifecycleCallbacks(this); } }
I hope this eases your migration for the AdMob sdk, frankly I think Google are wrong to bundle this sdk into play services but what can you do! Enjoy!
If you have a more complicated AdMob setup you can refer back to Google for some more in depth migration steps:
https://developers.google.com/mobile-ads-sdk/docs/admob/play-migration
If you want the code as source check out this gist explanation:
https://gist.github.com/blundell/421be929800c91ac663b
Any questions just ask below