Add Google Admob in Android Application

On May 1, 2011, in Android, by James Liu

Latest Update: Add AdMob with Google Play Service

Related Post:

Old Post About AdMob:
Add AdMob v6.0 to Android Apps
Add Admob To Your Android Apps With SDK 4.3.1

Admob is a new way to monetize mobile applications. Currently, it supports multi-platform, including iPhone, Android, WebOS, and Flash Lite. In this AdMob for Android example, I will show you how to integrate Admob in your android applications defined in layout XML file.

Try Example: download and run AdMobExample.apk on your android phone

First of all, we need to go to the Admob website to register an account, in order to get the Admod account ID. Then, let’s go the google code to download the latest Google AdMob Ads SDK for android. You also can get download it from your admob “Sites & Apps” setting page. After we get the AdMob SDK, we need to add the AdMob sdk jar in our android project. For those who want to implement the AdMob in Android Java code, you can copy the Jar file in the project and include it in your android project Build path. For more details, please refer the post Add AdMob v6.0 to Android Apps. If you want to layout the AdView in layout xml file, you need to add jar file in the /libs/ folder under your android project.

AdMob Build Path

AdMob Build Path

1. For Admob, it’s better to add the in your Manifest.xml file. And don’t forget to add the user permission for it. Below is my mainfest.xml example.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
	<uses-sdk android:minSdkVersion="8"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AdmobExample"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
		<activity android:name=""

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

2. The basic class in android applications are composed of View objects. For Admob, we need use AdView class which is provided in the SDK jar file. Actually, it will be put into the main layout file, it’s easy for you to build your own layout. Let’s see my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearLayout xmlns:android=""


  	ads:adUnitId="Your Admob ID"

3. Send AdMob AdRequest in your Activity class:

        AdView adview = (AdView)findViewById(;
        AdRequest re = new AdRequest();

There are a most important things you need to know:

  • At the first time, the Admob Ads will take 1 or 2 minutes to show.
  • In the simulator, I never try it successfully (As Alan Jay Weiner‘s solution, it maybe because I have two INCs, internet adapter, in my computer. His solution is that disable all other NICs in computer, only leave one INC working which can access the internet, and restart the emulator. Then, it will work properly. Thanks Alan. ). I always get the time out error. I only try it in my HTC Desire, and it really works.
  • Some times, it will show no ads to show. It’s not the problem, because it really doesn’t have ads to show for some situations.

Compile Errors Solutions

One common compiling error is “Error inflating class”. The error message is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jms/com.jms.InfiniteGalleryActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class

This is one of the compile errors which many android programmers meet with. The solution is creating the /libs folder under your android project root folder and putting the AdMob Jar file inside.

AdMob Build Path

AdMob Build Path

Another compiling error is “Required XML attribute missing”. The error message is:

Could not initialize AdView: Required XML attribute “adSize” missing

Thanks Thomas giving me the suggestion. This is because the Android can not find the AdSize defination. It can be solved by specifying the right ads name spacing in the layout xml:

Tagged with:  

39 Responses to “Add Google Admob in Android Application”

  1. Mustafa says:

    Thank you very much, this is superb article…

  2. Peerless says:

    Whoa, thngis just got a whole lot easier.

  3. Helen says:

    Sorry for what might seem like a very simple question but I am a total novice on this. I have an android app built in flash cs5 which I have yet to publish but it’s pretty much ready to go. Want to add adverts in but I’m not sure what you mean by “android application Build Path”. I’m not a developer so some of this language is totally new to me but I’m trying to figure it out. Any help would be great 🙂

    • James says:

      It means you need to right click your project, and add the jar in “Build Path” configuration. Any questions, let me know.

  4. Elgin says:

    [2011-08-20 22:40:54 – TrickshotLite] D:\EclipProj\TrickshotLite\res\layout\main.xml:12: error: No resource identifier found for attribute ‘adSize’ in package ‘com.jms.AdmobExample’
    [2011-08-20 22:40:54 – TrickshotLite] D:\EclipProj\TrickshotLite\res\layout\main.xml:12: error: No resource identifier found for attribute ‘adUnitId’ in package ‘com.jms.AdmobExample’

    I am getting these errors. I did not have an attrs.xml file so I created one. Any Ideas?

    • James says:

      Yes, you need to create this “attrs.xml” file. As the example says, the full path is “res/values/attrs.xml”. Create it manually, then copy and paste the example code in it.

      Let me know if u have any other problem. 😛

  5. Alan Jay Weiner says:

    About your not getting ads in the emulator – you should be able to (I do…).

    Do you have more than one NIC in the machine on which you’re running the emulator?

    I do, and using Wireshark could see that the emulator was trying to get to the Internet by the wrong NIC.

    I disabled that NIC, reloaded the emulator and it worked properly. Annoying to have to play that dance – disable the NIC for the emulator, re-enable it when I want to use that network – but it works. Once the emulator is running, you can re-enable the other NIC until you close and restart the emulator.

  6. javo says:

    i get this error AdView missing required XML attribute “adSize” pls help…

  7. Air Android Tutorial says:

    you need to add this file “attrs.xml”, in which you need to define the “adSize”

  8. john says:

    is it possible to convert in actionscript. I ‘d like to add in android app and I have adobe flash pro cs 5.5 and AIR and actionscript 3.0. 🙂

    can I import-add only this xml file ?
    Do not need to add the .jar file which I download it from adMob account?

  9. Jelle says:

    I get these errors in my java activity file:
    Adview can not be resolved to a type
    Adrequest can not be resolved to a type
    Adview can not be resolved to a variable

    Do you know what I have done wrong?

  10. Rupesh says:

    Hi tried your code but my code stuck at line setContentView(R.layout.main);
    i add attrs.xml but he cannot find adSize.showing adSize missing.error – android.view.InflateException. Error inflating class

  11. thomas says:

    Hi Jamas

    I have to use admob this way only how can i solve the adSize error
    Please help me out.

  12. thomas says:

    i solved “AdView missing required XML attribute “adSize”” problem by replacing xmlns:ads=”” with xmlns:ads=””

  13. pankaj says:

    hello i have made this admob example but i can see only one add on device i want more add randomly change can u have solution or code pls suggest me
    thanks in advance

  14. Rizwan Ahmed says:


    I am getting syntax here. String type not allowed

  15. Darren says:

    Hey thanks for the article,

    I’m having a problem. The app doesn’t show any ads. I see “Above is the Ad Banner which is returned by Admob!” but there is no ad. I’ve left it for a long time. I have reinstalled the apk.

    In my Admob dashboard my site status is still red.

    -I am testing on my Galaxy Note with Android 4.1.1
    -The AdmobExample is set to 4.0.3
    -I used your AdmobExample and changed my Publisher ID

    I am going to create an emulator to try test.

    (I also downloaded your admobexample apk here and I also see no ads there.

    Do you have any suggestions?

    • James says:

      Hi, Darren,

      I am using samsung galaxy tablet and my htc desire to test the example. Both of my device can get the ads. I think it also depends on your location. If there is no related ads in your region, the admob may not return any ads. This is what I guess.

      If you have any questions, let me know.

  16. Vladislav says:


    Hello from Ukraine
    I have a problem
    Pleace help me if you can.

  17. Vladislav says:


    Hello from Ukraine
    I have a problem
    Pleace help me

  18. erico256 says:

    Great article… very helpful.
    Thanks a mil

  19. Reena says:

    Thanks a lot its working

  20. Danish says:

    What if you are getting the same error with Adobe Flash CS5???

  21. jon inazio says:

    excellent! you helped me out!!

Leave a Reply


Premium WordPress Themes