Tuesday, 11 October 2011

Using Admob in Android Apps

Admob is a mobile advertising service, acquired by Google 2009. There are numerous competing ad services out there but Admob is the most well-known in the Android world. It's also the one I started with in January 2010, and continue to use today. In this article I would like to share with you my experience using Admob to add advertising to an app, mentioning the good points and a problem I came across.

On the right is the introductory screen of an app showing the Admob "test" ad. The screenshot is from an accelerometer-based tilt game called Tilt 'n' Squish.

Nice features


Easy to implement:

Adding advertising to an app is pretty straightforward using the Admob API. The basic element is an "AdView" class which can be added to your screen layout just like any other view. The API also provides a degree of flexibility and additional options, for example to define ad appearance or choose whether to enable location-based ads. Your user account can also be easily managed via the Admob website.

Fast and reliable payments:

Admob start to process payments one month after the calendar month in which they are earned. In practise, I found this means the income made throughout January would typically arrive mid-March. That might not sound speedy but actually it's one of the fastest payment schedules out there.

Ability to create "House Ads":

There is the possibility to create your own ads and have Admob show these in the case where there are no others available. If you have more than one app or service, this is a particularly convenient method of cross-promotion.

AdSense integration:

It is now possible to increase the number of ads available to you by using Google's AdSense network. This option is available in the Admob online account management area.

Using location to optimize ads:

If your app uses the phone's location (i.e. has either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions), then you can pass the location information to Admob, to (hopefully) serve a more relevant ad.

Here is some example code - this method loads and shows the ad, and should be called whenever a layout containing an ad element is shown.
public void showAd() { AdView adView = (AdView) findViewById(R.id.Ad); if (adView != null) { LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); AdRequest adRequest = new AdRequest(); adRequest.setLocation(location); adRequest.addTestDevice(AdRequest.TEST_EMULATOR); adView.setVisibility(View.VISIBLE); adView.loadAd(adRequest); } }

Problem


This is a problem I hit pretty quickly with the 4.1.1 version (latest at time of writing) of the Admob SDK.

It seems to affect only older Android (1.5 or 1.6) devices, and is triggered periodically when loading an ad. The application would unexpectedly crash - but with no "force closed" dialog and no stack trace. However there was a long VM crash log which started like this:
10-11 11:10:14.363: INFO/DEBUG(9241): Build fingerprint: 'tmeu/htc_hero/hero/hero:1.5/1.76.110.6/47214:user/release-keys' 10-11 11:10:14.363: INFO/DEBUG(9241): pid: 13148, tid: 13165 >>> com.spwebgames.test <<< 10-11 11:10:14.363: INFO/DEBUG(9241): signal 11 (SIGSEGV), fault addr 00000000 10-11 11:10:14.363: INFO/DEBUG(9241): r0 00000007 r1 411ab858 r2 411ab858 r3 00000000 10-11 11:10:14.363: INFO/DEBUG(9241): r4 411ab858 r5 001c7458 r6 00000000 r7 44adfcac 10-11 11:10:14.363: INFO/DEBUG(9241): r8 44adfda0 r9 4104be40 10 4104be2c fp 00000001 10-11 11:10:14.363: INFO/DEBUG(9241): ip 00000011 sp 44adfc70 lr ad046f05 pc ad03ff72 cpsr 00000030 It's probably the same problem as in this report.

The solution is to ensure the ad is destroyed when each view containing the ad is destroyed. This means calling a method like this just before an ad-containing layout changes:
public void destroyAd() { AdView adView = (AdView) findViewById(R.id.Ad); if (adView != null) { adView.destroy(); } }

Using Admob


Overall, I have found Admob to be an effective and worthwhile method of generating income from free apps. The ease of implementation combined with Google's large advertising network make for a compelling combination. However, a number of other players have appeared on the scene such as MobClix, InMobi and MobFox, and there is some debate as to whether Admob is still the best paying service from the developer/publisher's viewpoint. I will try to investigate this question in future articles.