Adobe
Products

Top destinations

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • SiteCatalyst
  • Students
  • Elements family

Adobe Creative Cloud

  • What is Adobe Creative Cloud?
  • Design
  • Web
  • Photography
  • Video
  • Students
  • Teams
  • Enterprise
  • Educational institutions

Design and photography

  • Photoshop
  • Illustrator
  • InDesign
  • Adobe Muse
  • Lightroom

Video

  • Adobe Premiere
  • After Effects

Web development and HTML5

  • Edge Tools & Services [opens in a new window]
  • Dreamweaver
  • Gaming [opens in a new window]

Adobe Marketing Cloud

  • What is Adobe Marketing Cloud?
  • Digital analytics
  • Social marketing
  • Web experience management
  • Testing and targeting
  • Media optimization

Analytics

  • SiteCatalyst
  • Adobe Discover
  • Insight

Social

  • Adobe Social

Experience Manager

  • CQ
  • Scene7

Target

  • Test&Target
  • Recommendations
  • Search&Promote

Media Optimizer

  • AdLens
  • AudienceManager
  • AudienceResearch

Document services

  • Acrobat
  • EchoSign [opens in a new window]
  • FormsCentral [opens in a new window]
  • SendNow [opens in a new window]
  • Acrobat.com [opens in a new window]

Publishing

  • Digital Publishing Suite

  • See all products
Business solutions

By business need

  • Digital analytics
  • Digital publishing
  • Document management
  • Media optimization
  • Social marketing
  • Testing and targeting
  • Video editing and serving
  • Web development [opens in a new window]
  • Web experience management
  • See all business needs

By industry

  • Broadcast
  • Education
  • Financial services
  • Government
  • Publishing
  • Retail
  • See all industries
Support & Learning

I need help

  • Products
  • Adobe Creative Cloud
  • Adobe Marketing Cloud
  • Forums [opens in a new window]

I want to learn

  • Training and tutorials
  • Certification [opens in a new window]
  • Adobe Developer Connection
  • Adobe Design Center
  • Adobe TV [opens in a new window]
  • Adobe Marketing Center
  • Adobe Labs [opens in a new window]
Download
  • Product trials
  • Adobe Flash Player
  • Adobe Reader
  • Adobe AIR
  • See all downloads
Company
  • Careers at Adobe
  • Investor Relations
  • Newsroom
  • Privacy
  • Corporate Social Responsibility
  • Customer Showcase
  • Contact us
  • More company info
Buy
  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers
  • Adobe Marketing Cloud sales [opens in a new window]
Search
 
Info Sign in
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Welcome,
My Adobe
My orders
My information
My preferences
My products and services
Sign out
My cart
Privacy My Adobe
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out Privacy 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
Promotions
Estimated shipping
Tax
Calculated at checkout
Total
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center /

Building a native extension for iOS and Android – Part 4: Building the Android library

by Nathan Weber

Nathan Weber
  • digitalprimates.net

Content

  • Creating the Android library
  • A note about debugging
  • Where to go from here

Created

27 August 2012

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScriptAdobe AIRAndroidFlash Buildermobilenative extensions
Was this helpful?
Yes   No

By clicking Submit, you accept the Adobe Terms of Use.

 
Thanks for your feedback.

Requirements

Prerequisite knowledge

This series of tutorials is designed for developers with an intermediate to advanced understanding of ActionScript 3 and building Adobe AIR applications. Familiarity with Flash Builder, Java, and Objective-C will also be helpful. You should have previously read Part 1 and Part 2 of this tutorial series. If you are also going to use the AIR native extension on iOS, you should have previously read Part 3 of the tutorial series.

 

Additional required other products

  • Android ADK
  • Volume native extension

User level

Intermediate

Sample files

  • system-volume-native-extension.zip

In Part 2 and Part 3 of this series, you created the main and default libraries in ActionScript and the iOS library for a volume native extension for Adobe AIR. If you are going to use the native extension on Android devices, you must also create an Android library.

For me, building the Android library was an interesting experience. It was easier for me to write Java than Objective-C as it is more similar to ActionScript. Some amount of debugging (log statements) is actually possible with no work on the developer’s part, which is a great help. Also, using the AIR runtime extensions in Java is a much simpler process.  However, the Android operating system leaves a great deal of work up to the developers. You’ll find that some functionality that is built into the iOS framework will need to be built from scratch for Android.

Creating the Android library

Creating the Android library is fairly straightforward. If you haven't done so already, download the Android SDK and install the ADT Eclipse plugin into a standard version of Eclipse (not Flash Builder). Then follow these steps to create the library:

  1. Open Eclipse and create a new Android project. Name it whatever you want and choose the minimum SDK version you wish to support. Chances are you'll want to pick 2.2, which is the first version of Android that AIR supports.

Next, you'll need to add the AIR runtime extensions to the project.

  1. Right-click the project and select Properties.
  2. Click Java Build Path and select the Libraries tab.
  3. Click Add External JARs, navigate to air_sdk_dir\lib\android, select the FlashRuntimeExtensions.jar file, and click OK.
  4. Create a class that implements the FREExtension interface. The package and name for this class should match the initializer node for the Android-ARM platform in the extension.xml file. This class should have the methods createContext() , dispose() , and initialize() .

The createContext() method accepts a string parameter that is the type of context to create. This is the second parameter passed to the ExtensionContext.createExtensionContext() method. The return value for createContext() should be a class that extends FREContext.

public FREContext createContext(String contextType) { return new VolumeExtensionContext(); }

The dispose() method will be called when the native extension is no longer needed by the application. Here you should do any clean up required.

The initialize() method is called when the native extension is ready to be used.

  1. Create a class that extends FREContext. This class should have the methods dispose() and getFunctions() .

The dispose() method serves the same purpose as the dispose method of the FREExtension .

The getFunctions() method returns a collection of FREFunction objects. There should be one FREFunction for each method that is called from the main ActionScript library. Each FREFunction is mapped to a string value, which is the name of the function.

public Map<String, FREFunction> getFunctions() { Map<String, FREFunction> functions = new HashMap<String, FREFunction>(); functions.put("init", new InitFunction()); functions.put("setVolume", new SetVolumeFunction()); return functions; }
  1. Create one class that implements FREFunction for each method invoked from the main ActionScript library.

These classes will each have a call() method. This method is invoked when the function is called from the main ActionScript library. This is where the heavy lifting happens in the native extension and where you'll add the native code to implement whatever capabilities that your native extension offers.

The call() method is passed a FREContext instance and an array of FREObject instances. The FREContext is the extension context used to invoke the method. The array of FREObject instances is a collection of the parameters passed to the method, one FREObject for each parameter.

FREObject is a Java representation of an ActionScript object. It is a wrapper class, so to get to the underlying value you must invoke methods of FREObject. For example, in the setVolume() method you would pull out the Number value that is the new volume.

public FREObject call(FREContext context, FREObject[] args) { Context appContext = context.getActivity().getApplicationContext(); AudioManager aManager = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE); double volume = 1; try { volume = args[0].getAsDouble(); } catch (Exception e) { } int maxVolume = aManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); volume = volume * maxVolume; int index = (int) volume; aManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0); return null; }

In the code above, the line that reads volume = args[0].getAsDouble(); is where the value passed from ActionScript is extracted from the FREObject collection.

Now you're ready to create the JAR file.

  1. Right-click the Android project and select Export > Java > JAR File.
  2. Ensure the project you're exporting is checked in Select Resources To Export.
  3. Choose where to save the JAR file and click Finish.

A note about debugging

Debugging native extensions isn't easy, but Android native extensions aren't quite as difficult to debug as iOS native extensions. You won't be able to hit breakpoints but you can see log statements from the native code. When using one of the Log.d(TAG,"Context disposed."); statements, the output will show up in the LogCat window in ADT when the native extension is running on the device. The LogCat window shows all of the logging that is happening on the device, so you'll likely want to filter (click the green plus sign) by tag to see only your native extension's specific logging.

Where to go from here

The Android library is now complete. You have also completed the main and default libraries in ActionScript, as well as the iOS library if you are using your native extension on iOS devices. You're now ready for the final tutorial in this series—Developing a native extension for iOS and Android — Part 5: Building the ANE file.

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

More Like This

  • Developing cross-platform Adobe AIR applications
  • Performance-tuning Adobe AIR applications
  • Using Badger for Adobe AIR applications
  • Building a native extension for iOS and Android – Part 2: Developing the ActionScript library
  • Creating your first Adobe AIR application on Android
  • Using web fonts with Adobe AIR 2.5
  • Signing Adobe AIR applications
  • Using push notifications in AIR iOS apps
  • Building Lupo: A case study in building commercial AIR applications
  • Using the Push Notifications native extension for iOS

Products

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • Digital Publishing Suite
  • Elements family
  • SiteCatalyst
  • For education

Download

  • Product trials
  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR

Support & Learning

  • Product help
  • Forums

Buy

  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers

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 © 2013 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

Ad Choices

Reviewed by TRUSTe: site privacy statement