Building desktop applications usually is not a simple task. Building desktop applications with elegant user interfaces, animated transitions, and all that Flex provides is typically even more challenging. Therefore, in a relative way building AIR applications is extraordinarily simple. There are a few minor differences between building AIR applications and building web applications using Flex. The workflow for building AIR applications using Flex is as follows:
Create a new Flex project. If you are using the SDK, there is nothing different about how you configure a Flex AIR project versus a Flex web project. If you are using Flex Builder, you should select the “Desktop application” option from the “Application type” section of the first screen of the New Flex Project Wizard.
Write a Flex application using MXML and ActionScript just as you
would for a web application. However, instead of using Application
as the root tag for the
application MXML document you must use the WindowedApplication
tag. Also, because AIR
allows for features beyond what web applications allow, you can also
use the AIR-specific APIs and components.
In addition to the normal application files (MXML, ActionScript, embedded assets, CSS files, etc.), you also need to define a descriptor file. A descriptor file is an XML file that contains information about the application, such as the names of files to include in the application, the name of the application, and various other pieces of information, some required and some optional.
Compile the AIR application as an .swf file using the amxmlc
compiler from the command line or
from an Ant script (if you are using the SDK), or by using Flex
Builder. Note that if you are using Flex Builder, the application will
compile automatically if Build Automatically is selected; otherwise,
it will compile when you run the application. The amxmlc
compiler is really just a shortcut to
the standard mxmlc
compiler,
automatically setting the flag that tells the compiler to compile for
AIR instead of for the Web.
Test the AIR application by running the .swf using the AIR debug launcher. If you
are using the SDK, you need to run the debug launcher, which is called
adl
. If you are using Flex Builder,
running or debugging the application will launch the application using
the debug launcher.
Once you have a complete application to distribute, you need to
create an .air file, which
contains the .swf, the
descriptor, and any additional files you’d like to include in the
application (database files, image files, audio or video files, text
files, etc.). You can use the AIR developer tool for this step. The
developer tool is called adt
, and
you’ll need to run it from the command line or from an Ant script if
you’re using the SDK. Otherwise, if you are using Flex Builder, you
can use the Project→Export Release Builder option. Creating an
.air file requires that you digitally sign it by providing a
certificate. If you don’t already have a certificate, you’ll need to
either acquire one or create one during this step.
Now you can distribute the AIR application. You can achieve this by simply emailing the .air file or making it available for download. You can also use the seamless install feature, which allows users to install the application from the Web using what’s called a badge, a simple Flash application that runs in the browser.
Next, we’ll look in more detail at creating a new Flex AIR project, defining descriptor files, compiling AIR applications, and testing AIR applications. We’ll look at creating and distributing .air files later in the chapter.
As we already mentioned, you create and organize a Flex AIR project the same way you would a Flex web application if you are using the Flex SDK. If you are using Flex Builder, the only difference is that you must specify that the new application is a desktop application. You can do that by selecting the “Desktop application” option in the “Application type” section of the New Flex Project dialog.
Once you’ve created a new Flex AIR project, you need to do one
more thing to differentiate it from a Flex web application. Instead of
defining the main application document’s root tag as Application
, you must define it as WindowedApplication
. That means an empty main
application file for an AIR application looks like this:
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> </mx:WindowedApplication>
WindowedApplication
is a subclass of Application
, which means everything that
applies to Application
also applies
to WindowedApplication
. However, in
addition to the properties, methods, and events in the Application
class, the WindowedApplication
class also defines an API
that is specific to AIR. For example, WindowedApplication
objects have a title
property, which allows you to set the
text that appears in the title bar of the application window. If you
look at the documentation for WindowedApplication
(http://livedocs.adobe.com/flex/3/langref/mx/core/WindowedApplication.html),
you’ll see that many of the properties (e.g., systemChrome
, maximizable
, minimizable
, transparent
, etc.) are read-only. Many of
these properties can be set only from the application descriptor file,
which you can read about in the next section.
Descriptor files are XML files that AIR applications require to determine what to run and how to run it, as well as how the .air file gets packaged and installed. For example, a descriptor file tells the AIR runtime what .swf file to run in the main window, and it can specify various settings for the main window, such as title and transparency.
The descriptor file allows you to configure a lot of settings. However, at a minimum, AIR requires that you define the following settings: application ID, name of the AIR file, version number, and content for the main (initial) window. Here is an example of a basic descriptor file:
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://ns.adobe.com/air/application/1.0"> <id>Example</id> <filename>Example</filename> <version>1.0</version> <initialWindow> <content>Main.swf</content> </initialWindow> </application>
Many of the additional settings apply to the initialWindow
tag, within which you can nest additional elements for those
settings. For example, you can add x
and y
elements to set the initial x
and y coordinates of the main window.
You can read about all the descriptor file settings at http://livedocs.adobe.com/flex/3/html/help.html?content=File_formats_1.html#1043413.
If you are using Flex Builder, when you create a new desktop application project, Flex Builder will automatically create a descriptor file for you. The descriptor file will be placed alongside the main MXML file, and it will be named <ProjectName>-app.xml where <ProjectName> is the name of the Flex project.
If you’re using the Flex SDK, you must create the descriptor file yourself. The file should be a UTF-8-encoded XML file. You do not have to use a specific name for the file.
Compiling and testing AIR applications is extraordinarily simple if you are using Flex Builder. In fact, all you need to do is run the application as you would for any Flex application, and Flex Builder will take care of compiling for AIR and running the application in the debug launcher. Therefore, if you are using Flex Builder, you can skip the remainder of this section and jump to Working with AIR Features.”
If you are using the Flex SDK, compiling and testing AIR
applications is not difficult, but it is a little more involved than
compiling and testing using Flex Builder. To compile an AIR application
using the SDK you must run the standard Flex compiler (mxmlc
) using the additional +configname=air
parameter. Optionally, you can use the amxmlc
compiler instead, which is a shortcut that automatically adds the
+configname=air
parameter. For the
examples in this section, we’ll use amxmlc
.
At a minimum, the compiler requires a main MXML file. In the simplest format, the following command will compile Main.mxml into an AIR .swf:
amxmlc Main.mxml
When you compile an AIR application, the compiler options are identical to those you use when compiling a web application. You can refer back to Chapter 2 for more information about these options.