[ˈfɔːwədˌθɪŋkɪŋ]

Invoking the Flex Compiler from Adobe AIR… with Merapi and the Flex Compiler API

As the first step in a larger effort trying to automate my Flex Application development, I have been creating a simple Adobe AIR application that through the AIR bridge from the Merapi Project invoke the Flex compiler.
The implementation is only intended as the first experiment, so currently the use case for the App is a bit farfetched… however, the offspring of this experiment and the potential implications are very strong in terms of workflow optimizations.

Anyways, lets get on with it…
I have to warn you in advance, this is not the simplest possible implementation, but originally it was not my intention to post about it. A better sample would have excluded all the didn’t relate to the central elements which in this case is the Flex Compiler API and getting in touch with it through Merapi. Anyways… after this disclaimer… let’s get on with it…

There are a number of prerequisites in order to get started.

First you need to download and familiarize yourself with the Merapi Project.

Then you need to get the Flex Compiler. The easiest way is to grab it from the Flex SDK which was installed alongside Flex Builder / Flash Builder.

You can find the JAR file in following location:
{Flash Builder Installation Directory}/sdks/{SDK Version}/lib/flex-compiler-oem.jar

Setting up the appropriate Java project is an exercise I will leave for you to accomplish on your own as out of scope of this post to go through that.

After setting up the project, the first we need to create the Java application which will be running in the background and listening for messages from Adobe AIR.
This application will instantiate the Merapi AIR bridge and through this listen for commands in the form of Compile message types.

The message which will be using to pass through the bridge is called CompileMessage and inherits from Merapi’s message class and we are using the Application class from the Compiler API to actually do the building of the SWF from the MXML file.

Seeing that I copied most of the Java code from the HelloWorld sample of the Merapi project, the calling back to the Adobe AIR application is implemented in the ProgressMeter seeing that this was a very easy and convenient way of getting the callback to function. It should not be regarded as a recommended way of doing it, this is merely an experiment and should not be taken on account for recommended ways of implementing callbacks.

One of the key elements in the establishing the bridge between Adobe AIR and Java is the message handler invoked by the Java application.
This eventually registers a message type and will assume the role of the observer in the messaging flow.

These 4 classes are all the constitute the Java application based on the existing Flex Compiler API and the classes from the Merapi Project.
Now in the same order, I will go through the Flex part of the application.

First we have the application itself, in this case it is the container of the form and the Mate EventMap and to spice thinks up, a PreferenceMap which wraps the logic relating to managing user preferences.

<s>
</s>

The message which corresponds to the message in the Java application looks something like this.

For your reference, I will just be adding the EventMap and the PreferenceMap as well…

First we have the EventMap which is a core element of Mate based applications, again… this as well is outside the scope of this post to discuss further.

This is the preferencemap, again it might be total overkill to put in a sample such as this… but seeing that it is a basic part of any application I do these days, I saw no reason to exempt it. Remembering the user’s preferences is paramount when creating user experiences which seeks to satisfy the increasingly high expectations of users today.

This is far from a complete or perhaps even suitable sample to illustrate the interoperation with both Merapi and the Flex Compiler API, but I decided to put it out here as part of a collaborative effort of getting my hands dirty with extending Flash Builder and Flash Catalyst.
If you have any questions regarding this sample, please don’t hesitate to contact me or comment on the post.

I will soon make the source code available under the “unsponsored” framework.

Well, now with the AIR 2.0 NativeAPI, there is hardly any need for Merapi anymore, as you can do everything you can with Merapi directly from AIR’s own API’s.
I therefore suggest that you check out AIR 2.0.http://labs.adobe.com/technologies/air2/

Peter,
Nice Blog, but i have some issues here, after creating the compleate project using merapi and Air , i want to give it to my customer/end user, then in which format i need to give him , if only Air i can give him as Air executable and if java i can give him as executable jar , But if i give him both of them individually then he will not be happy im sure about that.
Is there any way i can give him as a single executalble ?

You should definitely check out AIR 2.0 !
With this latest release of AIR support for native applications is included in the SDK and has build in support for installations of the native apps as well.
In addition, Serge Jespers did a killer app which makes it even easier to package your installers: Package Assistent

I read your articles it is very informatics and knowledgeable.Its worthy as well as very important information. Doing great job to sharing it.
Invoking the Flex Compiler from Adobe AIR… with Merapi and the Flex Compiler API Peter Moelgaard's Blog

This approach I have described in this article is very old and already massively deprecated in favor of better ways to do it.
I suggest you head over to the Adobe DevNet and check out some of the latest announcements and releases there, they should help you along.