Update: There’s some indication this method no longer works in Flex 4, be careful.

If you use mxmlc and the [Embed] tag you may be aware that you’ve just involved Flex in your project since [Embed] is actually a Flex concept.

Using the “-link-report” option in mxmlc you can see what classes have been compiled into your swf and how much space they use. I found this entry like this in my link report for an [Embed]‘d image.

<script name="com/brokenfunction/project/SomeClass_EmbeddedImage.as" mod="1236990005000" size="684" optimizedsize="343">
   <def id="com.brokenfunction.project:SomeClass_EmbeddedImage" />
   <pre id="mx.core:BitmapAsset" />
   <dep id="AS3" />
</script>

See the “mx.core:BitmapAsset” requirement? Since the [Embed]‘d asset ends up as a class which extends BitmapAsset it is used even if you don’t use it. I generally just use it as a Bitmap, rather than a BitmapAsset, so it’s largely unnecessary for my needs.

Now, this is all fine, this is how mxmlc does embedding, so it’s necessary. But checking the documentation for BitmapAsset and looking at the link report, we see it’s actually bringing in a bunch of extra classes and interfaces.

<script name="/home/max/bin/flex3/frameworks/libs/framework.swc(mx/core/BitmapAsset)" mod="1224183575340" size="1300" optimizedsize="678">
   <def id="mx.core:BitmapAsset" />
   <pre id="mx.core:IFlexAsset" />
   <pre id="mx.core:FlexBitmap" />
   <pre id="mx.core:IFlexDisplayObject" />
   <dep id="flash.display:BitmapData" />
   <dep id="AS3" />
   <dep id="mx.core:mx_internal" />
</script>

All the “mx” classes are actually compiled into the swf. Now Flex isn’t really bogging down the swf, all in all I found these classes to be around 2k-3k when compressed, but they annoyed me anyway while trying to shrink a preloader for a project. After a lot of Googling I found a solution, but I decided to write a more complete solution here.

You can replace the mx.core.*Asset classes with your own, which replace the originals and remove those unnecessary dependencies. Something like this:

package mx.core {
   import flash.display.Bitmap;
   public class BitmapAsset extends Bitmap {}
}

You can download a collection of these I’ve made here.

I recommend adding them to a new AS3 library, rather than mixing it with any major ones you use, then adding it as another source path for a project.

These could interfere with how classes interact in AS3 if you mix classes using this library and classes that use Flex. Since saving that extra 2k is unnecessary for Flex projects anyway, it’s probably a good idea to use it for AS3-only projects that need to save space.

10 Responses to “How to remove Flex libraries completely from your AS3 project”

  1. Erik Says:

    Any suggestions on how to use compc to embed fonts without dragging in the flex framework?

  2. Max Says:

    Erik: Sorry Erik, I have no experience with that.

  3. Jens Struwe Says:

    You just might add a compiler option wich loads an external file with exclude definitions. Works well.

    Compiler command:

    Execute command: compc -is P:/sibirjak/asdpc/src -load-externs P:/sibirjak/asdpc/build/exclude_sources.xml -o P:/sibirjak/asdpc/bin/asdpc.swc

    exclude_sources.xml:

    <report>
    <external-defs>
    <ext id="mx.core:BitmapAsset" />
    <ext id="mx.core:IFlexDisplayObject" />
    <ext id="mx.core:IFlexAsset" />
    <ext id="mx.core:IRepeaterClient" />
    <ext id="mx.core:FlexBitmap" />
    <ext id="mx.core:mx_internal" />
    <ext id="mx.utils:NameUtil" />
    </external-defs>
    </report>

    EDIT: Fixed that for you. Thanks for the tip Jens. -Max

  4. Max Says:

    Jens, I’m not sure what purpose that is for since excluding those classes could lead to runtime errors. In one of my builds (just mxmlc, no swc files) it caused these errors unless I removed the BitmapAsset exclusion (which is still needed) and used my fake *Asset classes. But even then there were no differences in file size or the classes used.

  5. NAS.C.O.D.E » Blog Archive » Embedding Asset at Compile Time in Pure AS3 Project Says:

    [...] to see by yourself just how many class definition included when adding Flex Classes. See this useful blog to know more about this. Senocular has tutorial on mxmlc, if you wish to know what options mxmlc [...]

  6. Embedding Asset at Compile Time in Pure AS3 Project - Nightspade Blog Says:

    [...] to see by yourself just how many class definition included when adding Flex Classes. See this useful blog to know more about this. Senocular has tutorial on mxmlc, if you wish to know what options mxmlc [...]

  7. Erik Says:

    Some notes on getting Jens’ suggestion to work with flex4: http://tech.groups.yahoo.com/group/flexcoders/message/148762

  8. Erik Says:

    http://stackoverflow.com/questions/2664947/how-to-embed-pure-as3-bitmap-assets-with-flex4-worked-with-flex3

  9. Andrew Says:

    This article is a timer saver. Thanks a lot!

  10. phillip louderback Says:

    “Error : The definition of base class BitmapAsset was not found” googling was getting me nowhere and stumbled upon this site for an excellent solution for as3 air app. thanks!

Leave a Reply