Using Native Extensions
This tutorial will cover adding a Native Extension for Adobe AIR to your application.
We are going to be focusing on the native extensions developed by distriqt, but this guide should apply to any native extension (ANE).
Native Extensions for Adobe AIR are code libraries that contain native code wrapped with an ActionScript API. Native extensions provide easy access to device-specific libraries and features that are not available in the built-in ActionScript classes.
Their usage is similar to a SWC package of precompiled Flash symbols.
In the following tutorial we’re going to go through the process of adding an ANE to your AIR application using some of the most common AIR development tools. We assume you already understand the process of developing and deploying mobile applications using AIR so will just be concentrating on the additional steps required to get an ANE working.
An extension is a single file with the extension “ane”. This file contains all of the native and actionscript libraries that are implemented by this extension. You don’t need access to a separate SWC file or to the source code to be able to use the ANE.
In CS6 Adobe have added the ability to simply add an ANE to your AIR project.
In your AIR project FLA file, open your applications publish settings, by going to File > Publish Settings.
Select the wrench icon next to the Script dropdown. This will bring up the ActionScript Settings dialog.
Select the Library path tab and click Browse to a Native Extension (ANE) file, as shown above, and select the ANE file.
You should now be able to use the extensions functionality in your project and CS6 will include the ANE code when you publish your application.
In your mobile project you want to add it into, open up the project properties by right clicking on your project and selecting Properties. Find the "Actionscript Build Path" (or "Flex Build Path" if it's a Flex project).
You'll see a tab up the top labeled "Native Extensions" in which you can "Add ANE...".
Click on this and select the ANE extension file. Once it's added in, you can expand out the details of the extension to see the target platforms supported by the extension. Below I'm showing the details on our Android Camera Extension.
The extension is now included in your project, however before you leave the project settings, go to the "Build Packaging" for the platforms you are deploying and check the extension is going to be packaged (and available) with them.
You should now be able to return to your code and use the functionality and classes supplied in the extension, hopefully you've been supplied with documentation or example usage of the extension!
The older version of Flash Builder is a lot more complex to get running and unless you’re forced to I’d highly recommend updating to 4.6/4.7. It involves a few scripts and tricks to get it running which we’ll show on OSX here, but you should be easily able to adapt it to other operating systems.
There are many methods but this is what we’ve found to be the simplest.
Firstly rename the ANE file to have the extension SWC.
com.distriqt.Camera.ane > com.distriqt.Camera.swc
Then we include the SWC like we do any normal SWC, open:
Project / Project Properties / Flex/Actionscript Build Path
In the Library Path tab, click Add SWC...
Browse to the SWC file and select Open. The ANE file should now appear in the screen above. You need to now expand the SWC entry and double click the Link Type to open the Library Path Options dialog, as below:
Change the Link Type to be External instead of the default Merged into code.
You should now be able to compile your project, using Project > Build Project.
Note: You won’t be able to run this application through the debugger as it won’t correctly package the extension through Flash builder.
Note: The part about changing the ANE file’s file extension from .ane to .swc is not strictly necessary. When you browse to find the file, in the “Choose a SWC file” dialog box, you can change the setting of the Files Of Type drop-down field. It is set by default to *.swc. Change it to *.*. This will have the same effect.
You’ll need to manually add the extension ID to the application descriptor at this stage. See the Advanced details section on how to do this.
Now you’ll need to open up a command line in the directory of your built application. The directory should contain:
The details on this command is dependent on the application you are developing and the platform you are targeting. You should review the documentation at the following url to determine the details of what the command should contain for your environment.
For example the following command will create an APK for Android:
-storetype pkcs12 -keystore YOUR_SIGNING_KEY.p12
The procedure for CS5.5 is very similar to Flash Builder 4.5. The only difference is in the procedure to include the renamed ANE and set the linkage to external.
To add the extension:
You should now be able to follow the instructions in the section: Flash Builder 4.5 / Packaging to package the application into an installable application including the ANE.
To enable the extension in your application you need to add some additional information to your application descriptor. Basically this informs the compiler of what extensions are required in your application. Most of the IDE’s will add this automatically but it’s useful to understand what’s changing when you add an extension into your application.
The following shows two extensions added to an application. This node is generally located as the last node in the application descriptor.
More details on the extensions ID can be found here:
An extension may also require additional permissions or definitions in the application descriptor. For example, requesting permission to access the vibration function with a vibrate extension. You should carefully consult the extensions documentation and add any required additions to the application descriptor. Incorrectly updating the descriptor may mean the extension cannot correctly operate.
If you aren’t developing for iOS you can skip this step.
In the particular case of developing for iOS it is quite often a requirement that you have access to the iOS SDK to access the most recent features of the SDK.
You will need your Apple user ID, this is the same login you use when you access the iOS developer portal but you can sign up for a free one such as the one you use to access iTunes. The SDK is obtained through Apple’s XCode so we need to download and install that.
Note: You’ll need an OSX machine to do this, but you can quite easily do this on windows by installing an OSX virtual machine. You’ll need this machine to get updates to the SDK but once you’ve acquired the SDK you can use it on a windows machine.
Once Xcode is downloaded, launch the installer and follow the instructions.
You will now have the latest version of the SDK installed on your machine. You’ll need to locate the directory, earlier version of Xcode will place the SDK here:
However more recent versions will place it here:
Note: If you’re on windows you’ll need to copy the SDK directory completely to your windows machine and use that path in the following.
Adding the SDK to your project again depends on your development tool so we’ll go through each individually below.
Under Flash Builder 4.6/4.7 you simply go to your project settings and select the Apple iOS section in the Build Packaging. You’ll see a place down the bottom to enter in or browse for the path to the iOS SDK.
In Flash CS6 you can simply go into
File / AIR 3.x for iOS Settings...
Check the box next to iOS SDK and enter in the path to the SDK.
These remaining environments require usage of the adt command line to include the SDK. In particular we’re going to be using the platformsdk option of adt.
You should already have created a script or at least have a command to run on the command line from the previous sections. If you haven’t consult the section on Flash Builder 4.5 for a detailed guide. Windows developers using other environments should also do this.
In order to add the iOS SDK all we need to do is add in the platformsdk option with the full path to the SDK. For example:
Note: you’ll need a more recent version of AIR to use this platformsdk option. Version prior to 3.2 do not have this ability and you won’t be able to include the iOS SDK.
You should now be set to go. Consult the documentation for the extension and start using native functionality!
For more information: