Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
An extension mechanism needs a model, that is, a methodology by which new APIs are bound into the runtime. AIR extensions are modeled as ActionScript libraries that contain native code. This model allows the creation of a native extension to be separated from its consumption. Extensions can be developed independently, published completely separately, licensed and sold, as can any ActionScript library. They can also be created and consumed by the same developer and even be used within the same application.
Now extensions contain both ActionScript code and native code, and the boundary between the ActionScript and the native code, whether that's of course Objective-C or Java, can really vary arbitrarily. The boundary can also be different on different platforms. Now as a Flex developer we can use this flexibility to our advantage as the extension author, because we can choose the most suitable implementation language for each portion of the extension. Now, typically, as a biased Flex developer, it's advantageous to write as much code as possible in ActionScript, because ActionScript is much easier, it's much faster, it's much safer, and it's much more portable than Java or Objective-C.
In some cases it may even be possible to write the implementation for a specific platform entirely in ActionScript, and native extensions allow this. Now, in the two previous sections we lightly covered the native side of things, and we talked about how to generate a static library file that'll be used to compile a native extension library that will be used by our AIR application to access the vibrate function of an iOS or an Android device that it's deployed to. Now to actually build out the ANE file that we used in the first movie, we have to go through two steps.
The first step is to build a Flash library project, a SWC file, and we have to compile this library project against AIR libraries, and we have to expose an ActionScript API that interfaces with the native library through the extension context. Now if you recall, in the first movie we actually utilized this ActionScript API to actually called the vibrate functions that were in the native code. And of course, our native code matched, and we have the same functions in both Java and Objective-C so it worked well.
So that's what we're going to cover in this movie is how to build this SWC file, this Flash library project. In the next movie we're going to talk about how to use the ADT Command line tool to package everything up and compile this against this SWC and the native library and package everything up as an ANE file. Now the Flash external extension context is inside of the AIR mobile SDK, and it's your main access point to the native library. We just created a new instance of an extension context using an ID defined in the extension descriptor compiled into the native extension.
So before I even do that, let's create a new mobile project here. So let's create a New > Flex Library Projects, remember we're building a SWC file here for our native extension, so I'm going to saying Flex Library Project, and this is going to build out a SWC file. Let's call it vibrate. And we can either use a generic library if we want our native extension to work on both the desktop-- so for example, if we want it to work on Windows and on Mac, as well as Android and iOS--or if we are just utilizing Android and iOS, we can simply choose a mobile library and it will include all of the AIR libraries and everything else we need.
So you need to of course make the choice here, depending on where your extension is running. Our extension is only going to run on iOS and Android, so I'll just choose the Mobile Libraries, and I won't have the overhead of everything else. So then click Next and click on Finish. So you'll see that there's a source directory already in here and all the SDKs are already here. So what I want you to do is go to exercise files here, and then you'll see that there is also a source directory. And then I have my ActionScript API here called Vibration.as, and then I have this extension.xml. Just take this and copy this to your clipboard, like so, and then switch back to Flash Builder and paste this inside of your source directory.
So you should have a com package. You should have Adobe/nativeExtensions/Vibration.as. Now if you recall, comadobe.nativeExtensions was exactly the path that we used in the first video. So hopefully you're starting to see how this all comes together. Now if you open up the extension.xml, let's take a quick look at this and make sure that we understand what is going on. Notice the ID node value that you see there. Right at the top here on line 3 you'll see the id node value, and this is what's actually used to create a new extension context instance.
The native library node value is the native library that we created in the previous section. Of course, on Android, it's going to result in a jar file, and it's going to result here on Xcode, and what's in Objective-C, you can see we have those native libraries. Now those weren't done in the previous section, and note also here that we have defined the iPhone arm platform as a target, as well as the Android. So again, we have both here, and we have both native libraries that we looked at in the two previous sections.
So again, you can see that the extension descriptor file describes the association of the ID to the native library that the extension context will look upon the instantiation. So now we can utilize this. Now we just have to build an actual extension context inside of ActionScript. In here I have source, Com, Adobe, nativeExtensions, and let's take a look at Vibration.as. Okay, so now you'll see that we have our APIs, and you can see, again, that if the AIR application creates multiple vibration objects, all the objects will share one instance of the extension context class.
You'll see here that we have our extension, and again, you can see that there is a call method where it says initNativeCode. This is inside of the native library. So you can see the same API that was on the native side, and you could see, again, we have this vibrate. This matches, right, to the native side, initExtension. You can see again we're calling this directly utilizing this create extension context. So we're building an ActionScript API that connects right back to the native API, so our Flex developers can do anything.
This is very powerful because anything that the flex framework can't do, we can just get our developers to write it in Android and C#, build a nice little API here, and our Flex developers can utilize it here. So again, very, very powerful, and you'll see that because we created this Flex library project, you'll see that it automatically created us the SWC file. Now this is what we're going to use in the next video, this Vibrate.swc that has all of his packaged up here into the SWC file, and we're going to utilize that to actually build out the ANE file.
So that is the process of how to create a SWC file and a Flex library project with everything that we need for our ActionScript API for our developers.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 102013 Viewers
61 Video lessons · 88729 Viewers
71 Video lessons · 72554 Viewers
56 Video lessons · 104194 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.