Using Apache Ant with the SDK tools



This topic provides examples of using the Apache Ant build tool to test and package AIR applications.

Note: This discussion does not attempt to provide a comprehensive outline of Apache Ant. For Ant documentation, see http://Ant.Apache.org.

Using Ant for simple projects

This example illustrates building an AIR application using Ant and the AIR command line tools. A simple project structure is used with all files stored in a single directory.

To make it easier to reuse the build script, these examples use several defined properties. One set of properties identifies the installed locations of the command line tools:

    <property name="SDK_HOME" value="C:/AIRSDK"/> 
    <property name="ADL" value="${SDK_HOME}/bin/adl.exe"/> 
    <property name="ADT.JAR" value="${SDK_HOME}/lib/adt.jar"/>

The second set of properties is project specific. These properties assume a naming convention in which the application descriptor and AIR files are named based on the root source file. Other conventions are easily supported.

    <property name="APP_NAME" value="ExampleApplication"/> 
    <property name="APP_ROOT" value="."/> 
    <property name="APP_DESCRIPTOR" value="${APP_ROOT}/${APP_NAME}-app.xml"/> 
    <property name="AIR_NAME" value="${APP_NAME}.air"/> 
    <property name="STORETYPE" value="pkcs12"/> 
    <property name="KEYSTORE" value="ExampleCert.p12"/>

Invoking ADL to test an application

To run the application with ADL, use an exec task:

<target name="test" depends="compile"> 
<target name="test"> 
    <exec executable="${ADL}"> 
        <arg value="${APP_DESCRIPTOR}"/> 
    </exec>  
</target>

Invoking ADT to package an application

To package the application use a Java task to run the adt.jar tool:

<target name="package"> 
    <java jar="${ADT.JAR}" fork="true" failonerror="true"> 
        <arg value="-package"/> 
        <arg value="-storetype"/> 
        <arg value="${STORETYPE}"/> 
        <arg value="-keystore"/> 
        <arg value="${KEYSTORE}"/> 
        <arg value="${AIR_NAME}"/> 
        <arg value="${APP_DESCRIPTOR}"/> 
        <arg value="${APP_NAME}.html"/> 
        <arg value="*.png"/> 
    </java> 
</target>

If your application has more files to package, you can add additional <arg> elements.

Using Ant for more complex projects

The directory structure of a typical application is more complex than a single directory. The following example illustrates a build file used to compile, test, and package an AIR application which has a more practical project directory structure.

release
This sample project stores application source files and other assets like icon files within a src directory. The build script creates a release directory to store the final AIR package.

The AIR tools require the use of some additional options when operating on files outside the current working directory:

Testing
The second argument passed to ADL specifies the root directory of the AIR application. To specify the application root directory, the following line is added to the testing task:
<arg value="${debug}"/>

Packaging
Packaging files from subdirectories that should not be part of the final package structure requires using the -C directive to change the ADT working directory. When you use the -C directive, files and directories in the new working directory are copied to the root level of the AIR package file. Thus, -C build file.png copies file.png to the root of the application directory. Likewise, -C assets icons copies the icon folder to the root level, and copies all the files and directories within the icons folder as well. For example, the following sequence of lines in the package task adds the icons directory directly to the root level of the application package file:
<arg value="-C"/> 
<arg value="${assets}"/> 
<arg value="icons"/>
Note: If you need to move many resources and assets into different relative locations, it is typically easier to marshall them into a temporary directory using Ant tasks than it is to build a complex argument list for ADT. Once your resources are organized, a simple ADT argument list can be used to package them.
<project> 
    <!-- SDK properties --> 
    <property name="SDK_HOME" value="C:/AIRSDK"/> 
    <property name="ADL" value="${SDK_HOME}/bin/adl.exe"/> 
    <property name="ADT.JAR" value="${SDK_HOME}/lib/adt.jar"/> 
 
    <!-- Project properties --> 
    <property name="PROJ_ROOT_DIR" value="."/> 
    <property name="APP_NAME" value="ExampleApplication"/> 
    <property name="APP_ROOT_DIR" value="${PROJ_ROOT_DIR}/src/html"/> 
    <property name="APP_ROOT_FILE" value="${APP_NAME}.html"/> 
    <property name="APP_DESCRIPTOR" value="${PROJ_ROOT_DIR}/${APP_NAME}-app.xml"/> 
    <property name="AIR_NAME" value="${APP_NAME}.air"/> 
    <property name="release"  location="${PROJ_ROOT_DIR}/release"/> 
    <property name="assets"  location="${PROJ_ROOT_DIR}/src/assets"/> 
    <property name="STORETYPE" value="pkcs12"/> 
    <property name="KEYSTORE" value="ExampleCert.p12"/> 
     
        <target name="init" depends="clean"> 
        <mkdir dir="${release}"/> 
    </target> 
 
    <target name="test"> 
        <exec executable="${ADL}"> 
             <arg value="${APP_DESCRIPTOR}"/> 
            <arg value="${APP_ROOT_DIR}"/> 
             </exec>  
    </target> 
 
    <target name="package" depends="init"> 
        <java jar="${ADT.JAR}" fork="true" failonerror="true"> 
            <arg value="-package"/> 
            <arg value="-storetype"/> 
            <arg value="${STORETYPE}"/> 
            <arg value="-keystore"/> 
            <arg value="${KEYSTORE}"/> 
            <arg value="${release}/${AIR_NAME}"/> 
            <arg value="${APP_DESCRIPTOR}"/> 
            <arg value="-C"/> 
            <arg value="${APP_ROOT_DIR}"/> 
            <arg value="${APP_ROOT_FILE}"/> 
            <arg value="-C"/> 
            <arg value="${assets}"/> 
            <arg value="icons"/> 
        </java> 
    </target> 
 
    <target name="clean" description="clean up"> 
        <delete dir="${release}"/> 
    </target> 
</project>