Building Flex Applications with Gradle

In a recent post, I touched on my frustration with Maven and my interest in other build tools. Gradle looked interesting (Grovy DSL with Ant integration). I've been playing with building a Flex app, so I thought I would see if I could get Gradle to build it. Since Adobe provides custom Ant tasks as part of its SDK distribution, it should be easy. In fact, it is.

Assumptions:
  • You have downloaded the Adobe SDK (or Flex Developer)
  • You have a sample Flex (or Air) app to build.
  • You have Gradle downloaded and installed.
The first step is to create a Gradle build file (build.gradle) in your Flex application directory. To test your Gradle install, put a simple Hello World task:

task hello << {
println "Hello World"
}

You can then run gradle hello from your command prompt. It should echo back:

:hello
Hello World

Now you need to 'install' the Flex Ant Task Jar. This can be found in your Flex SDK Install under the ant directory (sdk_install/ant). There are a couple ways to install this. The easiest is to put it in the lib directory under your GRADLE_HOME directory. You can also specify the classpath in the taskdef command, but I'll leave that as an exercise for the reader...

Once you have the Flex Ant Tasks installed, you can create your Gradle build script. Here is a straight foward compile script:

ant.FLEX_HOME="c:/Program Files/Adobe/flex_sdk_3"

ant.taskdef(resource: "flexTasks.tasks")

task compile << {
ant.mxmlc(file: "src/Main.mxml") {
}
}

You will need to change the FLEX_HOME value and Main.mxml to your specific environment/app, but that is it. You can then execute gradle compile from your command prompt and compile the application.

If it is an Air app, you will need to add configname: "air" to the mxmlc task definition so it uses the Air configuration. Otherwise, you will likely see: Unable to locate specified base class 'mx.core.WindowedApplication'

To build the Air package for your application, you will also need to call the script file included with the SDK. You can find a sample of the ANT version of this here.

This example is more a sample of how easily Ant tasks can be used in Gradle than the overall power of Gradle. But it does demonstrate that Gradle can be used to build anything that has Ant tasks, and that is a great start.