Ramblings of an Idle Mind: Frolic through the playground of my mind

Introduction to Manual Compiling with Flex SDK

So, many of you have sent me emails or asked me in person about how, exactly, to get started playing with the Flex SDK without having to pour any money into unnecessary resources or IDEs. First, let me say that purchasing and using Flex Builder will be one of the best investments you can make in your Flex development learning, but yes, you can indeed download the SDK free of charge and manually compile AS3 and AIR applications. This will be a very simple post dealing with just how this can be done. While there are many, many, many details we could approach, I want to tackle this issue in its simplest form and hopefully get people on the ground running within minutes of reading this post. Please note that this will be written for Windows users, though I highly recommend people set up a Linux environment and play with the command line tools there as well, if possible.

Download and Set Up the SDK

Perhaps the most obvious step for getting started using the SDK is to obtain your copy of it. The newest version is always available from Adobe’s website, and I recommend downloading it directly from them. In our case, we’re only interested in downloading the SDK itself, so be sure to follow the links for Flex SDK rather than those pointing to the Flex Builder 3 or other products. Keep in mind that, if you have already previously installed Flex Builder 3, the SDK will already be present on your system, and you will want to locate it and move on to the next step.

Once the SDK has completed its download, simply unzip the archive to a location on disk of your choosing. Be sure to choose a path that you can easily remember and access later, as we will be working in this directory for any manual compiles. In my case, I unzipped to the following location:

C:\Flex\SDK\3.5\

For simplicity’s sake, I will be referencing only a single file in the bin/ directory within your newly created folder. There are dozens of files and approaches we could take with this, but again, we are only looking for simplicity and speed to get moving.

Familiarize Yourself with mxmlc.exe

Yes, this is the one file with which we will be spending our time in this crash course. In my case, the full path to this file is as follows:

C:\Flex\SDK\3.5\bin\mxmlc.exe

This is the single file that executes our compile for us. There are only two things that we care about for raw compiling: input and output files. The easiest way to run a test compile is to open an explorer window to your bin/ directory for the Flex SDK and simply drag an AS3 (or Flex MXML) application file onto this mxmlc.exe file and release. In doing so, the command window will appear and you will see some SDK messages appear briefly as the compile is executed, and the command window will then close. If you have provided an application file with no errors (one of which I will provide in a moment), the default action is to dump the resulting SWF file into the same directory from which your AS3 or MXML file was provided. That’s all there is to it! You have just successfully compiled your first SWF file without spending a dime!

Those of you who may not have followed my posts and are unfamiliar with what is necessary for an AS3 only application, examine the following code. As a standalone AS3 file, this can be compiled and run as its own SWF – generating a 300 x 300 pixel stage with a red circle moving up and down on it:

package {
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.*;

	[SWF(width="300", height="300", frameRate="30", backgroundColor="#ffffff")]
	public class SDKTest extends Sprite
	{
		public var velocity :Number = 3;
		public var radius   :Number = 10;
		public var s_height :int = 300;
		public var ball     :Sprite;

		public function SDKTest()
		{
			this.setupAndRun();
		}

		public function setupAndRun() :void
		{
			this.ball = new Sprite();
			this.addChild(this.ball);
			this.ball.x = 150;
			this.ball.y = 150;

			var g :Graphics = this.ball.graphics;
			g.lineStyle(1, 0x000000);
			g.beginFill(0xcc0000, 1);
			g.drawCircle(0, 0, this.radius);

			this.addEventListener(Event.ENTER_FRAME, moveBall);
		}

		private function moveBall(e :Event) :void
		{
			if (this.ball.y <= (this.radius * 2))
			{
				this.ball.y = this.radius * 2;
				this.velocity = 3;
			}
			else if (this.ball.y >= (this.s_height - (this.radius * 2)))
			{
				this.ball.y = this.s_height - (this.radius * 2);
				this.velocity = -3;
			}

			this.ball.y += this.velocity;
		}
	}
}

Just copy and paste the code above into a file named SDKTest.as, then drag it onto the mxmlc.exe executable, and you will have an SDKTest.swf that you will be able to open in your browser or Flash player.

Compilation Options

You will notice that using all the default settings of the compiler are somewhat limiting, though not altogether un-useful. However, there are times we may wish to stick in some configuration options that allow us to customize the output filename, location or any other number of compile options. While I am not going to get into the usage of these options in this post (since every single implementation may have slightly different needs), I did want to give you some direction on where to look for customizing your own SDK configuration.

If you open up a command window in Windows and navigate to your Flex SDK bin, you will be able to run the compiler manually – in my case, I can get there by simply:

cd C:\Flex\SDK\3.5\bin

Now, by running the compiler script and requesting the help menu, we can get a full (and quite full it is) list of optional parameters that can be passed to the compiler. The following command will give you this list:

mxmlc.exe -help list advanced details

If you have a specific environment or two (or more) that you wish to have preconfigured to compile your SWFs, you can easily create batch files that set up the environment options and call the compiler for you. By setting up batch files that take only the files to be compiled, you can predetermine the exact result set of your compiled applications and have a flexible environment in which to work.

I hope this is of some help to someone out there. If you find this overview useful in any way, drop me a line and let me know how it helped. Good luck in your endeavors!

You can follow any responses to this entry through the RSS 2.0 feed.