Flex Builder Conditional Compilation
April 3, 2009

In Flex, there’s a define directive, which is useful to enable/disable certain sections of your code.  The syntax is like this:


It is similar to C’s #if #endif and #ifdef #endif feature.

You can, for example, designate sections of code to be only compiled on Debug build.  Or to disable sections of code for Limited or Trial versions.

Here’s an example of using Conditional Compilation for an Action Script Project.  This example is using Flex Builder 3.  If you use Flash CS4, then the dialog is accessible in the IDE, as explained here:

Example 1

In this example, let’s suppose I have a block of code that I want to only compile in the debug version.  Here’s a step by step example:

    • Create a new project (File -> New ->ActionScript Project).
    • Open the Project Settings window.
    • Goto ActionScript Compiler and add the define statement.  This is the flag that says whether I want to build the debug section.  In this example, I am using the namespace CONFIG (any name will do) with identifier name debugBuild (any valid variable name can be used, but pick something meaningful).  I decided to check this variable and compile some debugging code, only when debugBuild is set it to true.


  • In the code, add the following:
trace("debugBuild configuration");

(Remember, this example is for ActionScript type project.  Flex and MXML projects have similar syntax although you can only use the conditional inside action script code-block). Run it and you should see that the trace outputs: “debugBuild configuration” (use Flash Tracer to see traces).

Now change the Additional compiler arguments to


Recompile, and you would no longer see the trace.  You can use this technique to add code during development which you can easily turn-off on release build.

Example 2:

Slightly different example where the identifier acts as a constant.  In this example, I have a different code block for a debugBuild and non debugBuild, which can be switched by changing debugBuild identifier value to false.

Suppose I set it to true (eq: -define=CONFIG::debugBuild,true), then this code:

if (CONFIG::debugBuild)
trace("debugBuild configuration");
if (!CONFIG::debugBuild)
trace("NON debugBuild configuration");

Will produce these lines:

debugBuild configuration

Of course you can always define a constant in the code.  The benefit of this is that it needs only be defined once and it is recognized by all classes in your project.  The disadvantage is, you will have to use Flex Builder to compile your code because the Flash IDE does not recognize these conditionals.

Note that any conditional compilation identifier that you use must be defined.  If you did not specify the identifier in the Additional Compiler Argument example, then you will get an error that says something like: “1120: Access of undefined property debugBuild.”   I have not found a good way to easily overcome this and unfortunately, Flex Builder lacks a common feature that allows for different build-configurations within the same project (something that IDEs like Visual Studio have has for a long time), so unless you like to keep changing the flag, then another option is to create another project for the release configuration.

This example also shows how you can treat the variable as if it is a variable that exists in the name space CONFIG.

Example 3:

Here you can see how to specify more than one identifiers.  When you have more than one identifiers, type them on a single line on the Additional Compiler Arguments, separated by dash.  Note also the use of single-quote on string values.

-define=CONFIG::debugBuild,true -define=CUSTOM_PARAM::myName,'permadi' -define=CUSTOM_PARAM::company,'someCompany'

With the above applied, then this code …

trace("My name is "+CUSTOM_PARAM::myName);
trace("My company is "+CUSTOM_PARAM::myCompany);

Will produce…


My name is permadi

My company is someCompany

Example 4:

For software versioning,  blocks of code can be turned on or off by examining the value of an identifier that you define in the Additional Compiler Arguments.  For example, below I have some features that should only be on version 3 or newer.  Then I can define an identifier, that I call version (just for the sake of this example, the name is arbitrary).  Here you see how the value of the identifier can be used with conditional operators (< > ==).


Then use it in the code, like this:

if (CUSTOM_PARAM::version>=3)
trace("put code for version 3 or newer in this code block");
// put older version of code here

If I want to compile for older version with the same code base, I can change the identifier value to, say 2, for version 2.


There are more information on the Adobe site.
Also see: Accessing conditional compilation dialog in Flash CS4: