Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing code between Flex and AIR

As you know, we could build a RIA application based on flex. Also, we could build an desktop application based on AIR. I have a question, If we want to build web & desktop application simultaneously. Could we use the same codes to ship our production to web & desktop?

like image 513
Joseph Avatar asked Jun 04 '26 20:06

Joseph


2 Answers

If you design your application for it, you should have no problems in sharing 99% of your codebase between your Flex and AIR builds.

  • You will need a separate application MXML for the Flex / AIR versions as AIR uses a WindowedApplication and Flex uses Application

  • You will need to abstract your usage of any AIR-only APIs. That is, any class, property or method marked with the AIR-only icon ( ) in the Online Documentation. You might find this process easier if you are using a Dependency Injection container like Swift Suspenders.

  • Alternatively, you can split your service definitions into two different source trees. This would result in your AIR project and Flex project sharing one source path, but also having their own source path. This way, code that accesses com.application.MyService would be shared across AIR and Flex but the implementation of com.application.MyService would differ depending on which 'service source path' was being used.

  • You may find it useful to configure each build with a compiler flag like -define+=CONFIG::AIR. This allows you to use conditional compilation so that you can compile the same file for both builds, but include specific code for the AIR build.

Here is an exmaple of conditional compilation:

public function getMyService() : IMyService
{
    CONIFG::AIR
    {
        return new MyServiceThatUsesAnAIROnlyAPI();
    }

    return new FallbackServiceForFlex();
}

Unfortunately there is no way to 'negate' a conditional flag (ie. !CONFIG::AIR) so you either need to be smart about your usage of it, or include two flags (CONFIG::AIR and CONFIG::FLEX)

like image 94
Richard Szalay Avatar answered Jun 07 '26 22:06

Richard Szalay


I'm surprised no one said it yet, but this is how I would do it:

  1. Create a library project. this project will include all your shared code.
  2. Create a Flex project for web deployment
  3. Create an AIR Project for AIR deployment

Both the Flex and AIR projects can reference and use code in the library project. The AIR project can use AIR specific functionality without affecting the web project.

If you need to perform different actions differently based on whether using the web project or the AIR project, you can create interfaces in the library project and implement them in the main project to use the respective APIs.

like image 21
JeffryHouser Avatar answered Jun 07 '26 22:06

JeffryHouser