Android: Switching from Admob SDK v6.4.1 to Google Play Services API library

Google announced on the 11th of February 2014 that they were deprecating the standalone Admob Android SDK. They will stop accepting apps using the old SDK on the 1st of August 2014. Ads will keep serving, but eventually you will have to switch over.

They've already started adding incompatibilities in the two projects. I first noticed this when I tried using both Play Services library for maps and Admob wasn't happy about it. I've documented the migration process along with a few other useful tips.

Things to remove

First of all, remove "libs/GoogleAdMobAdsSdk-6.4.1.jar". You won't be needing that anymore.

Things to add

Secondly, import the Google Play Services library project into Eclipse. You can find it at "Android-SDK/extras/google/google_play_services/libproject/google-play-services_lib".

Add it to your project as a library via right click (on project) > Properties > Android > Library > Add > google-play-services_lib.

In your AndroidManifest.xml file, add in this meta-data tag.

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

While you're there:

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


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

Imports to change

Search through your imports and replace:

// Replace
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;


// with this
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

AdView code to change

You should have noticed the "The constructor AdRequest() is not visible" errors by now. If you're creating AdViews from code then you'll have to tweak it a little.

// Replace
AdView ad = new AdView(this, AdSize.BANNER, YOUR_ADMOB_ID);
ad.loadAd(new AdRequest());


// with this
ad = new AdView(this);
ad.setAdSize(AdSize.BANNER);
ad.setAdUnitId(YOUR_ADMOB_ID);

AdRequest adRequest = new AdRequest.Builder().build();
ad.loadAd(adRequest);

Remember to manage configuration changes on pause/resume.

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

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

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

Done!

Check out the Google migration guide if you're also using AdListener interfaces or if anything in this post was confusing.

Other than that, your project should be good to go!

av0A06E_460sa
See any other problems? LASER BEAM THEM!

Sources

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog