Add AdMob with Google Play Service

On April 9, 2014, in Android, Make Money Online, by James Liu

The Admob service is integrated in Google Play. After August 1, 2014, Google Play will stop accepting new or updated apps which are using the standalone AdMob SDK. Adding AdMob in android app seems to be easy. But it still takes me half of the day to run the app on my android phone successfully. In this Android example, I will show you how to integrate AdMob from Google Play services in your android apps.

Try Example Android App

You can download and install this android app by click the following link, or scan the following QR code to download and install on your android phone simply. If you feel it is helpful, please support me by clicking the banner inside the app.

Download and Run AdMob Example App on Your Android Phone

Download Google Play Services Lib

To use Google Play service, you need to download Google Play services package in Android SDK Manager. If your app wants to support Froyo, android 2.2, you have to download Google Play services for Froyo. After you get everything done, your Android SDK Manager will look like this.

Android SDK Manager

Import Google Play Services Lib into Eclipse

Then, you have to add google play services into your Eclipse project. From google document, there is a step by step instruction. But you will get Invalid project description error when you are trying to import google play services lib into Eclipse project.

import google play services problem

Here are the right steps to import google play services lib in Eclipse.

  • Goto Menu File and select Import
  • Select “Existing Android Code Into Workspace” in Android folder in popup window
  • Click “Browse” to find the google player services lib folder. In my example, the location is:
  • D:\android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib

  • Check “Copy projects into workspace” option
  • Click “Finish” button

import google play services
import google play services 2

Add Google Play Services Lib into Android Project

Now, you need to add google play services library into your android project. Follow these step by step instructions:

  • Right click on your android project and select “Properties”
  • Select “Android” section
  • Click “Add” in library area
  • Select “google-play-services_lib” and click “OK”
  • Click “Apply” and click “OK”

add google play service library
add google play service library 1
add google play service library 2

Add AdMob In Android App Source Code Example

Now, you are able to add AdMob in your android app. Please edit your android app AndroidManifest.xml by adding an extra meta-data and activity. Please check following example:

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

        android:targetSdkVersion="19" />

	<uses-permission android:name="android.permission.INTERNET"/>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        android:theme="@style/AppTheme" >
        <meta-data android:name=""
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
        <activity android:name=""


In my AdMob example app, I will put a AdMob on the top and show a text below the ads banner. The following is my app layout source code example:

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

In my main activity class onCreate function, I create an AdView and show it in the LinearLayout. Please this source code:

	protected void onCreate(Bundle savedInstanceState) {
		LinearLayout rootLayout = (LinearLayout) findViewById(;
		adView = new AdView(this);
		rootLayout.addView(adView, 0);
		AdRequest adRequest = new AdRequest.Builder().build();

Compile Errors And Solutions

When I am testing the above example, I meet lots of unacceptable problems. Here are some problems and solutions. The first problem is GC overhead limit exceeded problem after I import Google play service into old project. When I am running the app, my eclipse hang and go out of memory.

Unable to execute dex: GC overhead limit exceeded
Conversion to Dalvik format failed: Unable to execute dex: GC overhead limit exceeded

Google and stackoverflow provide an answer that we must change in eclipse.ini

change -Xms40m to -Xms1024m
change -Xmx384m to -Xmx1024m

After increasing memory in eclipse.ini as is indicated in various posts, I obtain a new error:

Unable to execute dex: GC overhead limit exceeded
GC overhead limit exceeded

To solve above new error, I have to change more in eclipse.ini. Here is my final version without any problems:


At the beginning, I also get another compile error.

android-apt-compiler: /blahblah/AndroidManifest.xml:409: error: Error: No resource found that matches the given name (at ‘value’ with value ‘@integer/google_play_services_version’).

To solve the problem, I need to link Google-play-services library properly with my project. To import and add google play services library properly, please check this:
Import Google Play Services Lib into Eclipse
Add Google Play Services Lib into Android Project

If you try to install my example app on your android phone and get following error, it is because your android version is too older than android 2.3.

Parse error
There is a problem parsing the package.

After you download my example project source and import into your own eclipse workspace, you may face these problem:

The import cannot be resolved

That is because you are missing the appcompat_v7 library project. It is a support library from Android to provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. To solve this problem, you can just create a new project in your Eclipse and keep minimum required SDK as API8 or API9 and set target SDK as the latest version. Then, click next and next to create a new project. After that, the project appcompat_v7 will appear in your project explorer.


AdMob Example In The App

After running the example app on my phone, I get the ads banner successfully. Here is the screen shot of the result.

Another very common error problem is like:

AdRequest cannot be resolved to a type
AdSize cannot be resolved to a type
AdViewcannot be resolved to a type

These are basically because there is no google play services lib in your project. You can follow the step Import Google Play Services Lib into Eclipse to add it in your project and the problem will be solved.

Try Example Android App

You can download and install this android app by click the following link, or scan the following QR code to download and install on your android phone simply. If you feel it is helpful, please support me by clicking the banner inside the app.

Download and Run AdMob Example App on Your Android Phone

Download Android Example Source Code

I have put my android AdMob example source code online and you can download it and try with your own AdMob ads unit. Please remember to import google play services library into your Eclipse and add it in my example project.
Download Source Code

Tagged with:  

49 Responses to “Add AdMob with Google Play Service”

  1. Freddy Marin says:

    Hello i need Library appcompat_v7 the zip does have this library

    • James says:

      Can you let me know what version of your eclipse and android sdk? If you are using the latest eclipse and android sdk, it will automatically create a appcompat_v7 library for you when you create a new Android project.

  2. Alper Kahriman says:

    Thank You very much. You saved me. I have worked for 3 day on this issue. I have a lot of apps to update. You are great.

  3. Robin says:

    Hello Sir,

    I just want to add admob in my android app but I dont find any way I tried a lots but can’t find a better way or step by step way to add. For example I did this way but it shows me error when I add the

    adView = new AdView(this);
    rootLayout.addView(adView, 0);

    AdRequest adRequest = new AdRequest.Builder().build();

    This line it shows me error sign .. I don’t know how to resolve this help me please if You can..

  4. James says:

    As your error information, do you add “google-play-services_lib” in your project?

  5. Pedro says:

    Hi James, I have searched in google much information about errors that occur to me and you are the only person that your code works perfectly. I congratulate you.

    But when I make a new project and adding the libraries appcompat_v7 google-play-services-lob and try to import the,, com libraries.

    The MainActivity shows me the following error: The import can not be resolved

  6. Mahesh says:

    I work on last 7 days but i did’t get any positive response …please help me…

    • James says:

      OK. Let’s check it step by step. You’d better to give us some screenshot.

      1. Show me your screenshot about how to import the google play services lib with the library path
      2. Show me your activity source code, the most simple one
      3. Show me your manifest.xml

      BTW, I have to make this clear. I am not helping you solve your homework though

    • Pedro Lama says:

      Hi Mahesh,

      Maybe you have the same problem as I had, in the Libs folder, just delete android-support-v4.jar and try add admob with Google-Play Library following the steps in the link below:

      • Mahesh says:

        face same problem…please help…

        • James says:

          Hello Mahesh,

          I remembered that you have sent me your project. Then I tried your project in my eclipse, it worked properly. After that, I sent you one email about that, with some screenshot.

          Since that, I haven’t got your reply. I still want to know what your situation now.


  7. RobB says:

    Hi James,

    Your tutorial is nice and very clear. I have one issue with mine and it is probably staring me in the face but when i run my code it errors.

    LinearLayout rootLayout = (LinearLayout) findViewById(;
    adView = new AdView(this);
    rootLayout.addView(adView, 0);

    rootLayout is null and causing a null pointer exception. Any ideas what this could be. I have copied your xml code for the view and put it in a new xml file called adverts.xml

    • James says:


      I think the rootViewGroup is the id of your layout id. You can find it in my layout file source code. By the way, it is the container of the admob banner.


  8. RobB says:

    Hi James i managed to fix this i wasnt using the content view id.

    my root layout is a relative layout, does this matter? I dont see any ad

    • James says:

      Yes, it does matter. If you are using relative layout, it’s hard to control the position of your ad banner. My suggestion is you can create a layout in your root layout which is for hosting your ad banner. Then, you can place your ad banner in that layout.

      After that, please don’t forget to replace my “AD_UNIT_ID” in your code.


  9. tufail says:

    Thank your very much for this post. This post is very help for me. I want to asked one question can we set width and height of banner in java code.

    • James says:

      Hello Tufail,

      You can set the size of your banner container, but you cannot set the size of banner directly. And you have to watch out, if your container’s size is smaller than the banner’s size. The banner will not be shown.

      I suggest you can use the admob smart banner. In my post: How to Add Smart Banners, I list all admob banner size. You can have a look.

      Best Regards

  10. Elroy Toscano says:

    Hello James, I am facing a very weird problem. I have imported the google play services lib into my workspace and even added it to my project. However, when I run the application,it gives me an error saying “Unable to execute dex: Java heap space
    Java heap space”. I have increased the xms and xmx parameters. However, the problem still persists. And I’m on the last step of publishing my application. So any help is appreciated.

    • James says:

      Hello Elroy,

      I think your problem is because of lacking of memory. I have pasted my eclipse.ini in the post. You can take a look.

      Best Regards

  11. XtremeSolution says:

    who help me in this, as I put a google ad admob to Play Servises in eclipse in my game without activity

    Belkis Sandoval

  12. Pawel Lewinski says:

    I found your tutorial the easies to catch for the newbie like me – thanks. I got strange situatuion unfortunately – I got GP_services library via SDK manager and it is installed. But when I try to import it as you described I have message “No projecta are found to import”.
    If this is not a big problem I should appreciate any reply
    Thanks in advance

    • James says:

      Hi, your problem is quite weird. I guess your google library may be broken. My suggestion is delete your google library from SDK manager, and reinstall it.

      Every time when you import the library, please check the “Copy projects into workspace” option. This option will avoid to damage the library source code.


  13. BlissA says:

    I need your help.
    in my program. below is my erro. cannot be cast to android.widget.linearlayout.

    please help me how to solve

  14. Jenny says:

    Thanks, this was a life saver 🙂

  15. Luan Nguyen says:

    Very helpful, thank you very much.

  16. khalid says:

    i purshace a code source of game application
    pleas how can me change old google admob SDK with new “google play service” in this code …….????

    • James says:

      First of all, delete the old Google AdMob SDK and import the “Google Play Service” library. Then, following my tutorial to let AdMob work in your app.

  17. Muhammad Hasem says:

    Hellow Sir,

    I done all that you tell in the above tutorial. But there is also exception error “Application has stopped unexpectedly”.

    Please help me…

    • James says:

      I think it could be a very small mistake in your project. Usually, it could be very simple to be solved. Just be careful and patient. Good Luck!

  18. chinni says:

    Why this is working on some devices but not not on other devices

  19. Arnold says:

    I was able to do all the steps above from my research of each error. But
    still the ad failed to load. Here is the error:
    Could not find class ‘’, referenced from method

    But the android game load with a blank white above the game that displayed on the screen. I am using eclipse and not android studio. I also change the settings in the eclipse.ini and add both appcompatv7 and google play services package thru sdk manager inside eclipse.

    Thank you

    • James Liu says:

      I think it may not be your code problem. I never meet with this problem before. You can search on Google about your term, hope you can find some solutions.

  20. Parth Raval says:

    Thanks brother. You saved my time.

Leave a Reply


WordPress Themes