Integrating AdMob in your Android App and make money

As an android developer, I write android apps and publish them on the market mostly for free as I try to build a reputation :). But, wouldn’t it be great if I could offer my app for free at the same time make some $$ through “Advertisement”? In this post I will go through integrating AdMob in an android application and start making some money (at least few cents ;)).

If you want to learn more about AdMob, please visit their website http://www.admob.com/

Let’s get started:

Step 1: Registering with AdMob
First of all you need to register yourself with AdMob. You can do this by simply clicking on “Register” Link when you visit “www.admob.com”.

You can find more info about registration in http://developer.admob.com/wiki/PublisherSetup

Step 2: Setting up AdMob with your App

Once you are registred, login to the site and follow link to http://www.admob.com/appdevs You can get here by clicking on “Android” Icon on the homepage. (At the time of this writing AdMob website has an icon in the center of the page that features, iPhone, Android etc, I assume that link above should still work.)

i. Click on “Get Started” and then click on “Sites & Apps”. You can follow the link http://www.admob.com/my_sites/

ii. Click on “Add Site/App” button, this should take you to a setup wizard. In this example we will be working on an android app. Click on “Android App”

iii. Provide the Details. Put your app name under “App Name” (In this example I used “Demo”), As this is a new app and we haven’t published the app yet, you can put your website in the “Android Package URL”. (This is a required filed, so you have have something here). Select a category and add description and click “Continue”.

iv. Thats it you have successfully set up your “App” on AdMob. (You will see a page from where you should be able to download the admob library, if you do this now, you can skip Step 3 below.)

Step 3: Getting the AdMob library jar

If you did not download the jar that you saw at the end of Step 2. You can still get there by navigating to http://www.admob.com/my_sites/ You should see the app you registered in Step 2.  If you hover over the app name, you should see “Manage Settings”, Click on that. This should land you to a page from where you can get the library by clicking on “Get Publisher Code”.

Step 4: Setting up an Android App

 Let’s create an Android a Demo app. You can do this by going into Eclipse or any IDE with Android SDK and creating a new project.

Step 5: Adding the Library:

Once you have the android project set up create a directory called “libs”, and add the jar file you downloaded in step 3.

Now, go to the project properties by right clicking on the project name and clicking on “Properties”.  Go to “Java Build Path”, then on the right side, click on “Libraries” tab.

To add the jar, click on “Add JARs…” button and locate the Jar file in the libs folder in your project.

Step 6: Setting up a view

Since you added the lib and if you do not see any project problems you are all set. Now this should allow us to use a “AdView” in your android app. This is the view where AdMob will place a HTML5 advertisement.

There are two ways we can add this view, 1. in the Activity java code or 2. in the Layout XML. For Simplicity, lets do the XML one.

If you have the project set up lets go to the main layout of the app found in “res>layout” directory in your project. Locate “main.xml”.

In the XML, Lets Copy Paste the following XML in your layout.

<com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="YOUR_PUBLISHER_ID HERE"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="true"/>

Replace “YOUR_PUBLISHER_ID HERE”, with id you got while in Step 3.

Also, Include following namespace code int he Layout tag.


xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

So your final code should look something like this


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
    <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="a14e2f8fe3af5a6"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="true"/>
</LinearLayout>

Step 7: Setting the Manifest
AdMob requires internet and network access, so it can pull down the ad to display on your app. So, Lets update the AndroidManifest.xml.
Edit the AndroidManifest.xml file found in your project. Add Following Activity right below the current activity.

    <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation"/>

And Now, add following permissions

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

Your Manifest should look something like:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.novaapps.android.couponmanager"
      android:versionCode="1"
      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CouponManager"
                  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"/>

    </application>

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

</manifest>

Step 8: Done!
That’s it, now you can run your demo app and see the admob advertisement show up in your app!
(Note: It may take a while for the app to appear on the screen as it is being set up for the very first time, it should be faster in subsequent loads)

You can learn more Here: http://code.google.com/mobile/ads/docs/android/fundamentals.html

Advertisements

16 thoughts on “Integrating AdMob in your Android App and make money

  1. Hi, thank for tutorial.
    have question:
    always get some error in logCat:

    10-17 16:29:39.858: INFO/Ads(6491): onFailedToReceiveAd(Ad request successful, but no ad returned due to lack of ad inventory.)

    can you help me?

    Thank

    1. Hi Valdimir,
      It seems like to me that AdMob is not sending you ads to be displayed. There are few things you can try.
      AdMob provides a “Test” flag you can send that will make sure u get an ad. (might want to check their help, i can look into it further later today)
      Other is to make sure there aren’t any filters set that that is restricting ads you can see. You can check that by going to your admob account go into the ad settings, and “Category / Type Settings”, make sure you have list of adds that are “running”

      Let me know if this works.

      Thanks,

  2. I had GoogleAdMobAdsSdk-6.1.0.jar for AdMob
    You have to add more configChanges attributes as mentioned above.
    Otherwise it will not work…

    1. Hi Yash,
      Sometimes it take a while for them to start sending you ads. Wait for few hrs and try again. Also try listing your device as testDevice so it will try to load an ad as soon as view loads.
      >> ads:testDevices=”TEST_EMULATOR, TEST_DEVICE_ID”

      See the full xml here:

       <com.google.ads.AdView android:id="@+id/adView"
                               android:layout_width="wrap_content"
                               android:layout_height="wrap_content"
                               ads:adUnitId="MY_AD_UNIT_ID"
                               ads:adSize="BANNER"
                               ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                               ads:loadAdOnCreate="true"/>
      

      and don’t forget to put xmlns

      xmlns:ads=”http://schemas.android.com/apk/lib/com.google.ads”

      1. You can find your hashed Device ID in the logcat output by requesting an ad when debugging on your device. Should look something similar to “AC98C820A50B4AD8A2106EDE96FB87D4”

      2. I set the ad completely as you told but it now shows that “you should declare ads activity in androidmanifest.xml with config changes ” What to do now ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s