Search This Blog

27 May 2010

AdMob and AdWhirl

Do you want to display advertisements on your app in hopes of bringing in some money, displaying cross-productization ads or even just so you can make a Lite vs Pro version?  In this example, we will do that using both AdMob (ad server) and AdWhirl (ad aggregator).

To get started, we are going to need to setup a couple of online accounts.

  • Create an account at AdMob.
  • From your dashboard, click on "Sites & Apps".
  • Click on "Add Site/App".
  • Click on "Android App"
  • Fill in the form (I'd ignore the URL for now but eventually it will look like
  • Hit "Continue"
  • Click on "Download AdMob Android SDK"
  • Extract it to the disk somewhere and navigate to it from the command line
 C:\java\admob-sdk-android> mvn install:install-file -Dfile=admob-sdk-android.jar -DartifactId=AdMobAndroid -Dversion=20100331 -Dpackaging=jar
  • Note: I got the version from the top of the Changelog.txt file
  • Click on "Go to Sites/Apps"
  • Under the row for your new app, click on "Manage Settings" (it uses mouse-hover, so if you don't see it move your mouse)
  • Copy the id# from "Publisher ID: a14bfed196e8994"
  • Create an account at AdWhirl.  AdWhirl will act as aggregator, allowing you to mix-n-match ad hosting accounts (like AdMob).
  • Under "Apps" click on "Add Application"
  • Fill out the form. Make sure to choose 'Android'.  Click "Add App"
  • Note: At this point, I have seen an issue where it shows up on the screen twice. If I go into it and come back it resolves itself.  Don't worry about it.
  • Click on your new app's name
  • Copy your SDK key from: "SDK Key:d34d05761f0544dab08b396807778d23"
  • Next to AdMob, click on (mouse-hover again) "Edit Settings"
  • Put the id# from above into the "PublisherID" box and apply it
  • Under "Ad Serving" make sure AdMob is enabled
  • Save Changes
  • Download the latest Android zip from here. In this case, it is "".
  • Extract it to the disk somewhere and navigate to it from the command line
C:\java\AdWhirlSDK_Android_2.0.8> mvn install:install-file -Dfile=AdWhirlSDK_Android_2.0.8.jar -DgroupId=com.adwhirl -DartifactId=AdWhirlSDKAndroid -Dversion=2.0.8 -Dpackaging=jar
  • Note: I took the version from the filename 
Your Code

Whew!  Ok, with your accounts setup, let's create a basic project:

F:\work>  mvn archetype:generate -DarchetypeCatalog=

choose the galatea-archetype plugin
artifactId: AdWhirlTest
version: 1.0-SNAPSHOT

Assuming you have your emulator running...

F:\work>  cd AdWhirlTest
F:\work\AdWhirlTest>  mvn clean install

Add these dependencies to your pom.xml [change versions to match what you did earlier]:

Add this to your AndroidManifest.xml:  

        <uses-permission android:name="android.permission.INTERNET" />
Inside your src\main\android\res\layout\main.xml, insert this just above the bottom '</LinearLayout> '
Open your activity (src\main\java\org\eoti\android\ in my case)
Add this into the bottom of your onCreate method:
            AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR } );
            LinearLayout layout = (LinearLayout)findViewById(;
            AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this, "d34d05761f0544dab08b396807778d23");
            Display d = this.getWindowManager().getDefaultDisplay();
            RelativeLayout.LayoutParams adWhirlLayoutParams = new RelativeLayout.LayoutParams(d.getWidth(), 72);
            layout.addView(adWhirlLayout, adWhirlLayoutParams);
        }catch(Exception e){
            Log.e(TAG, "Unable to create AdWhirlLayout", e);
Note: Make sure to use the SDK Key from the AdWhirl setup earlier when creating the AdWhirlLayout.

Redeploy (mvn clean install) and launch it.  You should see an ad at the bottom of the screen.  If not, check your logs (you do have 'adb logcat' running, right?)


  1. I am having a problem with AdWhirl. I have set up accounts with AdMob, Millennial Media and ZestADS and have followed the instructions but when I try to run my app on my emulator I just get the message 'Application has stopped unexpectedly' which is really frustrating as it all worked fine before. I have coped my logcat below. I am quite new to Adnroid so would appreciate any help you can offer.

    09-15 12:43:54.809: ERROR/AndroidRuntime(1545): Uncaught handler: thread main exiting due to uncaught exception
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sam.donorregister/com.sam.donorregister.ButtonActivity}: java.lang.NullPointerException
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at$1800(
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at$H.handleMessage(
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.os.Handler.dispatchMessage(
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.os.Looper.loop(
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at java.lang.reflect.Method.invokeNative(Native Method)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at java.lang.reflect.Method.invoke(
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at$
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at dalvik.system.NativeStart.main(Native Method)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): Caused by: java.lang.NullPointerException
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at com.sam.donorregister.ButtonActivity.onCreate(
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at


  2. One of the variables on line 25 of is null. Try printing out all the variables to logcat to see which one it is.

  3. Hi Malachi,
    Very informative post. I am having trouble with the AdWhirl integration. I have created the layout in my XML and used the code snippet in the activity. When I launch the app on the emulator nothing comes up where the ad should be. I have the app activated on AdMob and AdWhirl. Does it matter that the app is not published?

  4. I was unable to get it to work in the emulator until I told AdMob to disable Test ads. I am sure that is not the correct solution, but it worked for me (and I started getting real ads).

  5. got it to work, at least with AdMob. I am still having trouble with AdWhirl, but I think it is an issue with the layout. thanks for this post.