How to create your own ANE for iOS using Adobe Air


Adobe Air gives us the amazing opportunity to access the device capabilities using extensions, also named ANE (Adobe Native Extensions). Creating an ANE is pretty simple, although it requires a laborious process which is not always easy to remember. Adobe has some tutorials about it, but I find them a little confusing and not straight to the point.

This is a step by step guide with the very basic knowledge to know to build a simple ANE for iOS. The ANE we will build doesn’t really have any use, it will simply return a String from iOS that says “Hello World!”. Although this is a kick start to create any other Native extension. Soon I will post some more examples to generate more complex behaviors.

Tools

First thing, you need a Mac, I know this sucks, but we need to use XCode, and that’s only available on Apple computers.

Download the Flex SDK and the Air SDK. After you have completed the download unpack them somewhere, they can be in the same folder if you want, but it is better to keep them separated.

For the .as and .xml files you can use any text editor.

Using XCode

Launch XCode and select Create New Project,  under iOS Framework & Library, select Cocoa Touch Static Library and press Next.

You will be prompted to enter a product name, insert it (for my project I called it ANEGeneric) and then hit continue.

Now you have a project containing 2 files, trash the .h file and clear the content of the .m file.

In the Air SDK/include folder there is a file named FlashRuntimeExtensions.h, drag and drop that file on your XCode project. Select your project in Add to targets and Copy items into destination.

Start to type some code, your first line of code is in the .m file you cleared before:

Let’s create a simple method to retrieve a String from iOS:

Passing strings is probably the biggest pain in the butt because they have to be converted between C, Objective C and AS3, but anyway, done it once then it becomes easy to repeat.

In order for the ANE to be initialized and finalized you need to specify 4 methods, 2 initializer and 2 finalizer. Those look all pretty much the same for any future ANE you will ever have to make, unless you want to do something particular, the only thing you have to make sure is that the names of the methods are unique and do not contain underscore “_”, dashes “-” or special characters, follow this rule also for your class names and method names. I have also noticed that using words like Test, Example or similar will not work properly, still investigating why. Looks like when the ANE are unpacked at compile time they confilct with other ANE that use the same Initializer and Finalizer names.

Anyway this is pretty much how your code will look:

In the initializer you also specify the name of the function that is going to have the hook between XCode and AS3.

You are almost done, go in Product->Edit Sheme and select Release in Build Configuration

Click OK and press command+B to compile, this will generate a new .a file in the Product folder, you will need this file to create the ANE.

ios ane

 

To copy the file you can right click on it directly from your project and find the exact location using “Show in Finder”.

Creating the AS3 Code

The code we wrote in XCode by itself is not going to create the hook we need with AS3, you will have to generate a new AS3 class (.as file) to communicate with you Air. Create a new folder “src” and make your .as file, maybe you can specify more folders to create a package, in my case it is src/com/lorenzonuvoletta/helloworld/ANEHelloWorld.as

Preparing the XML descriptors

To create an ANE you need to specify 2 XML descriptors, those contain the id of your ANE, the initializer, finalizer and some other informations.

The first XML is the “exstension.xml” and it looks like this:

here you specify the name of the .a file and the names of the Initializer and Finalizer method.

The other XML contains some platform options like the SKD and the Minimum SDK version supported. This is optional, but it is safer to have it, “platformoptions.xml”:

Creating the ANE and SWC files

The last step is actually creating the final files, which are the ANE and the SWC, you will need both those files in order to use the Native Extension.

What you have to do is use “acompc” to generate a SWC file, then extract the library.swf that is packed inside it and then generate the ANE with “adt”, I created a shell script file that does all for you.

The NAME will be whatever you decide. Make sure to change the ACOMPC and ADT folders to the ones where you unpacked Flex and Air, also in the INCLUDE_CLASSES if your project contains multiple classes you can add more separating them with a space. Execute this file in the same folder where you saved all the other files and it will generate the ANE and SWC for you.


10 thoughts on “How to create your own ANE for iOS using Adobe Air

  • Yang,HS

    hello,I try to follow your guide to build the ANE.
    but I’m not sure about the lastest step,run the script file you build.
    I paste your code into command”nano” in the terminal,save as a filie and then use command “chmod +x fileName” to turn the file into exec. <–is this correct?

    thanks for your help and good understanding guide for ANE!

  • Jim O

    Thank you so much for posting this. This is the most straight-forward tutorial on building an ANE for iOS that I have found.

    In case someone else is trying this, in the code above, the following function is used:

    public static function get instance():ANEHelloWorld {}

    to create the instance. This means that in the AS3 code that uses the ANE, you must do the following:

    var hw:ANEHelloWorld;
    hw = ANEHelloWorld.instance;

    and NOT the following:

    var hw:ANEHelloWorld = new ANEHelloWorld();

  • Robbie

    Thanks for the tutorial! Supplying the source code for this would be great!

    I am getting a problem with the bash script “…..AIRSDK/bin/adt: No such file or directory
    “, I can cd the same path from terminal and it is correct. Any thoughts why this might be? I’m using Flash Builder to compile the swc (so don’t need the beginning bit), but the compiler path to adt has spaces in the address. I’ve wrapped the $ADT in quotes (eg “$ADT”) which sows the correct path.

    Thanks again!

  • Roberto

    Hi, I’m going nuts trying to build an ANE with the iOS UIActivityViewController.
    I always get “Invalid extension descriptor: extension.xml” on Terminal when I do adt on an example… Either with your method or with this one:

    /Users/robertocorralestejada/Desktop/Actionscript/AIRS/AdobeAIRSDK40/bin/adt -package -target ane ANEHelloWorld.ane extension.xml -swc ANEHelloWorld.swc -platform iPhone-ARM library.swf libANEGeneric.a

    It’s still possible to do ANEs?
    Thanks in advance

  • Lorenzo Nuvoletta Post author

    I noticed that in the new XCode in order to actually compile and .a file sometimes you have to change the “Per-Configuration Build Products Path” in the Buil-settings to something custom… :/

  • Hari

    Hi, i am looking for some thing like to get ‘save image’ option on clicking download button in my flex application so that the image i selected would save in iPad photos

Leave a comment

Your email address will not be published. Required fields are marked *