How to compile both Flex and AIR Application from

How to compile both Flex and AIR Application from the same codebase.

from: http://www.simplifiedchaos.com/2007/08/09/how-to-compile-both-flex-and-air-application-from-the-same-codebase/
9 August, 2007 (14:42) | AIR, Flex, Adobe Apollo

Today, I decided it would be easier for me to demonstrate my AIR application to end users if they could just go to a link on a website.  However, getting AIR code to compile into a Flex SWF file, isn’t as simple as I initially thought.  Naturally, there’s the additional APIs that AIR allows the developer to use, like all the local file system interaction.  But, I figured I’d just use some form of pre-processor compilation tags, place them around the code I didn’t want, compile,  call it a day, and drink a beer.  Wrong.

I searched around a bit and found a few conversations discussing how one should go about doing this.  David Coletta had some ideas posted over at his blog.  But I couldn’t find any working code.  Since this took me several hours how to get it working, I thought I’d share my sample project files with you.  Mind you, I’m not totally happy with my solution and think it can be improved upon, but it’s a start.

Download the code here.  (It’s only for Flex Build 3, Moxie.)

The solution:  create thee projects.  One for your AIR application, one for your Flex application, and one to place most all the code, we’ll call it CommonCode.  For these three projects, you create source path references to each other to include common code into their build paths.

For the Air Project, include a source path to the CommonCode Project.

For the FlexProject, include a source path to the Common Code Project.

For the Common Code Project, include a source path to BOTH the Air and Flex projects.


AirFlexIntegration 


The crux of the solution involves coding common code to Interfaces defined in the Common Code Project.  Both the Air and Flex Projects then Implement these interfaces into concrete classes specific to their projects.  For Air, the implementation might include functionality to save to local file system (File.browseForSave).  In Flex, you may prompt the user for an email address to send the file to. 

At runtime when you need to save a file, you selective create the appropriate class depending on the runtime environment (Flex vs AIR), and then call the method on the Interface. 

sample code 

Misc thoughts (for my own records):

    1. This one I didn’t figure out, but for some reason I couldn’t just call upon the applicationDomain and call getDefinition for the Flex version of the project.  Hence the implementation of getClassToCreate above.  I Just lucked into this by trial and error.
    2. Security.APPLICATION static string declarations exists only in the AIR framework.  Trying to reference that from a Flex project will result in a compilation error.  Use the string “application,” instead.   Same goes for Capabilities.playerType DESKTOP, use the string “desktop” instead.
    3. The Flex compiler will not include any classes that aren’t explicitly referenced in the code.  This must be an optimization.  Because of this, you need to include a declaration to all your classes that yo want to have dynamically generated at runtime.
    4. I don’t like having the class names hard-coded as strings in the Common Code.   I could get around this by making some a Factory available on each the FlexProject and AirProject, but this seems to be a little too complicated.
    5. This is for compilation purposes only.   There’s no dynamically loading modules at runtime. 

I’ll add more to this later.

I’m welcome to ideas on improving this implementation.

Share

0 条评论

留下评论