Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center /

Using the AdMob Adobe AIR native extension for Android

by Alex Liebert

Alex Liebert
  • Milkman Games

Content

  • Including the AdMobAPI Library
  • Getting started with the AdMob extension API
  • Updating your AIR application descriptor file
  • Building and troubleshooting the application
  • Where to go from here

Created

14 May 2012

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript Adobe AIR advertising Android Flash Builder Flash Professional gaming Native extensions screen size

Requirements

Prerequisite knowledge

Experience building apps for Android with Flash Builder and Adobe AIR will help you make the most of this article.

 

Additional required other products

  • Milkman Games AdMob Android Extension

User level

Intermediate

Required products

  • Flash Builder (Download trial)
  • Flash Professional (Download trial)
  • Adobe AIR SDK

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:

  • Monetize free games via Google AdMob
  • Display your first ad with just two lines of code
  • Jumpstart development with a complete sample application, ActionScript 3 documentation, and a getting started guide
  • Reposition ads automatically when your app changes orientation
  • Support AdMob banner formats on both phones and tablets
  • Register your test devices so you can develop without affecting your metrics
  • Create, destroy, show, and hide ads with simple commands

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.

Including the AdMobAPI Library

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:

  1. Create a new project of the type AIR for Android.
  2. Choose File > Publish Settings.
  3. Click the wrench icon next to Script for ActionScript Settings.
  4. Select the Library Path tab.
  5. Click Browse For Native Extension (ANE) File and select the com.milkmangames.nativeextensions.AdMob.ane file.

In Flash Builder 4.6:

  1. Go to Project Properties (right-click your project in Package Explorer and select Properties).
  2. Select ActionScript Build Path and click the Native Extensions tab.
  3. Click Add ANE and navigate to the com.milkmangames.nativeextensions.AdMob.ane file.

In FlashDevelop:

  1. Copy the AdMobAPI.swc file to your project folder.
  2. In the explorer panel, right-click the SWC and select Add To Library.
  3. Right-click the SWC file in the explorer panel again, select Options, and then select External Library.

Getting started with the AdMob extension API

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:

  1. Import the API Classes:
com.milkmangames.nativeextensions.android.*; com.milkmangames.nativeextensions.android.events.*;
  1. Verify that AdMob is supported on your current platform, using the 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; }
  1. Show your first ad. The following call displays a banner ad, with bottom-left alignment:
AdMob.showAd(AdMobAdType.IAB_BANNER,AdMobAlignment.LEFT,AdMobAlignment.BOTTOM);
  1. To avoid making ad impressions while testing, you can set a list of testDevice IDs. You can retrieve the test ID for your device by running adb logcat (the command is 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.

  1. To use absolute pixel positions for your ad you can supply x and y offsets as the fifth and sixth parameters to showAd() respectively. Note that the pixel offsets are relative to the true size of the screen (which you can read with 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.

Updating your AIR application descriptor file

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.

  1. Set your AIR SDK to 3.0 or later in the app descriptor file:
<application xmlns="http://ns.adobe.com/air/application/3.0">
  1. Include a link to the extension in the descriptor:
<extensions> <extensionID>com.milkmangames.extensions.AdMob</extensionID> </extensions>
  1. Update your Android manifest additions. The 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>

Building and troubleshooting the application

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:

  • Ensure that your publisher ID is correctly set via AdMob.init().
  • Ensure you have ad inventory. You can guarantee an ad will display by setting your test Device's ID when you call showAd(). See example/AdMobExample.as for a sample of how to do this.
  • Check for errors retrieving ads by listening for the FAILED_TO_RECEIVE_AD event:
AdMob.addEventListener(AdMobErrorEvent.FAILED_TO_RECEIVE_AD, onFailedReceiveAd);
  • Execute android-sdk/platform-tools/adb logcat to review full debug text from the AdMob extension.
  • Make sure you're handling all possible AdMobErrorEvent events. Uncaught errors may cause your code to halt.

Where to go from here

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.

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

More Like This

  • Tips for building AIR applications that can be easily updated
  • Performance-tuning Adobe AIR applications
  • Getting started with Adobe AIR for Flex and ActionScript 3 developers
  • Using the Adobe AIR update framework
  • Adobe AIR and the experience brand
  • Introducing the Adobe AIR security model
  • Building Lupo: A case study in building commercial AIR applications
  • Using Badger for Adobe AIR applications
  • Developing cross-platform Adobe AIR applications
  • Adobe AIR Marketplace FAQ

Tutorials and samples

Tutorials

  • Using the iOS Simulator to test and debug AIR applications
  • Using the Amazon In-App Purchase Adobe AIR native extension for Android and Kindle Fire
  • Transferring data with AIR native extensions for iOS – Part 3
  • Exchanging Vector and Array objects between ActionScript 3 and C, C++, or Objective-C

Samples

  • Licensing Adobe AIR applications on Android
  • Using web fonts with Adobe AIR 2.5
  • Using Badger for Adobe AIR applications

AIR blogs

More
07/09/2012 Protected: Publishing Adobe AIR 3.0 for TV on Reference Devices
07/08/2012 Source Code: Adobe AIR 3.3 Retina Video Application
07/06/2012 Application specific File Storage on Adobe AIR based ios Application
07/04/2012 Recent Work - iPad/Android App: Inside My toyota

AIR Cookbooks

More
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay
01/20/2012 Skinnable Transform Tool
01/18/2012 Recording webcam video & audio in a flv file on local drive
12/12/2011 Date calculations using 'out-of-the-box' functions

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement