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
chart

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="http://schemas.android.com/apk/res/android"
    package="com.jms.admobgoogleplayexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

	<uses-permission android:name="android.permission.INTERNET"/>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>
        
        <activity
            android:name="com.jms.admobgoogleplayexample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>

</manifest>

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="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:id="@+id/rootViewGroup"
    >
    
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="@string/ads_string"
    android:textSize="30sp"
    android:layout_gravity="center_horizontal"
    />
</LinearLayout>

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) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		LinearLayout rootLayout = (LinearLayout) findViewById(R.id.rootViewGroup);
		adView = new AdView(this);
		adView.setAdSize(AdSize.SMART_BANNER);
		adView.setAdUnitId(unitid);
		rootLayout.addView(adView, 0);
		
		AdRequest adRequest = new AdRequest.Builder().build();
		adView.loadAd(adRequest);
	}

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:

-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20120913-144807
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
1024m
--launcher.defaultAction
openFile
-vmargs
-Xms1024m
-Xmx1024m

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 android.support.v7.app 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.
getsupportlibrary

getsupportlibrary2

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
chart

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

Previous PostNext Post

50 Comments

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

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

  2. 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);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(unitid);
    rootLayout.addView(adView, 0);

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

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

        1. Are you using my source code? Please verify that you import the write library:


          import com.google.android.gms.ads.AdRequest;
          import com.google.android.gms.ads.AdSize;
          import com.google.android.gms.ads.AdView;

  3. 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.google.android.gms.ads.AdRequest, com.google.android.gms.ads.AdSize, com libraries. google.android.gms.ads.AdView.

    The MainActivity shows me the following error: The import can not be resolved com.google.android.gms.ads

        1. So my code can work properly with your google play services library. But your own project doesn’t. That’s weird. Do you try to clean your project and rebuild? Do you add google play services library in your project library? You’d better to double check all possible situation.

          Thanks

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

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

          Thanks

          1. I resolved this issues …i download fresh SDK ….now it’s working fine ….now feel very happy ………Thnks a lot James…

  4. 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(R.id.rootViewGroup);
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);
    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

    1. Hi,

      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.

      Thanks

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

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

      Thanks

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

    1. 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
      James

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

    1. 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
      James

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

    Regards
    Belkis Sandoval

  9. Hello
    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
    Pawel

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

      Thanks

  10. hello^^
    I need your help.
    in my program. below is my erro.
    com.google.android.gms.ads.Adview cannot be cast to android.widget.linearlayout.

    please help me how to solve

  11. 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 …….????

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

  12. 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…

    1. 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!

  13. 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 ‘android.app.AppOpsManager’, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza

    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

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

  14. What’s up everyone, it’s my first pay a quick visit at this site, and article is genuinely fruitful in support of me, keep up posting such content.

Leave a Reply to Bhushan Cancel reply

Your email address will not be published. Required fields are marked *