Android SDK – Adding AdMob
There are two ways for adding AdMob ads to your apps. The most common way is with the SDK provided by AdMob when you add an app, and the other way is with an alternate AdMob SDK provided by Google. In this tutorial, I will show how you can quickly setup ads in your app using the alternate AdMob SDK. Other then the difference in how the SDK works, I’m not sure what the differences between these are.
The official site can be found here: http://code.google.com/mobile/ads/docs/android/fundamentals.html
I find it interesting that every time I look into working with some SDK, the instructions and sample app that is provided never work. There always seems to be something missing. I don’t know if they simply don’t test when they write these docs, or what. Regardless, I have plugged the whole and will show you every detail so that you can get this working.
One thing I’d like to point out, is that what I demonstrate is the simplest ad functionality, which is enough in most cases. If you really want to dig in, you can get fancy with tracking and even serving ads based on various criteria, such as gender and location. They also have a feature called “Interstitial Ad”, which is defined by Google as…
Interstitials present rich HTML5 experiences or “web apps” at natural app transition points such as launch, video pre-roll or game level load. Web apps are in-app browsing experiences with a simple close button rather than any navigation bar—the content provides its own internal navigation scheme.
The richer, more immersive nature of these ads makes them more expensive and subject to impression constraints.
However, it’s apparently only open to select publishers…
Interstitial ads are currently only offered to a select group of publishers. If you become eligible, Google will be sure to contact you.
Anyway, back to the topic. If you break it down, there are really only four steps that need to be done to get this to work.
- Import the .jar into your project
- Setup the layout xml
- Update AndroidManifest.xml
- Create and request ads in the project .java file
I use Eclipse, so any reference to a development platform will be for that.
Step 1: Import the .jar into your project
Create your Android project as usual and download the sample project HERE. The reason you need the sample project is because it contains the required GoogleAdMobAdsSDK-4.0.2.jar file. After unzip the sample project, drag the libs folder onto the top level of your project in Eclipse. This will copy it and the jar into your project. Finally, link the jar file to your project by right-clicking the top level folder and clicking properties.
Select “Java Build Path” and click the Libraries tab. Now just click the button “Add JARs”. This will bring up another window. Expand your project and the libs folder, then select the jar file. Click Ok, click Ok again and you’re done with step 1.
Step 2: Setup the layout xml
Expand the res—>layout folder and double-click main.xml to open the edit view. If it opens in the “Graphical Layout”, just click the tab on the right, “main.xml”. If you renamed this layout file, that name will be reflected here. The default view already has everything you need, except for a LinearLayout id. Add the following line.
The final xml should look like this.
Step 3: Update AndroidManifest.xml
Directly above the tag, add the following.
And above the tag…
Step 4: Create and request ads in the project .java file
According to the instructions and sample app provided, the following code needs to be added in the onCreate activity after the line, setContentView(R.layout.main); and replace “MY_AD_UNIT_ID” with your AdMob publisher ID.
However, when you try this, you get an error with the number you enter for the publisher ID. It tells you that the variable has not been declared. For some reason, the function is not accepting a string passed directly into it. So to fix this, you need to add the following line above, “AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);”
Replace “YOUR_PUBLISHER_ID” with your publisher ID. What this does is create a variable called “pubID”, and sets it to equal whatever you enter as the publisher ID. Now back to the line where it originally requested your ID and replace “MY_AD_UNIT_ID”, with this new variable, “pubID”.
It works when done this way.
Finally, add these imports at the top, if they have not already been added.
If you run this project, you will start sending requests for ads. While in development, you should enable test mode so that you will get ads every time. When it’s live, you won’t always get an ad, so to make sure it’s setup correctly and if you want to play around with different settings, enable test mode. To do this, add the following before “adView.loadAd(request);”
Ok, run the project and when it starts, you should see an ad. I did notice that even in test mode, the first time you run it, it doesn’t return an ad every time. If you exit, and run the app again from the app drawer, it starts returning ads. When you are ready to go live, just comment out that line, or change “true” to “false”, or just delete this line all together.
You can find my version of this sample app in my downloads, http://taiic.com/downloads
Join our community on facebook. Just hit the Like in the left sidebar (towards the top of the page), this button
Or directly on our fb page at http://www.facebook.com/pages/tAIICcom/116243311786911