case
7Jul/096

How to identify at runtime if the swf is in debug or release mode/build

It may be interesting to know at runtime whether an application is running from a debug or release SWF file. It allows the application to automatically execute debug specific code when the file is a debug SWF file and ignore this code when it is published as a release file. Also, it should be nice to know if you have a debug flash player just for the same reasons. But for the last one, there is a function in the Capabilities class called isDebugger.

One solution would be to use conditional compilation. The other solution is in the following code (hack).

package org.adm.runtime
{
	import flash.system.Capabilities;
 
	public class ModeCheck
	{
		/**
		 * Returns true if the user is running the app on a Debug Flash Player.
		 * Uses the Capabilities class
		 **/
		public static function isDebugPlayer() : Boolean
		{
			return Capabilities.isDebugger;
		}
 
		/**
		 * Returns true if the swf is built in debug mode
		 **/
		public static function isDebugBuild() : Boolean
		{
                        var st:String = new Error().getStackTrace();
			return (st && st.search(/:[0-9]+]$/m) > -1);
		}
 
		/**
		 * Returns true if the swf is built in release mode
		 **/
		public static function isReleaseBuild() : Boolean
		{
			return !isDebugBuild();
		}
	}
}

This code simply searches for line numbers of errors in StackTrace result. Only StackTrace result of a debug SWF file contains line numbers. Then we know that we are running a debug or release SWF file.

This class is also available in the Components Pack at Runtime subpackage

Comments (6) Trackbacks (2)
  1. isDebugBuild() will work only for debug flash player. The release version of the player returns null for getStackTrace().

  2. Calling:

    isDebugSWF = new Error().getStackTrace().search(/:[0-9]+]$/m) > -1;

    can prevent your swf from loading in the release version of the Flash Player. If you need to use it call it after the application has loaded and not in the constructor of your class.

  3. thanks… i`m a clumsy… i always have errors in my code
    Flash XML recently posted..Free Live Session on Multiscreen Content Development with Flash CS5My Profile

  4. I heard there was another way but there is great chance who commented that was goofing around. Still I have some errors during compilation but it might be some other mistakes of my code.
    Daniel recently posted..Wool socksMy Profile

  5. i hadnt read the comments so i had a bug that happened only in the release version… here is the fixed code for the function (still, thanks for the original idea)

    public static function isDebugBuild():Boolean {
    var st:String = new Error().getStackTrace();

    return (st && st.search(/:[0-9]+]$/m) > -1);
    }


Leave a comment


CommentLuv badge

*