Over the past week or so I’ve been looking at Adobe AIR Native Extension (ANE) development. It’s something I’d been meaning to take a gander at for ages but never seemed to find the time. Plus on the surface, developing an ANE seemed a little intimidating. Well the good news is it’s not really that hard once you learn a few steps. The hard part is really finding the right documentation, tutorials, and example source code to get you going.
So if you’re interested in developing your own native extensions but don’t know where to begin, here’s a list of the resources that I found extremely useful. I’ve listed everything in the order you should tackle them.
1. Read ‘Programming in Objective-C Fifth Edition’
There’s no way around the fact that you’ll need to write some native code. If you’re new to iOS development then I strongly suggest you pick up a copy of Programming in Objective-C as the book serves as a really great introduction to the language. Of course there are many online resources for learning Objective-C but having spent some time with this book earlier this year I can honestly say it’s worth the money.
2. Consider reading ‘Learn C The Hard Way’
Adobe provide a native code API that is written in C, so you’ll need at least a little knowledge of the C programming language too. Objective-C is actually a superset of C, so you should get everything you need to know from the Programming in Objective-C book I recommended above. It has a section detailing the language’s underlying C features.
However, if you’d like to spend a bit more time with C then I recommend you take a look at Learn C The Hard Way, which is available online. It’s possibly a little hardcore for absolute beginners but a good refresher for those who haven’t done any C development in quite a while.
3. Read Adobe’s official documentation
Okay, once you’re at least familiar with the basics of C programming it’s time to take a look through Adobe’s official Native Extension documentation. While the documentation is lacking somewhat in actual concrete code examples, what’s there is fairly comprehensive and to the point. At this stage you don’t need to fully understand it all but you should spend a few hours reading over everything at least once to familiarise yourself with the development process.
There’s a section detailing the C API for native extension development. You’ll need to use this API when developing your native extension but don’t worry too much since the C API is only required to create a bridge between your extension’s ActionScript code and your native implementation. Essentially what I’m saying is that you can use Objective-C for the majority of your development and only drop down to the C layer for some very specific things.
4. Walk through your first tutorial
Once you’ve scanned over the official documentation you’ll be ready to try and get something up and running. The best place to start is by following through a tutorial. There are a few good tutorials out there detailing how to write native extensions. I personally really liked Lorenzo Nuvoletta’s, who gives a fairly quick and concise set of steps complete with full source code and build scripts. The extension Lorenzo has you build is extremely simplistic (it returns a string from the native layer) but it covers the basics, you’re unlikely to slip up, plus you’ll learn a lot from it.
5. Write your first very simple native extension
So once you’ve got Lorenzo’s native extension up and running, I suggest you take what you’ve learned and try writing a slightly more complicated version. Give your extension a few more methods and experiment with some more primitive types. You know how to return a string from the native side so try returning integers, numbers, boolean values, and arrays. Also, experiment by passing ActionScript primitive types into your extension and working with them.
All this will require you to work with the native extensions C API. Mastering this API will allow you to exchange data between your AIR app’s ActionScript and your native code. If you’re new to C programming then you may struggle a little to begin with. However, Adobe’s Tom Krcha has written an excellent three part tutorial explaining how to use the C API to transfer data from ActionScript 3 to C/Objective-C and back again. Unlike Adobe’s own official documentation, these tutorials have plenty of code samples that you can just lift and drop into your own code. If C isn’t your strong point then keep this tutorial open in your browser as you develop.
6. Dealing with compilation and packaging errors
There are a lot of steps when writing and packaging native extensions, so you’re likely to make mistakes. Unfortunately you can encounter some pretty obscure errors. Here’s an excellent resource detailing common errors you may encounter and how to fix them.
7. Examine the source code from other ANEs
Once you start feeling comfortable with the basics you’ll probably want to start doing some fancy stuff like layering native UI components over your AIR content or working with third-party iOS libraries. The best way to learn is to pop over to somewhere like GitHub and take a look at the many native extensions that have been open sourced.
I spent some time looking over the many native extensions that developer Richard Lord (Stick Tennis developer) has made available. You should also check out Fresh Planet’s (of Song Pop fame) many extensions. There’s tonnes you can learn just by looking over their source code. It’s also a great way to learn how best to organise the folder structure for your own projects.
8. Learn how to write build scripts
There’s a lot of jumping around when writing a native extension. One minute you’ll be compiling ActionScript, the next you’ll be inside Xcode writing Objective-C. You’ll also be using ADT to package everything together into the final .ane file. This means that making and testing your changes can take some time as you jump between IDEs and the terminal. If you want to be as productive as possible then you really need to spend some time writing a build script. It’ll save you so much time and will generally reduce your compile and testing time from several minutes to seconds.
As a starting point go back to Lorenzo Nuvoletta’s tutorial and take a look at his shell script. If you want to take things further (and you should) then consider writing an Ant script to do the bulk of your work. Take a look at the Ant scripts (this build.xml file on GitHub) in Richard Lord’s native extensions. For a quick introduction look over this tutorial, which lists seven things you need to know about Ant. It’s a really quick and simple guide to Apache Ant. There’s also a pretty good looking tutorial over on the net tuts+ site.
Writing build scripts isn’t essential when writing native extensions but it will save you so much time. Oh and I forgot to say: Apache Ant typically comes pre-installed on your Mac.
9. Other resources worth looking at
Most of the links I’ve mentioned above are those that I used on my journey into native extension development. There are however a few additional resources that are worth checking out:
- 20 tips for creating Air Native Extensions for iOS
- Building a native extension for iOS and Android (5 part tutorial)
- Make your first iOS ANE in under an hour ($30 eBook)
- Extending Adobe AIR
- Adobe AIR Developer Center
10. What about Android development?
One of AIR’s benefits is the fact that you can target both iOS and Android. So how do you write the native Android component of your extension? Well that’s something I’ve yet to fully explore. If you do need your native extension to target Android then I suggest you take a look at Lee Brimelow’s excellent two part video tutorial (part 1 and part 2) that takes you through the process.
That’s about it. I hope you find the recommended resources useful. It probably looks like a fairly lengthy list but if you follow the steps then you’ll be able to write your own native extensions. Good luck and have fun!