Search code examples
apache-flexwebactionscriptswcconditional-compilation

Flex conditional compilation-Is it possible to have conditional compilation in flex library project?


Using Flex, created a desktop and web application in that used conditional compilation. It runs successfully. Now, I Would like to have the single swc file for both Desktop and web. So created the library project for satisfying that condition. While using conditional compilation in flex library project getting many issues like conflicts variable name and duplicate functions and so on, which I haven't faced while using flex projects without swc file.

So the question arises now: Is it possible to have conditional compilation on a flex library project?


Solution

  • When compiling your SWC, you can specify compile constants by passing them in a -define argument. This will however, only include whatever code you've added using that constant - i.e. you can't reset the const in a project that includes the SWC to get a different result.

    Below is the code for a bat file for creating an SWC. Copy it into a new file, and save it with the extension .bat. Replace the file paths as necessary.

    @echo off
    
    set flexroot=D:\Program Files\FlashDevelop\Tools\flexsdk\
    set proj=D:\Dev\TestSWC\
    
    cd %flexroot%bin\
    compc.exe -source-path %proj%src -is %proj%src -optimize -define CONFIG::debug false -define CONFIG::release true -output %proj%bin\TestSWC.swc
    
    pause
    

    I used this to build a SWC file containing a single class, like so:

    package  
    {
        public class TestClass
        {
    
            public function sayHello():void 
            {
                CONFIG::debug
                {
                    trace( "Hello debug" );
                }
                CONFIG::release
                {
                    trace( "Hello release" );
                }
            }
    
        }
    
    }
    

    I then created another project, included the SWC and set the CONFIG::debug flag to true, and called the sayHello() function. It traced "Hello release" as the SWC was compiled with the CONFIG::release flag as true.