I'm new to Air. I've got an existing Flex 2 application which I'm considering bringing into Flexbuilder 3. My question is can I use the same base application (and source code) to make the Air version, or would I have to maintain to separate code trees: one for the Air version and one for the Flex/SWF version?
The best approach I've found to creating both Flex and AIR applications from the same source, is to use a core library project for almost all code, with separate small projects for the Flex and AIR applications.
There are two key concepts that make this very powerful and maintainable, allowing for not just two applications, but for many "editions" if you're so inclined.
Modules:- If the core application is actually a module (or a module that loads other modules) you'll be able to easily create stub Flex and AIR applications that are basically there to set project properties, reference classes for cross-module communication, and then simply load the core application module with a ModuleLoader.
Factory Objects:- When there are things you want to do on the AIR desktop application that you can't do in the Flex application, or any case where you want something to work differently across applications, a factory object that creates an instance of a project specific class is a great way to do it. for example, you can call your "save" function, which for AIR saves to the file system, but for Flex calls a web service.
Have a look at Todd Prekaski's excellent Flex DevNet article on Building Flex and Adobe AIR applications from the same code base on how to do this.
Once you've created a Flex Library project where you're going to create your main module, create your Flex and AIR application projects. In the properties of the two application projects add the library project src directory to the Flex Build Path. While in the project settings you'll also need to add the modules to the Flex Modules section, setting the option to optimise for the project.
You can't mix both AIR and Flex in the same Flex Builder project, but you can share code. Here's how...
- Create a Flex based project as you normally would.
- Create a second AIR based project.
- In the second application, go to project->properties.
- Select the "Flex build path" option.
- Under "Source Path" add the folder that contains the source from your first Flex based project.
Now both projects share the code from the flex project.
You need to be careful not to use AIR only API's in code you intend to share between both apps.
One way is to put the bulk of the application in a Group-based component in a library.
Both Flex and AIR applications can embed that component in their Window.
If you need to have custom code, have your Group component accept an Interface object that has all the methods that are specific to a platform (loadFile, saveFile, etc.). Each application injects an object that implements these methods appropriately.
I've worked on a product that injected a whole local-data access layer (to the SQLite database) and the core application had no idea if it was running in a browser or on the desktop (connected or disconnected).
Cheers
Create 3 project: Air, Web, and common. from air and web, include common.
http://simplifiedchaos.com/how-to-compile-both-flex-and-air-application
© 2022 - 2024 — McMap. All rights reserved.