Home > Flex > Using Ant with AIR

Using Ant with AIR

September 29th, 2009 admin Leave a comment Go to comments

Its a bit annoying, but in order to package applications using ANT you need to have an understanding of amxmlc and ADT first. amxmlc is a simple command line compiler, the general syntax is;

amxmlc [compiler options] — MyAIRApp.mxml

where MyAIRApp.xml is the project config file. An example of an amxmlc invocation is;

amxmlc -library-path+=/libs/libOne.swc,/libs/libTwo.swc  — MyAIRApp.mxml


ADT stands for Air Developer Tool. Its the command line tool used to compile and package you AIR applications. Basically ADT is to AIR what javac is to java. In ADT to build an AIR application you need at minimum an app descriptor file and a main SWF file. You invoke the ADT command with the following syntax

adt -package SIGNING_OPTIONS air_file app_xml [file_or_dir | -C dir file_or_dir | -e file dir ...] …

SIGNING_OPTIONS include; storetype pkcs12 -keystore certificate.p12. An example of ADT invocation is as follows;

adt -package -storetype pkcs12 -keystore cert.p12 MyAIRApp.air release/bin/MyAIRApp.xml
-C release /bin MyAIRApp.swf
-C ../artwork/MyAIRApp images
-C ../libraries/release libs

in short;
-package tells adt that you want to create an air package
-storetype indicates the certificate format,
-keystore points to the actual cert

then you define the name of the package, followed by;
- the application descriptor
- the main compiled swf file
- any resources like images
- any libraries

All the -C directive means is change directory. For example, -C release/bin MyAIRApp.swf means change to the directory release and use the MyAIRApp.swf file. A full explanation of what ADT does is out of the scope of this post, but here is a good place to start;

http://livedocs.adobe.com/flex/3/html/help.html?content=CommandLineTools_5.html#1035876

Now that we understand amxml and ADT we can move onto the ant task. Firstly you need to define certain properties in your build file to use your ant task;

<property name=”SDK_HOME” value=”/Applications/Adobe Flex Builder 3 Plug-in/sdks/3.1.0/”/>
<property name=”MXMLC.JAR” value=”${SDK_HOME}/lib/mxmlc.jar”/>
<property name=”ADL” value=”${SDK_HOME}/bin/adl”/>
<property name=”ADT.JAR” value=”${SDK_HOME}/lib/adt.jar”/>

They are basically pointing to the flex SDK. I’ve installed flex builder so i point the SDK_HOME property task to its install directory (on a mac this is found under applications). Then define some app specific global variables;

<property name=”APP_NAME” value=”Test”/>
<property name=”APP_DESCRIPTOR” value=”${APP_NAME}-app.xml”/>
<property name=”VERSION” value=”1.1″/>
<property name=”APP_ROOT” value=”.”/>
<property name=”MAIN_SOURCE_FILE” value=”${APP_ROOT}/src/${APP_NAME}.mxml”/>
<property name=”AIR_NAME” value=”${APP_NAME}.air”/>
<property name=”DEBUG” value=”true”/>
<property name=”STORETYPE” value=”pkcs12″/>
<property name=”KEYSTORE” value=”CodeCert.p12″/>

Once this is done, define a property for the build, release and assets directories

<property name=”build”  location=”${APP_ROOT}/bin-debug”/>
<property name=”assets”  location=”${APP_ROOT}/src/assets”/>

There are only two tasks worth considering. Everything else is fairly straight forward. The two
tasks I’m referring to are;

<target name=”compile” depends=”init”>
<java jar=”${MXMLC.JAR}” fork=”true” failonerror=”true” maxmemory=”512m”>
<arg value=”-debug=false”/>
<arg value=”+flexlib=${SDK_HOME}/frameworks”/>
<arg value=”+configname=air”/>
<arg value=”-external-library-path+=libs/as3corelib.swc,libs/as3xls-1.0.swc”/>
<arg value=”-warnings=false”/>
<arg value=”-file-specs=${MAIN_SOURCE_FILE}”/>
<arg value=”-output=${build}/${APP_ROOT_FILE}”/>
</java>
</target>

<target name=”package” depends=”compile”>
<java jar=”${ADT.JAR}” fork=”true” failonerror=”true”>
<arg value=”-package”/>
<arg value=”-tsa”/>
<arg value=”none”/>
<arg value=”-storetype”/>
<arg value=”${STORETYPE}”/>
<arg value=”-keystore”/>
<arg value=”${KEYSTORE}”/>
<arg value=”-storepass”/>
<arg value=”c1d2m3s4″/>
<arg value=”${release}/${AIR_NAME}”/>
<arg value=”${build}/${APP_DESCRIPTOR}”/>
<arg value=”-C”/>
<arg value=”${build}”/>
<arg value=”${APP_NAME}.swf”/>
<arg value=”-C”/>
<arg value=”${build}”/>
<arg value=”assets”/>
<arg value=”-C”/>
<!– Specific to Mitto –>
<arg value=”${build}”/>
<arg value=”MittoCFG.xml”/>
</java>
</target>

Most of it is self explanatory, but a couple of things to note; the ant AIR compiler/packer uses a java task. This is why the above
looks vaguley similar to the way that the compiler was invokved on the command line above. The <arg> tag is as simple as it sounds, it is a means to specify in ant language a command line argument.

The java task can be memory intensive, which is why there is a maxmemory attribute is there and the fork attribute is set to true. The option tsa is an attribute used to specify a timestamping authority. If set to false it AIR will not timestamp the package. Sometimes where you are behind a corporate firewall or proxy, it helps to turn this off.

And there you have it, theres not much to it really. Getting it to work first time can be fidly, but once its running its extremely smooth.

NOTES

NOTE: If you get this error, “Error: Problem finding external stylesheet:” then you probably have a
style tage somewere with a declaration similar to this;

<mx:Style source=”assets/styles/default.css”/>

You asset folder is underneath the source folder and the compiler cannot not locate it. A quick fix
is to change the reference by adding an extra forward slash at the beginning as follows;

<mx:Style source=”/assets/styles/default.css”/>

NOTE: The amxmlc command invokes the standard Flex mxmlc compiler with an additional parameter,
+configname=air. This parameter instructs the compiler to use the air-config.xml file instead of
the flex-config.xml file. Using amxmlc is otherwise identical to using mxmlc.

NOTE: If you get this  you need to add the following properties to your ant task

fork=true
maxmemory=”512m”

adjust maxmemory as needed

NOTE: If you get the following error;

error 105: application.initialWindow.content contains an invalid value

In you app descriptor file (myapp-app.xml) you have a tag called <content> nested in a tag called
<initialWindow>. It has a value something like;

[This value will be overwritten by Flex Builder in the output app.xml]

This needs to be changed to the name of the main swf file in order for the build script to work.

e.g. myapp.swf

References

http://livedocs.adobe.com/flex/3/html/help.html?content=CommandLineTools_7.html
http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7fa1.html
http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postId=12175
http://flexcomps.wordpress.com/2008/08/18/appolo-command-line-tools/

Categories: Flex Tags: ,
  1. No comments yet.
  1. No trackbacks yet.