Sunday, May 16, 2010

Adding the Mob to Android (Part 2)

So, if you followed my previous AdMob post, you should have your app listed in AdMob, have the SDK downloaded, and have the documentation available. If you don't, you should go look at previous articles in my blog to find the steps followed to get all of these things.

Since I rebuilt my development machine recently, the first step I took was to make sure the code for my app would still run in the emulator. This proved two things to me. 1) My development environment is solid and configured correctly. 2) My program is still working as I would expect it to. I did this because it is always a good idea to know that what you have works before you go sticking new and unknown stuff in to your app.

When I first loaded my project in to Eclipse, I got the error, "Android requires .class compatibility set to 5.0. Please fix project properties." As I recall, there were some requirements on the JDK that is used with Android, so I assume that this error message is a direct indication of what needs to be done to make sure my code still works. But, this error message is also not entirely clear. Have no fear, wizards are here! Right click on your project in Eclipse, then select "Android Tools > Fix Project Properties", and all of our problems are solved!

Now, it is time to get to the meat of the work. For the sake of brevity, I won't recreate all of the information contained in the SDK document you should have already downloaded. Instead, I will reference that document, and provide my own commentary on anything that I feel wasn't explained very well.

If we turn our SDK documents to page 2 (or, the first page following the title page), we see the first few steps to getting AdMob installed. My project didn't have the lib directory, so I went ahead and created it. Following the instructions in step 1 is pretty straightforward. However, I did run in to one thing that wasn't covered. When you create the lib directory, and copy the jar over you need to refresh your project, or else you won't be able to find the jar file you need to add. If you click on your project in Eclipse's package explorer, and hit the F5 key, you will see the lib directory added to your project. Now you should be able to follow the rest of the instructions in step 1.

For the most part, the rest of the instructions make sense. However, the final instruction that indicates how to get a test ad appears to have changed a little. The instructions indicate that you should look in the logcat output for a string that identifies your test device. It may be that this is what you need to do when debugging on an actual device, however, if you are just working in the emulator you only need to add the line "AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR } );" to your onCreate() method.

If you followed all of the instructions carefully, you should now have an ad enabled version of your program! Now go make some money!

3 comments:

  1. Hi,

    I've been stuck for days on this, because i'm a newbie to android dev. :)

    Anyway, please could you show me when on the onCreate() method i should add the

    AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR } );

    This is my onCreate section

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    Thanks in-advance
    Lucy

    ReplyDelete
  2. You can add the AdManager.setTextDevices() call any time after the setContentView() call.

    ReplyDelete
  3. I'm having problems displaying ads in an Android application. I'm not seeing ads even in test mode.
    The layout is taken from admob's instructions, as below:





    The mainfest is below:
























    (I have my real publisher id in my code)
    I my Java code, I have included AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR } ); in the onCreate section, to enable test mode. I've also added log messages in the onFailedToReceiveAd, onFailedToReceiveRefreshedAd, onReceiveAd and onReceiveRefreshedAd functions.
    However I have still ever seen and ad, and can't see any output in the log cat.
    Can you suggest what else I should be doing?

    ReplyDelete