Simple automated way of removing debug code / another optimization for production swfs
A way to remove debug-code from your production SWFs by using Flex metadata
Many times when pushing swfs to production we have to remove debug traces — an easier way I’ve found is to use flex metadata and ant builds to remove it automatically. The basic idea is using Ant’s RegEx capabilities to remove lines of code that aren’t necessary in the final swf, but the lines of code will still exist for developers. Where this is particularly useful is not pushing lines of code that are specific for other developers — such as: Tink’s blog about simulating Abstract Classes by throwing errors, removing trace statements, and custom toString() statements. The basic idea looks like:
public class Abstract extends EventDispatcher {
/**
* @constructor
*/
public function Abstract():void {
// test for creation of an abstract class:
// per http://www.tink.ws/blog/stricter-abstracts/
[DEBUG_START]
if (super.toString() == '[Object Abstract]') {
throw ABSTRACT_CLASS_ERROR;
}
trace(this, "created')
[DEBUG_END]
}
}
From there we can create an ant build that removes those code elements and republishs the swf, which will reduce swf size, developer-centric error checks, and reduce the memory fingerprint for classes. If you’re a stickler for performance, this is for you. This method should be 100% QA/QE safe, as it is designed only to remove code that is specific for developers. This also gives developers a way to give hints to other developers about what not to do when creating an instantiation of a class. For instance, if we have a class called LoadManager, and don’t want it’s property duration to be referenced until it has been loaded, this sort of metadata can be given to other developers without adding it to the production swf. For example:
public function get someMethod():SomeObject {
[DEBUG_START]
if (!_someobject){
throw new Error('_someobject for this class must be set before calling someMethod()');
}
trace(_someobject);
[DEBUG_END]
return _someobject;
}
All that needs to be done when pushing a code to production, is running an ant build on the project, and using ant’s regex capabilities to create a new swf. No need to go running through your projects removing all your trace statements now before you publish your swfs. I’ve included the ant build below — this particular example compiles a SWC library and then a SWF (which I’m sure many of you are doing in your builds)
<project name="Binaries" default="buildSWCLibrary">
<property name="temp.dir" location="C:\\Projects\\some_project\\temp\" />
<property name="dest.dir" location="C:\\Projects\\some_project\build\\" />
<property name="source.core" location="C:\\Projects\\some_project\\core\\" />
<property name="compiler.mxmlc" location="mxmlc.exe" />
<property name="compiler.compc" location="compc.exe" />
<target name="clean">
<delete dir="${temp.dir}" />
<delete dir="${dest.dir}" />
</target>
<!-- COPIES FILES FROM CORE SOURCE TO DESTINATION -->
<target name="copy" depends="clean">
<mkdir dir="${temp.dir}/core/" />
<copy todir="${temp.dir}/core/">
<fileset dir="${source.core}" />
</copy>
</target>
<!-- REMOVE DEBUG METADATA -->
<target name="removeDebugMetaData" depends="copy">
<replaceregexp match="\[DEBUG_START\].*?\[DEBUG_END\]" replace="" flags="gs">
<fileset dir="${temp.dir}/core/" includes="**/*.as"/>
</replaceregexp>
</target>
<!-- NOW COMPILE THE SWC, AND THEN THE SWF -->
<target name="buildSWCLibrary" depends="copy">
<exec executable="${flex.compc}" failonerror="true">
<arg line="-output ${dest.dir}\some.swc"/>
</exec>
</target>
<!-- NOW BUILD THE MAIN SWF -->
</project>


Interesting method! I usually tend to just comment out trace statement after I don’t need them anymore but your approach is more versatile.
Comment on June 4, 2007 @ 9:01 pm
In Flash CS3 Pro, there is an option says “omit trace actions” in “Publish Settings”. It’s used to remove trace info from src, too. :)
Comment on August 11, 2007 @ 6:35 am
Great idea!
I’d escape the square brackets tho…
Comment on September 14, 2007 @ 3:32 pm
[...] Daniel Hai » Simple automated way of removing debug code / another optimization for production swfs Drop trace and debug statements on compile (tags: air as3 debugging flash flex tips trace metadata ant) [...]
Pingback on May 3, 2008 @ 10:36 am
[...] Daniel Hai » Simple automated way of removing debug code / another optimization for production swfs Drop trace and debug statements on compile (tags: air as3 debugging flash flex tips trace metadata ant) [...]
Pingback on May 3, 2008 @ 10:37 am
Nice find! Was just searching for a fast way to remove trace statements from classes.
Comment on January 16, 2009 @ 11:13 am
Ummmmmmm http://livedocs.adobe.com/flex/3/html/help.html?content=rsl_07.html
Does the actionscript command line opimizer not do the same thing?
Comment on December 11, 2009 @ 2:25 pm
Tramadol no rx 180 pills….
Tramadol cod. Buy tramadol. Tramadol hcl. Tramadol. Hydrochloride tramadol. Cheapest tramadol….
Trackback on May 30, 2010 @ 9:04 pm