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

0 Comments

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.

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

While you're there:

1.<!-- Replace -->
2.<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"></activity>
3. 
4. 
5.<!-- with this -->
6.<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:

01.// Replace
02.import com.google.ads.AdRequest;
03.import com.google.ads.AdSize;
04.import com.google.ads.AdView;
05. 
06. 
07.// with this
08.import com.google.android.gms.ads.AdRequest;
09.import com.google.android.gms.ads.AdSize;
10.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.

01.// Replace
02.AdView ad = new AdView(this, AdSize.BANNER, YOUR_ADMOB_ID);
03.ad.loadAd(new AdRequest());
04. 
05. 
06.// with this
07.ad = new AdView(this);
08.ad.setAdSize(AdSize.BANNER);
09.ad.setAdUnitId(YOUR_ADMOB_ID);
10. 
11.AdRequest adRequest = new AdRequest.Builder().build();
12.ad.loadAd(adRequest);

Remember to manage configuration changes on pause/resume.

01.@Override
02.protected void onPause() {
03.  if (ad != null) {
04.    ad.pause();
05.  }
06.  super.onPause();
07.}
08. 
09.@Override
10.protected void onResume() {
11.  if (ad != null) {
12.    ad.resume();
13.  }
14.  super.onResume();
15.}
16. 
17.@Override
18.public void onDestroy() {
19.  if (ad != null) {
20.    ad.destroy();
21.  }
22.  super.onDestroy();
23.}

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