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.

AdMob
  • 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 http://market.android.com/search?q=pname:com.example.ExampleApp)
  • 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 -DgroupId=com.admob.android.ads -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"
AdWhirl
  • 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 "AdWhirlSDK_Android_2.0.8.zip".
  • 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=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml

choose the galatea-archetype plugin
groupId: org.eoti.android
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]:
        <dependency>
          <groupId>com.adwhirl</groupId>
          <artifactId>AdWhirlSDKAndroid</artifactId>
          <version>2.0.8</version>
        </dependency>
        <dependency>
          <groupId>com.admob.android.ads</groupId>
          <artifactId>AdMobAndroid</artifactId>
          <version>20100331</version>
        </dependency>



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> '
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom"
        android:id="@+id/layout_ad"
    />
   
   
Open your activity (src\main\java\org\eoti\android\AdWhirlTestActivity.java in my case)
Add this into the bottom of your onCreate method:
        try{
            AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR } );
            LinearLayout layout = (LinearLayout)findViewById(R.id.layout_ad);
            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?)

5 comments:

  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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.os.Handler.dispatchMessage(Handler.java:99)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.os.Looper.loop(Looper.java:123)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.app.ActivityThread.main(ActivityThread.java:3948)
    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(Method.java:521)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
    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(ButtonActivity.java:25)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    09-15 12:43:54.879: ERROR/AndroidRuntime(1545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

    Thanks.

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

    ReplyDelete
  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?

    ReplyDelete
  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).

    ReplyDelete
  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.

    ReplyDelete