3

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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
raja
  • 57
  • 9

2 Answers2

1

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.

divillysausages
  • 7,883
  • 3
  • 25
  • 39
  • NOTE: from what it seems like you're trying to do, it's probably best to include your source as an external source dir rather than an swc. That way, it's the consts defined in your final project that determine the code that's added or removed – divillysausages Jun 12 '13 at 12:41
0

Quoting the docs:

The mxmlc compiler lets you pass the values of constants to the application at compile time

It does not mention compc, so I'm pretty sure it is not possible. I also wonder how that would work technically since you include/exclude parts of the code during compilation. The compiled SWC would no longer contain the conditional code.

Christophe Herreman
  • 15,895
  • 9
  • 58
  • 86
  • You didn't source your quote. But, "The component compiler can take most of the application compiler options" http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7a80.html . Why wouldn't the component compiler support conditional compilation? [I never tried it myself, though] – JeffryHouser Jun 12 '13 at 12:02
  • Thanks, I added the sources. The conditional compilation documentation is explicitly under "Using mxmlc, the application compiler". Also as I mentioned in my answer, I don't see how this would work technically. – Christophe Herreman Jun 12 '13 at 12:37
  • The SWC still gets compiled. I once spoke to a component vendor who was planning to use conditional compilation as part of a watermarking scheme on their SWCs; so they could distribute 'developer editions' like Flextras used to. But, honestly I have never tried it myself. IT sounds like @divillysausages has a working example... – JeffryHouser Jun 12 '13 at 12:51
  • Yes, I see how this is possible. I was confused in the sense that it would not be possible to use the SWC with conditional compilation on the application since the conditions might not match. Compiling from the sources would be the only option then. – Christophe Herreman Jun 12 '13 at 13:15