Tuesday, 17 April 2012

How to add AdWhirl to an Android app (with decent instructions!)

As I was getting poor results from AdMob I decided to add AddWhirl to my android app so I could use multiple ad providers.  Little did I know that the documentation for AdWhirl was going to be so bad!

A google search didn't help much, just lots of other people wanting to do the same and getting stuck or confused.  The official instructions didn't help much (https://www.adwhirl.com/doc/android/AdWhirlAndroidSDKSetup.html) - as soon as I had started I got a compile error because of something ommitted from the instructions.

So I set out to solve the problem on my own.  In the end the solution was pretty easy:

1. Set your app up on your chosen Ad provider website (or multiple, but for this explanation I'll go with Admob).  You'll need the ID

2. Set your app up on the AdWhirl website.  Add the ad provider (in this case AdMob) and enter the ID.  Switch it on!  You'll need the AdWhirl ID later, but not the AdMob one as AdWhirl handles that for you

3. Download the AdMob (or whoever) SDK and add it to the libs folder of your Android app

4. Do the same for AdWhirl

5. Add the jars to the build path of your app (I'm using Eclipse so right click > properties > Java build path > Add Jars

6. Add the following code to your activity

AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);
.setKeywords("some keywords delimited by spaces");
// you can add other stuff here to target your ads, age, gender etc
AdWhirlTargeting.setTestMode(true); // set to false before deploying
AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this, <your AdWhirl ID string>);
//adWhirlLayout.setGravity(gravity); // I haven't set this, but you might want to
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
getWindow().addContentView(adWhirlLayout, layoutParams);

Note that you don't need your activity to implement AdWhirlInterface, it works without it

7. In your AndroidManifest.xml add any permissions requored by the ad vendor (eg AdMob).  AdMob requires:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

8. Also in AndroidManifest.xml add anything required by the ad provider you are using (so in my case AdMob).

            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >

This is necessary because AdWhirl simply mediates the providers, you still need to set the provider-specific activity etc

9. Try it (Note that it seems to take AdWhirl some time to update settings so if you get no ads, check everything is set right, then wait a few minutes)

Yes that is it.  The above displays ads from AdMob using AdWhirl in my app.  It took me ages to work out, so hopefully this will help some others out there!

No comments:

Post a Comment