How to Use SWC as RSL in Pure AS3 Projects

Using SWC as RSL in Flex projects is a no brainer. Flash Builder and Flex ant task automate a lot of things for your con­venience. Unfor­tunately, that’s not the case with pure AS3 projects. I read somewhere that you can use a special Frame tag and a factory class to load the RSL swf before you instan­tiate your document class. I tried that but I got mixed results; it didn’t always work in all cases especially when the SWC con­tains a lot of assets. I might be doing something wrong; I don’t know.

It tur­ned out that you can use a “loader” SWF to load the RSL swf before your main SWFs to achieve the same thing. That left me with one problem, how to get the RSL swf out of the SWC. As we know, SWC is just another comp­res­sion for­mat, just like zip so any zip expander/extractor will work but manually extracting the SWC is going to be a pain in the @$$ and you’ll have to do it every time you update your SWC. This is where Ant becomes your best friend ;-)

In this post, I’ll show you how to do it with Flash Builder & Ant.

Creating Projects

First, we create a new works­pace and add build folder to store the published SWFs and then create the projects below:

  1. MyLibs — Flex library project. This is the RSL to be used by AppOne and AppTwo.
  2. AppOne — AS3 project
  3. AppTwo — AS3 project
  4. loader — AS3 project that will load all other SWFs

Then we add a Car class to MyLibs project and com­pile the project so we have MyLibs.swc in its bin/ folder.

App Projects

Now let’s open AppOne project properties.

  1. Go to Action­Script Build Path settings
  2. Add Mylibs.swc from MyLibs project and set its lin­kage type “exter­nal
  3. Open AppOne.as
  4. Instan­tiate Car class

Repeat those steps for AppTwo.

The Loader

Below is the loader.as file that loads everything.

ANT Script

Here’s the ANT script that extracts MyLibs.swc and put MyLibs.swf in the build folder.

And here’s a longer ver­sion If you want to com­pile your projects with ANT as well.

Tes­ting

When you open loader.swf you should see the following trace output in flashlog.txt:

How do we know that Car class didn’t get com­piled into the app SWFs? Well, if you have a decom­piler like Sothink SWF­Decom­piler, you can check it visually and see that Car is not com­piled into those SWFs.

Decom­piled SWFs

Here’s the zip con­taining the sam­ples → as3rsl.zip

Also in this category …


Comments are closed.