Latest Update: Add AdMob with Google Play Service
AdMob released AdMob sdk v6.0.1 for android a few days ago. It is compatible with my last AdMob tutorial, but I think it is better to create a new tutorial to show how to add AdMob ads to our android apps. In this tutorial, I am using:

  • Eclipse SDK Version: 3.7.2
  • ADT 18.0
  • Android SDK r18
  • AdMob v6.0.1 for Android
  • HTC Desire with 2.3.7


First, we go to the AdMob SDK download page to get the latest Admob v6.0.1 for android. Unzip the AdMob SDK package and copy AdMob SDK jar to our AdMob project folder.

AdMob SDK v6.0 Folder Structure
AdMob SDK v6.0 Folder Structure

Second, go to project properties window, add AdMob jar to Java build path.

Add AdMob SDK to Java Build Path
Add AdMob SDK to Java Build Path

Third, declare com.google.ads.AdActivity and add networking permissions INTERNET and ACCESS_NETWORK_STATE in project’s AndroidManifest.xml. AndroidManifest.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jms.AdmobExample"
      android:versionCode="2"
      android:versionName="1.1">
	<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AdmobExample"
                  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.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
    
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

Fourth, add AdView instance in our app’s Activity:

package com.jms.AdmobExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.google.ads.*;

public class AdmobExample extends Activity {
    /** Called when the activity is first created. */
	private AdView myAdView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myAdView = new AdView(this, AdSize.BANNER, "Your Publish ID");
        
        //get layoutView
        LinearLayout rootView = (LinearLayout)this.findViewById(R.id.rootViewGroup);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(480, 75);
        rootView.addView(myAdView, 0, layoutParams);        
        
        AdRequest re = new AdRequest();
        re.setGender(AdRequest.Gender.FEMALE);
        //re.setTestDevices(testDevices);
        //re.setTesting(testing)
        myAdView.loadAd(re);
    }
}

Solution for Error: String types not allowed at configChanges

Some friends meet the following error when running my example source code. The problem of this error is that Google AdMob Ads SDK for Andoird requires at least Android 1.5 and we must compiling the app target to Android v3.2 or API Level 13.

error: Error: String types not allowed (at ‘configChanges’ with value ‘keyboard|keyboardHidden|orientation|
screenLayout|uiMode|screenSize|smallestScreenSize’).

Build App Targeting to Android 3.2 API Level 13
Build App Targeting to Android 3.2 API Level 13

When you want to make your app compatible with version earlier than Android 3.2, you can set the “android:minSdkVersion” attribute in the AndroidManifest.xml file.

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>

Try AdMob example apk here.
Try source code here.

Previous PostNext Post

8 Comments

  1. manifest file shows followin error

    error: Error: String types not allowed (at ‘configChanges’ with value ‘keyboard|keyboardHidden|orientation|screenLayout|uiMode|
    screenSize|smallestScreenSize’).

    what to do ?

  2. Hi raman,

    First, please make sure that you are using the latest google android sdk, and ADT. And also make sure the your AdMob is the latest version.

Leave a Reply

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