Subcribe via RSS

How to build a SWC package with Flex and Ant

September 20th, 2011 | No Comments | Posted in Flash

Packaging classes in a SWC file is a good idea when you need to distribute your source code to a client, in order to publish a Flash movie, or when you work with other team members and you need to be sure the code remains unchanged.

With the Flex SDK we can generate SWC files from a pure AS3 source folder very easily. First of all we need to download the SDK and make the binary folder accesible in our system path.

Imagine we have a src folder inside our project folder and we want to add all the source code that is inside to a SWC package. To do that we will need to open a command line prompt and go to the directory of the project folder and execute the following:

compc -include-sources ./src/ -debug=false -optimize -output ./output.swc

Flex will generate a package called output.swc that contains all the classes inside src. Of course you should customize the parameters to fit your needs.

As you see this process is quite simple but it is worth making it automatic. Here is where we will use Apache Ant.

For those who are unaware of Ant, this tool was created to automate the software build process. Essentially it was for Java projects but now it can be used in multiple different ways because of the flexibility Ant provides.

Ant was implemented using Java and needs Java to work. It’s recommended you download and use the latest JDK release. The JRE can be used as well but some of the tasks may not be available.

Once you have Java up and running you can go ahead and download Ant here. Decompress the package and add the path to the Ant binary folder to your system path.

Now we have everything we need!

Go to your project folder and create a file called build.xml with the following content:

1
2
3
4
5
6
7
8
9
10
11
<project name="SWCbuilder" default="build_swc" basedir=".">
  <target name="build_swc">
    <echo>Building SWC...</echo>
    <exec executable="compc.exe" failonerror="true">
      <arg line="-include-sources ./src/"/>
      <arg line="-debug=false"/>
      <arg line="-optimize"/>
      <arg line="-output ./output.swc"/>
        </exec>
  </target>
</project>

Basically what we do here is to define a task called build_swc that executes the Flex compiler sending the parameters we saw previously. Ant has lots of different options and you can improve this build file adding properties that would specify the source folder, output folder and output file name for example.

The last thing we have to do is to call Ant and execute the task we just created. Go to the folder of your project with the command line and execute the following:

ant build_swc

And that’s it! You have your SWC file ready to distribute and you don’t need to remember more than ant and the task you want to execute!

I love Ant :)