trace( "maybe this will work…" );

How to Create a SWC Library from Your AS3 Code

Problem: Create a SWC library from your existing AS3 code. A SWC library is a pre-compiled file of classes and (optionally) graphical assets. They are useful for creating themes (see Creating Themes), distributing an application without distributing its source code, or for creating a compressed copy of your code library. As an example, the Flare library ships with a pre-compiled SWC that contains the entire library in one SWC file. If you’re worried about efficiency, compiling against a SWC library works exactly the same as compiling from source: only classes that you actually use are included in your final SWF. If you have a reusable code base that you want to compile into a single file, SWC’s are for you.

Solution: Flex Ant tasks.

This post assumes that you have Flex and Ant already installed. You can find the Flex installation page here and the Ant home page here. (If you’d be interested in a tutorial covering Flex/Ant, installation, please post a comment).

All you need to compile a SWC file containing your AS3 code is a build.properties file, a build.xml file, and, of course, a directory containing your code. First we’ll cover the contents of these files, then we’ll talk about how to use them in conjunction with Ant.

The ‘build.properties’ file is a simple text file that contains the configuration options for your SWC library. The following build.properties file will take all the code in the ‘myCode’ directory and compile it into a single SWC file called ‘library.swc’. Remember to change ‘FLEX_HOME’ to the path where you installed Flex.

# build.properties

# Change this to the installation directory of FLEX
FLEX_HOME = C:/Documents and Settings/Administrator/My Documents/alg/flex/

# This is the name the swc file that is generated
OUTPUT = library.swc

# This is the name the directory containing the code that the swc
# should include.
LIBRARY = myCode

# Don't change this (it's the path to the compiler to use)
compc.exe = ${FLEX_HOME}/bin/compc.exe

build.properties is the actual file (right-click and select “Save Link As…”).

The ‘build.xml’ file contains the commands that Ant will actually run. For a detailed interpretation of this file, refer to Flex Ant Tasks. You shouldn’t need to change this file, aside from adding a few more ‘‘ lines for advanced options (you’ll have to consult the Compc Reference or the Mxmlc Reference for a list of compiler arguments. A few common arguments like ‘-target-player’ are included, but commented out.

<!-- build.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project name="Build SWC Library" default="build-swc-lib">

        <!-- Include the build.properties file -->
        <property file="./build.properties"/>
       
        <!-- Point Ant to the Flex installation -->
        <taskdef resource="flexTasks.tasks"
                         classpath="${FLEX_HOME}/ant/lib/flexTasks.jar"/>
               
        <!-- Define the commands to generate a SWC library -->
        <target name="build-swc-lib" description="Generates a SWC library from a directory of as3 classes">
                <echo>Generating SWC Library</echo>
                <exec executable="${compc.exe}">
                        <!-- Set the output path -->
                        <arg line="-output ${OUTPUT}"/>
                        <!-- Include all of the AS3 files in LIBRARY -->
                        <arg line="-include-sources ${LIBRARY}"/>
                        <!-- Compile for Flash Player 10.0.0 and higher -->
                        <!-- <arg line="-target-player=10.0.0"/> -->
                        <!-- Turn off strict mode -->
                        <!-- <arg line="-strict=false"/> -->
                </exec>
        </target>
</project>

build.xml is the actual file (right-click and select “Save Link As…”).

To use these files with Ant, download both of them and copy them into one directory above where your code library resides. In the example, this is the directory containing the ‘myCode’ folder (not the ‘myCode folder itself). Open a console and change to the directory containing build.xml and build.properties. Now run the ‘ant’ command. Simply type the following into the console:

ant

The console should print ‘Generating SWC Library,’ along with some other technical details, and then you should have a nice new SWC appear in that same folder.

If you experience some compiler errors, remember that the ENTIRE library is being compiled, so it needs a path to EVERY other external AS3 file that the library references. For example, if you are compiling something that requires the Flare library, you will need to give add an argument in ‘build.xml’ that points to the Flare library source on your computer. Otherwise, you will get a seemingly endless list of errors. This can be done with either of the following lines:

<!-- This is for linking other SWC libraries -->
<arg line="-library-path path/to/flare.swc"/>

<!--- or --->

<!-- This is for linking directories or classes -->
<arg line="-source-path path/to/flare/src"/>

Now, you might be asking, how do I actually use a SWC file within an application? Well, if you have Flash CS4, you can add a path to the SWC under ‘Publish Settings->ActionScript3.0 Settings…->External Library Path’. I’m not sure if this works in Flash CS3, but you can definitely save your external SWC into ‘Adobe Flash CS3\Common\Configuration\Components’, where ‘Adobe Flash CS3‘ is your Flash installation directory. Then, the classes in these SWC files will be available in the Flash IDE. This method will also work in Flash CS4. In short, compiling against a SWC file is essentially the same as compiling against AS3 source files, you just have to tell Flash where to look for the SWC file.

This tutorial was meant to be a quick introduction to using Flex and Ant. Creating SWC code libraries is only one of the many uses of the Flex compilers. It is by no means a comprehensive overview of Flex Ant Tasks. If you would like to see a more detailed tutorial about Flex and Ant, please post a comment below. For now, please refer to Using the Flex Compilers from Adobe.

December 8th, 2009 at 3:33 pm

Posted in Flash

15 Responses to 'How to Create a SWC Library from Your AS3 Code'

Subscribe to comments with RSS or TrackBack to 'How to Create a SWC Library from Your AS3 Code'.

  1. It was a very nice theme! Just wanna say thank you for the data you have apportioned. Just continue writing this kind of post. I will be your true reader. Thanks again.

  2. Thanks again for a nice site.

  3. Couldn?t be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!

    Trinity Brenton

    14 Jan 10 at 3:33 pm

  4. Hello. Great job on this post

    Jeffrey

    17 Jan 10 at 2:23 am

  5. Thanks :-)

    Ryan

    17 Jan 10 at 5:01 am

  6. Thanks so much for this! There are sooo few examples of how to make SWC’s out there, this is a beacon of clarity. Shame that when i tried to knock out a Partigen2 (the great as3 particle engine) library, Ant spat out a couple of compile errors: 2 missing classes which were definitely there. Why they don’t include SWC’s in these otherwise lovely packages is beyond me. Maybe they can’t figure the black art of SWC-making either!

    Could the compile errors be down to the fact that I’m using the FLEX SDK rather than Flex Builder?

    chichilatte

    28 Feb 10 at 1:01 pm

  7. I’m glad you found this useful. I don’t think that using the Flex SDK instead of Flex Builder should make much of a difference. My best guess would be that a classpath is missing somewhere, so Ant can’t find your missing classes…I’ll take a look at the library and let you know if I figure that one out.

    Ryan

    1 Mar 10 at 1:13 pm

  8. :) ppreciate it. Pretty sure I followed all instructions to the letter.

    chichilatte

    4 Mar 10 at 10:25 am

  9. The second I began to look over this post, I felt kind of mystified. I get it finally. Now I’m in a very good frame of mind, consequently I gave everyone a link to a free Best Buy gift card. Just remember please don’t take advantage of it to order lame crap.

    Jacquetta Aubry

    26 Mar 10 at 6:10 am

  10. Very nice, gives me a lot of inspiration being right in the midst of a redesign. It抯 all in the details and comment forms are definitely overlooked more often than not. Time well spent on this post.

  11. Another interesting post, Great stuff. I wanted to say I just made a point of reading other entries you made and I have to say it’s pretty good. Cheers.

    Merri Gobble

    21 Oct 10 at 6:31 pm

  12. Usefull article do you mind if i translate into Dutch for our blogs readers? Thanks

    Bea Melara

    11 Nov 10 at 12:51 pm

  13. [...] creato un file .swc (utilizzando Flash Builder, ma è ovviamente possibile appoggiarsi ad altre soluzioni) contenente la libreria [...]

  14. What’s a good wordpress blog theme for an icanhascheeseburer or failblog clone?

    Theodore Degrave

    23 Jun 11 at 9:56 am

  15. Flash (animation program) 2d . Maya (animation program) 3d. Photoshop (for building websites). Image Ready (for building websites—this is a little older than…). Dreamweaver (website building)—this is the most current program for building websites. or/and Go Live which launches websites.. . Go Live, Photoshop, Image Ready all come together if you get the Adobe Creative Suite CS.

    Bethann Garlits

    28 Jun 11 at 9:07 am

Leave a Reply