« New Flex Blog by Al Manning | Main | Update on Using Flex SWCs within Flash CS3 »

Using Flex SWCs within Flash CS3?

Can you use framework-less Flex 3 SWCs within Flash CS3? I assumed that you could do this if the compatibility-version flag was used and you didn't use any of the Flex framework. I haven't been able to get this to work, however, so I'm hoping someone else has had some luck with this. I've also asked about this on flexcoders, so I'll update this post with any information I learn there.

There's little information out there on this subject. Tim Walling has a post on this which points to some problems, but there's nothing else that I've been able to google. I'm surprised that there's not more people who are stuck on this.

A colleague of mine, Crystal West, is the person who is really banging her head on this. She created an example to show the issue. The example has two SWCs within it, one created using Flash Authoring and one created using Flex 3. Try compiling swc_test.fla with just flash_bc_ads.swc in the same directory (that is, remove flex_bc_ads.swc). The fla should compile fine. Then try with just flex_bc_ads.swc. You should get a number of compile errors for the AdTranslator class.

She tried a lot of variations on compiling with flex_bc_ads.swc, and nothing has worked. flex_bc_ads.swc was built using the compatibility flag and doesn't use any of the Flex framework. She also tried compiling the Flex SWC using compute-digest=false (as recommended by the Tim Walling post mentioned above). That didn't work either, and, according to some of the comments on this blog post, didn't work for others.

Anybody have any experience here or any suggestions?

Update: A minute after I posted, Oscar told me about a Yahoo post on the subject. It doesn't seem like this is the same problem, however, since a related post says the problem is "code SWCs". So hopefully this isn't our problem, since we are trying to create a SWC which can be used as a Sprite.

Solution: There's a new post with an update on the issue.

Comments (11)

I had a similar problem in the past. I created a SWC in Flex Builder with AS3-only code (no Flex dependencies) and it refused to work in CS3.

The solution? I needed to add a Manifest file!

Go into Properties and select Flex Library Compiler, then specify a Manifest file under Compiler options.

I hope that solves your problem!

Thanks for the suggestion Nick. Unfortunately we just tried to use a manifest file, and it didn't fix it.

Owen, that's a really interesting post! I forgot to mention something very important above, which is that we don't actually want to link against the SWC. We want to use the SWC as an external library, but even that isn't working. It's good to hear that it may work at some point though. :)

Flash CS3 seems to only support SWCs that create "components", or more specifically, display objects that can be dragged onto the stage.

When you create a SWC, place it in [Flash CS3]/en/Configuration/Components and it will show up in Flash CS3's Components panel. As Nick Schneble mentioned, a manifest is required because you need to specify which classes can be instantiated as "components".

Ideally, we should be able to just drop any SWC containing any classes in the same folder as our FLA files and link them in just like raw source code for classes (like we can with the Flex compilers), but that doesn't seem to be supported. When I tested this behavior, I saw that the SWF would compile, but the compiled classes wouldn't be included in the SWF, causing VerifyErrors at runtime.

ive solved this very problem with the manifest xml file also, and from memory the swc included non display object classes.

Another vote for the manifest.xml file

Thanks all for your help. I have used manifests before, actually wrote this code within the Flex compiler. :) But there's something still missing in how we're doing things. It sounds like we might figure something out soon. I hope Crystal doesn't mind, but to quote a bit of what she just wrote to me:

"Interesting! So, I put the swc in the Components directory and AdSwf and AdTranslation show up but I can't seem to actually drag them on the stage. I think this will work, though, so I'll keep looking at it... Josh's explanation makes a lot of sense to me ..."

I looked further into this one. I think I know why Crystal can't drag them on stage. The Flex SDK and Flash CS3 use different SWC versions. Flash CS3 doesn't know how to properly parse the catalog.xml file included in the SWC.

With Flex 3.1, I made a SWC that has this information in the <versions> element:

<swc version="1.2" />
<flex version="3.1.0" build="2710" />

In the <components> section, each <component> element has a uri attribute that is only used by Flex.

Additionally, it has a <digests> element with a SHA-256 hash.

If you export a SWC from Flash, it has a slightly different format. The <versions> element has this:

<swc version="1.0" />
<flash version="9.0" build="r494" platform="WIN" />

The components section doesn't include the uri attribute, but it also has tooltip and preview attributes. There is no <digests> element at all. There is content in the <files> section if the component has a live preview SWF. This and the preview attribute are optional.

If you want to hack a Flex SWC to work, open the SWC file in your favorite ZIP file editor and extract catalog.xml from the SWC. Change the version information to match the Flash CS3 XML and remove the digests section entirely. Change the attributes in the components section too.

Based on this information, I don't think Adobe intends for Flex SWCs and Flash SWCs to be interchangeable. I'd proceed at your own risk. Consider exporting your SWCs from Flash CS3 (right click on the component in the library and choose "Export SWC..."). Seriously, to do otherwise will probably cause you major headaches, especially as new versions of the Flex SDK and the Flash authoring tool are released.

Thanks for digging into this Josh. I noticed as well that the versions were different, but I didn't notice the other differences in the catalog.xml. Unfortunately, I asked Crystal about this and she's already looked into it.

Here's another update (hope you don't mind my posting again Crystal if you are reading this!):

"I just got the swc to work by dragging it on the stage if none of our classes are *actually* compiled in. So a swc created using Flex w/ the manifest file and compute-digest=false does seem to work, just not with our classes. I decompiled the library.swf in the swc and there are several mx classes compiled in. That confused me, because we don't use any mx classes in the players, but then I realized that embedded symbols were being interpreted using flex classes (I guess during swc compilation). A bunch of icons are embedded in the theme classes and classes like FlexBitmap and BitmapAsset (there are 10 of them) are compiled into the library swc as a result. So, now I sort of think that could be one of the reasons this isn't working for us."


Regarding SWC compatibility, Peter Watson told me that using the compiler option "compute-digest=false" for the SWC being compiled from within Flex Builder would do the trick. So that, in combination with a manifest file allowed us to compile a SWC which could be used in the Flash IDE.