Creating partial package SWC files 14

If you’re a Flex developer, SWC’s are nothing new to you. But for Flash dev’s, SWC files are one of those ‘yea I’ve seen those, but I don’t totally understand how, what, or why I’m supposed to use them’ things in Flash development. They’re incredibly useful – if you know exactly how to use them. Especially in CS3 (CS4 made it a little less painless with the explicit abiliy to specify SWC files as internal or external libraries). Unfortunately, generating them can be even more confusing. there just isn’t much documentation, and if you use the flex sdk and/or FlashDevelop, the compc.exe command line options are daunting.

Luckily, it isn’t as hard as it seems. For developers, one of the best uses for SWC (Flash builds require a matching SWF file as well) files in flash AS3 dev is packing up pre compiled libraries for use at runtime ( you can also package assets, but that isn’t what we are here to talk about). It’s pretty straightforward using the export SWC option under publish settings. But what if you need to export only a portion of the package as a SWC/SWF library?

I did some searching, and the few topics I found that mentioned this had replies that questioned the merits of the idea instead of proposing how to go about it. In my case, for Fosfr, I was looking to produce an external library file, but needed to include about 13kb of the entire 50kb package in the core SWF file to properly launch the loading process of the framework, which would then go and get the rest of the library before running any code that needed the full framework library. So there are legitimate cases where you may want to do this. And actually, it can be done.

So, how to go about it? The problem lies in that generating SWC files pulls all classes referenced from your chosen classes along with them into the SWC. so, say you are using TweenLite in your package that you wish to turn into a SWC. Check the SWC afterwards and you’ll see that the TweenLite package got sucked in there too. So how can this be avoided? To do it you’ll need to use one of the powers of SWC’s themselves -compile time class referencing – and one of their usage nuisances – priority of classpaths over SWCs in Flash.

Here’s the steps to generate a partial package SWC:

1 – Create an FLA to make a SWC and generate a full SWC of the entire package you wish to generate as a SWC. Dont worry about file size or excess files, this file is for reference.

2 – Take the SWC and place it next to that same FLA (CS3) or in publish setting specify the SWC as an external library (CS4). Now, go back to your class package. Save a backup copy first, then in the package folder, erase all of the classes you’d like to EXCLUDE from the SWC library.

3 – Generate another SWC (give it a different name). If you did this right, this new SWC will only contain the classes you have left in the package folder. Since the Flash compiler prioritizes classpaths over SWC files, the compiler found the source for those files in the right place, so it placed them in the new SWC. When it got to one it couldnt find, it then searched your external library SWC’s, in this case the one holding the whole package and said ‘yes, here it is, it compiles and I’m cool with it’. Since it is an external library though, it doesn’t pull the class into the new SWC. The result? a partial package SWC and SWF library, containing only the .as files you explicitly wanted contained inside.

This leaves two open ends.

First, you must be positive that you aren’t referencing any of the classes you left out before you load in the full library for use (remember proper loader context, ApplicationDomain.currentDomain, to access those classes at runtime).

Second, what about that full library? now you have a full library that has files you’ve since duplicated in the new smaller SWC. Well, you can, if you wish, reverse the process and go back and use this new smaller SWC as an external lib and remove the classes contained inside from your package, and recompile. What you’ll wind up with is a full package split cleanly between two SWC files. Pretty nifty. It’s a fair amount of work, and it should be easier, and maybe it is – if someone has a better solution, please share!

14 thoughts on “Creating partial package SWC files

  1. Reply Andrew Nov 24, 2009 6:13 pm

    Hey Jeff, thanks for this tip. I’m using this technique for Partigen 2 to keep the size of the PEL and SWC file sizes down and it’s working great!

    Stripes out 20k for each file, and with 15-20 files, this adds ups fast.

    Saw a few other ways to do this, but the steps you laid out were easiest.

    Cheers,
    Andrew

  2. Reply Jeff dePascale Dec 1, 2009 3:11 pm

    Glad someone else got some use out of this post, good luck with the next Partigen release!

  3. Reply Simon Doury Dec 22, 2009 7:44 am

    I use this technique for my new flash project and it’s working great

    Thanks for sharing Jeff :)

  4. Pingback: How to Create SWC Actionscript Libraries

  5. Reply Nathan Mar 5, 2010 6:57 am

    Nice Tip Jeff .. Keep post cool tips like this.. ;)

    Keen to watch ur space..

  6. Reply Peter Mar 27, 2010 6:12 am

    Hi Jeff,

    Would you mind explaining in a bit more details how to create swc files from Flash CS4 packages (especially the fl.video and fl.transition, but probably the whole fl.* package) using either the Flash CS4 IDE or Flex3.

    Thanks,
    Peter

  7. Reply p48l0 Apr 13, 2010 4:35 pm

    that, or could just make a movie clip extends a class that internally links all the classes and right click and export it as an SWC… lol

  8. Reply Jeff dePascale Apr 13, 2010 4:42 pm

    @p48l0 – If it were that simple do you think I would have written a post? :) SWC files compile all dependencies as well, and unless an external resource is specified to compile against, which is exactly what the methods in this post are designed to do, all of your class dependencies will be included in your SWC. Moreover you could just as easily make a reference to a class in a doc class to do what you said, no need to do anything with lib items:

    pseudo code:

    import Foo;

    Foo;

    Thats literally all it takes – compile a SWC with that class reference and it’ll suck in all the dependencies all the way down the line by default, if thats what you were looking to do.

  9. Reply thienhaflash Mar 28, 2011 5:26 am

    I’m not sure if you’ve check the export swc plugin for flashdevelop out yet :) We can explicitly exclude classes from swc build so it’s very cool and handy.

  10. Reply Jeff dePascale Apr 12, 2011 6:34 pm

    I actually posted on the flash developr forum back when i posted this because I had found a few issues with SWC integration in FD. Good to see they have added this functionality, I’m not doing as much flash development these days so I’m not as aware of the changes coming out.

  11. Reply cyberprodigy Jan 20, 2012 9:39 am

    Super cool! (: thx

  12. Reply Amy Jun 13, 2012 11:56 am

    This is kind of an old post, so you probably already know this. I though I’d add this for people coming new to the post. If you program against interfaces, you can at least control the dependencies for your own files.

    Tweening libraries have the annoying property of doing most of their work through static methods, so they make controlling your dependencies very hard. However, you can make sure that every place you use them is behind an “Interface wall”, and only ever call the interface method without referring to any specific Class that implements the interface.

    One way to work this is to have the implementation Class be a timeline instance, where “Export on Frame 1″ is unchecked. This allows the compiler to just compile it in at the specific point where you need it, and your Document classes never know about the specific Class. All they know is their myStageInstance setter got an ISomething that they can now use, and any swf that might be loading and manipulating the swf with that Document Class does not need to have a reference to the Class that myStageInstance really is in the library, but only ISomething.

    Note that this would also work if you wanted to have an “addedToStage” handler that wanted to find any ISomethings that might have been part of a Loader you simply added to the stage.

  13. Reply Daver K Jun 21, 2012 12:24 pm

    If you want to be more conventional. Take out all imported classes that are not part of the SWC library. Use getDefinitionByName(‘some.package.to.a.class’); and wrap it in a try catch statement so you can gracefully fail and notify any outside coders what they need to be able to use the full SWC package.

Leave a Reply