How to remove Flex libraries completely from your AS3 project
March 16th, 2009
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.
July 29th, 2009 at 11:49 pm
Any suggestions on how to use compc to embed fonts without dragging in the flex framework?
August 9th, 2009 at 1:32 am
Erik: Sorry Erik, I have no experience with that.
January 29th, 2010 at 10:22 am
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
January 29th, 2010 at 12:17 pm
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.
February 1st, 2010 at 10:36 am
[...] 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 [...]
February 1st, 2010 at 10:57 am
[...] 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 [...]
April 18th, 2010 at 8:29 pm
Some notes on getting Jens’ suggestion to work with flex4: http://tech.groups.yahoo.com/group/flexcoders/message/148762
April 18th, 2010 at 10:45 pm
http://stackoverflow.com/questions/2664947/how-to-embed-pure-as3-bitmap-assets-with-flex4-worked-with-flex3