Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Building a Flex library project for deployment

From: Flex 4.6 and AIR 3.0 New Features for Mobile Apps

Video: Building a Flex library project for deployment

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.

Building a Flex library project for deployment

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.

Show transcript

This video is part of

Image for Flex 4.6 and AIR 3.0 New Features for Mobile Apps
Flex 4.6 and AIR 3.0 New Features for Mobile Apps

21 video lessons · 4240 viewers

James Talbot
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Flex 4.6 and AIR 3.0 New Features for Mobile Apps.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.