Save 37% off PRO during our Black Friday Sale! »

AIR Native Extensions

AIR Native Extensions

Overview of using and creating Native Extensions for use with Adobe AIR desktop and mobile applications.


David Ortinau

March 25, 2012


  1. AIR Native Extensions for Mobile

  2. David Ortinau @davidortinau 16 yrs web, interactive, mobile. Flash,

    iPhone, Android, WP7 BA English, Maryville University
  3. None
  4. AIR Defined

  5. “Adobe AIR is a cross-operating system runtime that enables

    web developers to use their existing web development skills, code and tools to build and deploy rich web applications and content to the desktop. Adobe AIR has a rich set of features, with support for building applications using HTML, JavaScript, Flex and Flash.”
  6. AIR Runtime • Adobe Integrated Runtime Runtime = AIR Runtime

    • Runtime environment contains: • Flash Player: runs Flash, Flex • WebKit: runs HTML, Javascript • Platforms supported • Windows • OS X • Linux* • Android • Blackberry Tablet OS • iOS *Adobe ended support at 2.6
  7. AIR SDK • Software Development Kit provides additional APIs •

    File System • Native Window Chrome • Enhanced Drag and Drop • Stage3D • Captive Runtime • Stage Video Hardware Acceleration • h.264 Video Encoding • Front Camera Support* • Hi-Res Bitmap Support • Multitouch and Gestures • Native Text* • Encrypted local storage* • Native JSON • Accelerometer support • StageWebView • Screen Orientation support* * mobile only
  8. AIR Native Extensions

  9. An Adobian “With we released of AIR 3.0 we added

    this awesome new feature called native extensions. Trust me these are cool. Essentially, a native extension will let you extend the functionality of your AIR app so it can access the native capabilities of a device.”
  10. Like What? • Notifications/Toasts • Push Notifications • Gyroscope •

    NetworkInfo • Licensing • Vibration • Speech Recognition • Battery Status • Kinect • Ads • Flurry Analytics • Mute • GameCenter
  11. Performance!!! Native code executes OUTSIDE the Flash Runtime

  12. Multi-Threading!!! Native code supports multiple threads

  13. AIR App AIR Runtime Device OS and Libraries Actionscript Extension

    Classes AIR Native Extension (*.ane) Resources (images, etc) Native Implementation
  14. AIR App AIR Runtime Device OS and Libraries Actionscript Extension

    Classes AIR Native Extension (*.ane) Resources (images, etc) Native Implementation
  15. ANEs are Platform Specific

  16. References AIR Native Extensions from Adobe and Community air.html

    What the Heck are AIR Native Extensions? heck-are-air-native-extensions
  17. How to Use ANEs

  18. Where to find ANEs • Adobe air.html • Community

    Sites • GitHub q=ANE&repo=&langOverride=&start_value=1&type=Everyt hing&language=ActionScript • ANE Explorer - Android App
  19. Using an ANE • AIRKinect.ane example • Space Invaders air/
  20. None
  21. How to Make ANEs

  22. Battery Status adapted from Todd Anderson’s post

  23. package { import; import flash.external.ExtensionContext; public class BatteryStatus extends

    EventDispatcher { protected var _extensionContext:ExtensionContext; private static const COMMAND_LIFE:String = "GetBatteryLife"; private static const COMMAND_INFO:String = "GetBatteryInfo"; public function BatteryStatus(){ super(); init(); } private function init():void { _extensionContext = ExtensionContext.createExtensionContext( "com.simplyprofound.BatteryStatus", "main" ); } /** * Returns the battery life in percent (0-1). * @return Number The percentage of battery life left on the iOS. Returns a value from 0 to 1. */ public function getBatteryLife():Number { return COMMAND_LIFE ) as Number; } /** * Returns the current state of the battery on the iOS device. * @return int Value corresponding to the state of the battery. UNKNOWN, UNPLUGGED, CHARGING, FULL */ public function getBatteryState():int { return COMMAND_INFO ) as int; } } } Actionscript Code
  24. ExtensionContext The ExtensionContext class provides an interface for calling functions

    in the native implementation of an ActionScript extension. You can use this class only in ActionScript classes that are part of the extension.
  25. Native Code #import "FlashRuntimeExtensions.h" // Access battery life. FREObject GetBatteryLife(FREContext

    ctx, void* funcData, uint32_t argc, FREObject argv[]) { UIDevice *device = [UIDevice currentDevice]; [device setBatteryMonitoringEnabled:YES]; float life = [device batteryLevel]; FREObject retVal; FRENewObjectFromDouble( life, &retVal ); return retVal; } // Access info about battery FREObject GetBatteryInfo(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) { UIDevice *device = [UIDevice currentDevice]; [device setBatteryMonitoringEnabled:YES]; int info = [device batteryState]; FREObject retVal; FRENewObjectFromInt32( info, &retVal ); return retVal; } // A native context instance is created void ContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet) { ! *numFunctionsToTest = 2; ! FRENamedFunction* func = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*2); ! func[0].name = (const uint8_t*)"GetBatteryLife"; ! func[0].functionData = NULL; ! func[0].function = &GetBatteryLife; ! ! func[1].name = (const uint8_t*)"GetBatteryInfo"; ! func[1].functionData = NULL; ! func[1].function = &GetBatteryInfo; ! ! *functionsToSet = func; }
  26. Extension Descriptor <extension xmlns=""> <id>com.simplyprofound.BatteryStatus</id> <versionNumber>1</versionNumber> <platforms> <platform name="iPhone-ARM"> <applicationDeployment>

    <nativeLibrary>libBatteryStatus.a</nativeLibrary> <initializer>ExtInitializer</initializer> <finalizer>ExtFinalizer</finalizer> </applicationDeployment> </platform> </platforms> </extension>
  27. Package the ANE "/Applications/Adobe Flash Builder 4.6/sdks/4.6.0/bin/adt" - package -target

    ane ../release/BatteryStatus.ane extension.xml -swc ../bin/BatteryStatusANE.swc -platform iPhone-ARM library.swf libBatteryStatus.a
  28. Tips • Provide a default implementation for testing • Write

    your Actionscript API first • Validate all input in Actionscript • Don’t include external dependencies (Flex SDK)
  29. References 20 tips for creating Air Native Extensions for iOS native-extensions-for-ios AIR Native Extension Example: iBattery for iOS extension-example-ibattery-for-ios/
  30. Thanks! @davidortinau