14 May 2012
Experience building apps for Android with Flash Builder and Adobe AIR will help you make the most of this article.
Additional required other products
Intermediate
With the AdMob Extension for Android from Milkman Games, you can rapidly integrate Google AdMob ads into your mobile AIR application using ActionScript 3.
The AdMob Extension for Android is a 100 percent native AdMob Java solution that enables you to:
This tutorial provides details on getting started with the AdMob Android extension. If you want to follow all the steps below, you'll need to purchase and download the AdMob Android extension.
The first step in using the AdMob Android extension is to add the com.milkmangames.nativeexetnsions.AdMob.ane library to your project. (If you are not using Flash Builder 4.6 and later or Flash Professional CS6 and later you'll need to add the AdMobAPI.swc library instead.)
In Flash Professional CS6:
In Flash Builder 4.6:
In FlashDevelop:
You can get your code up and running with the AdMob extension with a few simple calls. See example/AdMobExample.as for a full example that shows how to handle errors, destroy and refresh ads, set ad visibility, and handle ad status changes.
Follow these steps to get started:
com.milkmangames.nativeextensions.android.*;
com.milkmangames.nativeextensions.android.events.*;
AdMob.isSupported property, and initialize the API with your AdMob publisher ID (you can retrieve your publisher ID from the AdMob web site control panel):if(AdMob.isSupported)
{
AdMob.init("YOUR_PUBLISHER_ID");
} else
{
trace(“AdMob only runs on Android.”);
return;
}
AdMob.showAd(AdMobAdType.IAB_BANNER,AdMobAlignment.LEFT,AdMobAlignment.BOTTOM);
android-sdk\platform-tools\adb logcat ) once while running the app. Look for the following line in the output:I/Ads ( 834): To get test ads on this device, call adRequest.addTestDevice("THIS_IS_YOUR_TEST_ID");
Note: This is not the same as the Device ID. You'll need to run logcat at least this one time to find your ad test ID.
var testDevices:Vector.<String>=new Vector.<String>();
testDevices.push("YOUR_TEST_DEVICE_ID_HERE");
AdMob.showAd(AdMobAdType.IAB_BANNER, AdMobAlignment.LEFT, AdMobAlignment.BOTTOM, testDevices);
When you are done with testing, pass an empty testDevices Vector object or null instead.
stage.stageWidth and stage.stageHeight ) as the AdMob extension has no knowledge of any scaling being done by AIR (with the fullscreen property or otherwise). The following example shows an ad at the pixel position 20, 32 (relative to top left of the device):AdMob.showAd(AdMobAdType.IAB_BANNER,AdMobAlignment.LEFT, AdMobAlignment.TOP, testDevices,20,32);
Google has designed the AdMob API in such a way that the absolute physical pixel dimensions of the ads will vary based on the physical size of the device's screen. To precisely layout your application, you may need to know what dimensions the ad will have on the particular device. You can access these dimensions with the getPixelSize() function, which returns a Rectangle object that's width and height represent the real size in pixels, on the current phone or tablet, that an IAB_BANNER will display. For convenience, you can also read the .dimensions property of the AdMobAdEvent object to get this in an event listener function. You can use this information to accurately perform layouts based on the size of the ad. Here is an example function call:
var realSize:Rectangle=AdMobAdType.getPixelSize(AdMobAdType.IAB_BANNER);
Unfortunately, this means the size of the ad (in pixels) will vary based on the phone or tablet the ad runs on. However, the vast majority of Android devices will run in one of two modes that Google calls large and xlarge.
Almost all Android phones fall into the large category and will have banners that are 480 x 75 pixels. Almost all Android tablets and some of newer, larger Android phones are in the xlarge category and will have banners that are 640 x 100 pixels.
The large and xlarge dimensions are intended as guidelines; you should use the getPixelSize() method described above for accurately determining banner sizes on your own devices.
You need to configure your AIR application descriptor file to use the AIR 3.0 SDK (or later), include the AdMob extension, and update the Android manifest additions with some AdMob specific settings. For a working example, see example/app.xml.
<application xmlns="http://ns.adobe.com/air/application/3.0">
<extensions>
<extensionID>com.milkmangames.extensions.AdMob</extensionID>
</extensions>
android.permission.INTERNET and android.permission.ACCESS_NETWORK_STATE permissions must be present for AdMob to work. You also must add the com.google.ads.AdActivity activity within the <application> element, as shown below:<android>
<manifestAdditions>
<![CDATA[<manifest android:installLocation="auto">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application>
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation" />
</application>
</manifest>]]>
</manifestAdditions>
</android>
If you're using Flash Builder 4.6 or later, or Flash Professional CS6 or later, and have added the AdMob Android extension library as described above, then you can compile as you usually do directly from the IDE. If not and you are building your app with the extension from the command line, then you'll need to specify the directory containing the com.milkmangames.nativeextensions.AdMob.ane file.
Here is an example build command line:
[PATH_TO_AIR_SDK]\bin\adt -package -target apk-debug -storetype pkcs12 –keystore [YOUR_KEYSTORE_FILE] -storepass [YOUR_PASSWORD] anesample.apk app.xml anesample.swf –extdir [DIRECTORY_CONTAINING_ANE_FILE]
If you're having trouble displaying ads, try these tips:
AdMob.init().showAd(). See example/AdMobExample.as for a sample of how to do this.FAILED_TO_RECEIVE_AD event:AdMob.addEventListener(AdMobErrorEvent.FAILED_TO_RECEIVE_AD, onFailedReceiveAd);
android-sdk/platform-tools/adb logcat to review full debug text from the AdMob extension.Now that you have the AdMob Android extension up and running, you may want to explore the ActionScript 3 documentation or check out the other tools from available from Milkman Games.

This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.