Skip to main content

Examples

In the following examples are shown some of the most common usages of the ant tasks.

Compiling SWCs

Note most of the following can be equally applied to mxmlc to build a swf.

Basics

Build the all the source in src into bin/output.swc library:

build.xml
<?xml version="1.0"?>
<project name="example" default="compile" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />

<target name="compile">
<mkdir dir="${output.dir}"/>
<compc output="${output.dir}/output.swc" failonerror="true" maxmemory="1024m">
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
</compc>
</target>

</project>

Specify Configuration

The following specifies the configuration to use for compilation. You use the load-config tag to specify an xml configuration, this can either be a custom one, or one of the 2 default configuration files included with the AIR SDK:

frameworks/air-config.xml
frameworks/flex-config.xml

These represent build configurations for AIR and FLEX libraries respectively. They include all the references to the AIR and Flex frameworks allowing your code to access the standard framework classes.

build.xml
<?xml version="1.0"?>
<project name="example" default="compile" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />

<target name="compile">
<mkdir dir="${output.dir}"/>
<compc output="${output.dir}/output.swc" failonerror="true" maxmemory="1024m">
<load-config filename="${env.AIR_HOME}/frameworks/air-config.xml" />
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
</compc>
</target>

</project>

Including Libraries

The library-path option allows you to add other swc's as a dependency.

build.xml
<?xml version="1.0"?>
<project name="example" default="compile" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />

<target name="compile">
<mkdir dir="${output.dir}"/>
<compc output="${output.dir}/output.swc" failonerror="true" maxmemory="1024m">
<load-config filename="${env.AIR_HOME}/frameworks/air-config.xml" />
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
<library-path file="lib.swc" append="true"/>
</compc>
</target>

</project>

Or you could include a directory containing a series of swc libraries:

build.xml
<?xml version="1.0"?>
<project name="example" default="compile" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />
<property name="libraries.dir" value="libs" />

<target name="compile">
<mkdir dir="${output.dir}" />
<compc output="${output.dir}/output.swc" failonerror="true" maxmemory="1024m">
<load-config filename="${env.AIR_HOME}/frameworks/air-config.xml" />
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
<library-path dir="${libraries.dir}" append="true">
<include name="*.swc" />
</library-path>
</compc>
</target>

</project>

Metadata

You can use the keep-as3-metadata tag to specify the metadata tags to keep in the compiled code:

build.xml
<?xml version="1.0"?>
<project name="example" default="compile" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />

<target name="compile">
<mkdir dir="${output.dir}" />
<compc output="${output.dir}/output.swc" failonerror="true" maxmemory="1024m">
<load-config filename="${env.AIR_HOME}/frameworks/air-config.xml" />
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
<keep-as3-metadata name="Inject" />
<keep-as3-metadata name="PostConstruct" />
</compc>
</target>

</project>

Compiler Defines

Compiler definitions allow you to pass in values into your code at compilation time.

build.xml
<?xml version="1.0"?>
<project name="example" default="compile" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />

<property name="version" value="1.0.0" />

<target name="compile">
<mkdir dir="${output.dir}" />
<compc output="${output.dir}/output.swc" failonerror="true" maxmemory="1024m">
<load-config filename="${env.AIR_HOME}/frameworks/air-config.xml" />
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
<compiler.define name="CONFIG::VERSION" value="'${version}'" />
</compc>
</target>

</project>

Then in your actionscript:

Main.as
package 
{
import flash.display.Sprite;

public class MyApp extends Sprite
{
public static const VERSION : String = CONFIG::VERSION;

public function Main():void
{
trace( VERSION ); // outputs 1.0.0 as defined in the build script
}
}
}

Reports

You can output a few reports and the configuration from the task to analyse the result of the compilation. These are passed directly as attributes on the compc task

  • dump-config: write a file containing all currently set configuration values;
  • size-report: output an XML-formatted report detailing the size of all code and data;
  • link-report: output a XML-formatted report of all definitions linked;
    <compc ...  
dump-config="${output.dir}/config.xml"
size-report="${output.dir}/sizereport.xml"
link-report="${output.dir}/linkreport.xml" >

Compiling SWF

Most of the above can be applied to the mxmlc command to build a swf.

Basics

Build the all the source in src into bin/output.swf library:

build.xml
<?xml version="1.0"?>
<project name="example" default="compile" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />

<target name="compile">
<mkdir dir="${output.dir}"/>
<mxmlc output="${output.dir}/output.swf" failonerror="true" maxmemory="1024m">
<load-config filename="${env.AIR_HOME}/frameworks/air-config.xml" />
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
</mxmlc>
</target>

</project>

Compiling with ANEs

ANEs are a special type of library and require additional attention when building your application swf / swc. An ANE is essentially a SWC to the compilers and should be linked as an external library which will be included when packaging your AIR application using adt.

However, if you simply add an ANE as an external-library-path it will be ignored. You need to copy and rename these .ane files to .swc before compiling in order to correctly compile your swc or swf that references an ane. Fortunately this is easy with Ant:

build.xml
<?xml version="1.0"?>
<project name="ane_example" default="compile" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />
<property name="ane.dir" value="ane" />
<property name="ane.swc.dir" value="ane-swc" />

<target name="create-ane-swc">
<!-- Copy ANE files to the ane-swc and rename to .swc -->
<delete dir="${ane.swc.dir}" />
<copy todir="${ane.swc.dir}">
<fileset dir="${ane.dir}">
<include name="*.ane" />
</fileset>
<globmapper from="*.ane" to="*.swc"/>
</copy>
</target>

<target name="compile" depends="create-ane-swc" >

<mkdir dir="${output.dir}"/>
<mxmlc output="${output.dir}/output.swf" failonerror="true" maxmemory="1024m">
<load-config filename="${env.AIR_HOME}/frameworks/air-config.xml" />
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
<external-library-path dir="${ane.swc.dir}" append="true">
<include name="**/*.swc"/>
</external-library-path>
</mxmlc>

<delete dir="${ane.swc.dir}" />
</target>

</project>

Packaging AIR

Packaging an AIR project uses the exec Ant task which allows you to call an executable.

<exec executable="${air.adt}" failonerror="true">
<arg line="-package"/>
<arg line="-storetype pkcs12"/>
<arg line="-keystore certificate.p12"/>
<arg line="-storepass ${password}"/>
<arg line="${output.dir}/MyApp.air"/>
<arg line="${source.dir}/MyApp-app.xml"/>
<arg line="-C ${output.dir} output.swf"/>
</exec>

As you have to use adt.bat on Windows and adt on macOS there is a useful Ant script we can use to select the appropriate executable:

<!-- Simple condition to switch between adt.bat and adt -->
<condition property="isWindows" else="false">
<os family="windows" />
</condition>
<condition property="air.adt"
value="${env.AIR_HOME}/bin/adt.bat"
else="${env.AIR_HOME}/bin/adt">
<istrue value="${isWindows}"/>
</condition>

In the following we compile our source using mxmlc and then build an .air package from the output.swf using the MyApp-app.xml AIR application descriptor:

build.xml
<project name="air_example" default="build" >

<property environment="env."/>
<taskdef resource="flexTasks.tasks" classpath="${env.AIR_HOME}/ant"/>

<property name="output.dir" value="bin" />
<property name="source.dir" value="src" />
<property name="ane.dir" value="ane" />
<property name="ane.swc.dir" value="ane-swc" />

<!-- Copy ANE files to the ane-swc and rename to .swc -->
<target name="create-ane-swc">
<delete dir="${ane.swc.dir}" />
<copy todir="${ane.swc.dir}">
<fileset dir="${ane.dir}">
<include name="*.ane" />
</fileset>
<globmapper from="*.ane" to="*.swc"/>
</copy>
</target>

<!-- Compile a swf from our code -->
<target name="compile" depends="create-ane-swc" >
<mkdir dir="${output.dir}"/>
<mxmlc output="${output.dir}/output.swf" failonerror="true" maxmemory="1024m">
<load-config filename="${env.AIR_HOME}/frameworks/air-config.xml" />
<source-path path-element="${source.dir}"/>
<include-sources dir="${source.dir}" includes="*"/>
<external-library-path dir="${ane.swc.dir}" append="true">
<include name="**/*.swc"/>
</external-library-path>
</mxmlc>
<delete dir="${ane.swc.dir}" />
</target>

<!-- Simple condition to switch between adt.bat and adt -->
<condition property="isWindows" else="false">
<os family="windows" />
</condition>
<condition property="air.adt" value="${env.AIR_HOME}/bin/adt.bat" else="${env.AIR_HOME}/bin/adt">
<istrue value="${isWindows}"/>
</condition>

<!-- Package an air from our code -->
<target name="build" depends="compile">

<exec executable="${air.adt}" failonerror="true">
<arg line="-package"/>
<arg line="-storetype pkcs12"/>
<arg line="-keystore certificate.p12"/>
<arg line="-storepass ${password}"/>
<arg line="${output.dir}/MyApp.air"/>
<arg line="${source.dir}/MyApp-app.xml"/>
<arg line="-C ${output.dir} output.swf"/>
</exec>

</target>

</project>