4 Replies Latest reply: Apr 2, 2013 11:25 AM by pauland RSS

    conditional compilation and import statements

    DilipShah Community Member
      I have a web application written in Flex and I'm trying  to build an AIR application from the same code base using conditional  compilation.
      In the AIR application, I need to have import statements  such as the following:
      import flash.data.SQLConnection;
      import  flash.filesystem.File;
      but I cannot have these import statements in the web  application because they are AIR only classes.
      I thought I could use conditional compilation to  overcome this issue but then I read the following on page http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7abd.html:
      "You cannot use constant Boolean values to conditionalize metadata or import statements."
      If that's the case, how can I have common codebase for Flex based web as well as desktop applications? Has anyone solved this conundrum?
      Thanks,
      Dilip
        • 1. Re: conditional compilation and import statements
          Flex harUI Adobe Employee

          If the import is resolvable, but not used, I think you will be ok.  So

          conditionalize the use of the classes, leave the imports in and use the AIR

          config and see if it runs on the web.

          • 2. Re: conditional compilation and import statements
            DilipShah Community Member

            Even though Adobe document mentions that "You cannot use constant Boolean values to conditionalize  metadata or import statements.", I've managed to do so and it seems to  work so far. There is just one exception though. If I add such  conditions around import statements in my Cairngorm Model Locator class,  I get "Uncaught exception in compiler" or "1131 classes must not be  nested" error.

            Here is how I have it in my code:

            CONFIG::desktopMode {
              import flash.data.SQLConnection;
              import flash.events.SQLEvent;
              import  flash.events.SQLErrorEvent;
              import flash.filesystem.File;
            }

             

            It surely is a bit mysterious!

            • 3. Re: conditional compilation and import statements
              CyberProdigy

              DilipShah, as far as I see the classes are still included if referenced by import.

               

              Example:

              I have this statement in my class A

               

              if(CONFIG::platform == "ios") {

                                  import B;

              }

               

              I can see in linkreport that class B is still included and is included because A is referencing it.

               

               

              Flex harUI, classes are included into final swf file if any other class is referencing it through import even though not using it. I've experimented with it quiet a bit.

              • 4. Re: conditional compilation and import statements
                pauland Community Member

                CyberProdigy, the if statement is a run-time construct, not compile time, you can't use it for conditional assembly of the code.

                 

                DilipShah has the right idea, I think, but you'll have to use the same technique to eradicate all trace of the use of B for it not to be included.