How to Create SWC Actionscript Libraries

by troy on March 4, 2010

SWC's are semi mysterious and powerful. Today I'll show why they are important, how they can make your life easier, and a how to make them for actionscript libraries.

What's a SWC?

SWC's are like SWF's in that that incorporate all your compiled ActionScript, CSS, assets. They are alot like a zip or rar to keep a folder of things together. They differ from a SWF in that they are meant for compile time use, Flash or Flex can use the contents inside a SWC without having to have the original source files.

Most commonly they are used for visual components, to pass around that new widget you've created in a protected fashion.  But they are also used for non-visual components. Like you may not know this but when you compile anything in Flex, or build with Flash IDE.  it uses many SWC's behind the scene that correspond to what Flash player target you've requested. Like playerglobal.swc, contains all the interfaces to the functions built in to the player, flex.swc has all the flex related code you link against when you import anything from  mx.*

What's a SWC do for me?

When dealing with teams and clients, two things eventually happen and SWC's help in both. One is that you develop or rely upon your own or 3rd party libraries to do things and you have to pass this around. PaperVision for 3D, maybe a physics library like Box2D, some sort of framework to glue it all together.

Everybody needs to have those files. The team needs it when working with you on shared code, but probably don't want or can't deal with source control to get them, or maybe they shouldn't have access to your code at all, just be able to use it. Like if your just hired a contractor to help out do the animations on that game, they just want enough to be get it to build, and very well may have no clue about SVN/CVS.

Clients want all the files so if you get hit by a bus they can have the peace of mind, the project isn't on the bus too. Also they  can hand it off to some other developer/team for maintenance when you've gotten so busy.  Or perhaps they are using it in an automated build environment so while the code you write for them is in a source control, they don't want to track 3rd party libaries, as those invariable change and break things. A single zip of only what's needed locked in time is best, and that's what a SWC can be.

3 Ways to create SWC Libraries.

Looking for myself I ran across this post which describes what I needed perfectly.

I have a folder of pure AS files.
This folder is a package of AS classes.
(No main AS file. This package acts as a library.)

My library directory is "D:/libraries/mylibrary"
The output file path is "D:/libraries/bin/mylibrary.swc"

Turns out this is not as easy as it would seem,  most component tuturials focus on how to package a visual component (e.g. a button) into a SWC so you can sell it.

You can do it 2 ways

1) in Flash CS3 and CS4 this post covers roughly how that works in it's coverage how to create partial SWC packages

2) using the Flex compc component compiler which has many rather lightly documented parameters to specify what classes on a, or folders to include, and some of those classes could live in their own SWC!

which sounds scary but it's more tedious than anything.  Bascially you pass compc either a list of entry points of class names (e.g com.yoursite.cMain), or a entry point of folders to target (e.g . C:/actionscript/com/yoursite/), it prefer that you be verbose meaning that every folder.

This post talks about how to create a SWC using flash develop and ExportSWC this approach is great if you have say a whole library of files that have no main.  In it's build all option it first goes through athrough the all the files on the classpath and creates a manifest xml  to pass to the compc.

This turned out to not be good in my case as I had 3 libraries and there were many things not quite ported from it's AS2 days, plus it was rather big so I went with plan B.

First I went through the script files created for my client, and copied all the import paths to the 3rd party libraries, including my framework.

Second I created a new, that's only goal was to reference each of those classes it looked like

package  {
import flash.display.Sprite;
import com.troyworks.core.Signals;
import com.troyworks.core.cogs.CogEvent;
public class SWCMain extends Sprite {
private var  a : Class = Signals; //just a pointer so that the compiler picks it up
private var  b : Class = CogEvent; //if that references other classes it will include them too!
public function SWCMain() : void {

I put this file in a new project/folder called libs src, this was the only actionscript in that project and instead of building a swf as in normal development I'd be outputting a swc instead.

Then since I'm on windows I created a batch file, it needed 4 things

  1. the path of the flex compiler,
  2. the classpath to where the imported original source code lives,
  3. a path to this that tells it were to start from, in this case I use a folder
  4. what to call and where to ouput the SWC

That bat file looked like this, which roughly has one line that corresponds to that list.

-source-path C:\TroyWorksAS3\dev\src
-include-sources C:\TroyWorksAS3\libs\src
-optimize -output C:\TroyWorksAS3\libs\bin\myCustom.swc

Ran this in a command line environment and after a minute or so it output the swc.
You can test that swc by using that to output a SWF via a conventional build, if you have that SWC on the classpath then it should find all the files, and then you can give it to your team.

It's a good practice if your working on the originals to not link against the SWC, to give you the illusion that the SWC's are updated when they aren't. Classes in SWC's are considered after whatever import statements you have, so if a newer file is found it won't complain that the SWC is out of date.

I hope that this tutorial has made they mysteries around SWC's a bit clearer! so try creating one it's shouldn't take more than a few minutes with this knowhow!

{ 3 comments… read them below or add one }

Nathan March 5, 2010 at 6:44 am

Thanks a lot for sharing this valuable info.
Will try it out. :)

Angelo November 2, 2013 at 4:56 pm

Actually I learned here, but Im just curious on how did greensock and other libraries exposed there codes that are not in an SWC format.?

troy November 28, 2013 at 1:46 am

SWC’s are just zips of swfs and assets, so as long as ApplicationDomain is the shared and the swfs are loaded into the same domain the logic and assets are accessible.

Leave a Comment


Previous post:

Next post: