At the moment I’m developing a series of games with Actionscript and compiling them for the Adobe AIR runtime. Because I tend to forget updating that version number by hand, I started looking for a simple, automated solution. Since my entire build process was already done by Ant, I looked for a solution within Ant. And in the end it seemed to be pretty simple, making it a very likable solution.
Ant natively comes with a built-in task for buildnumber management called, what else could it be, buildNumber. Every time you call this task, it updates the value in an automatically generated textfile by 1. After that, you can easily access the value of that variable by using ${build.number} in your Ant file.
Because I’m building an AIR application, I need to inject the version number into a so-called ‘application descriptor’, which is an XML file. I do this by making a copy of a template XML-file of the descriptor and injecting the buildnumber into that copy.
So here is the Ant script doing this magic:
| 01 | <?xml version="1.0" encoding="UTF-8"?> |
| 02 | <project default="updateBuildNumber" name="Build Game"> |
| 03 | |
| 04 | <!-- properties --> |
| 05 | <property name="app_descriptor" value="../air-assets/game-app.xml" /> |
| 06 | <property name="app_descriptor_template" value="../air-assets/game-template-app.xml" /> |
| 07 | <property name="mainversion.num" value="0"/> |
| 08 | |
| 09 | <!-- adds build number management to the air application descriptor --> |
| 10 | <target name="updateBuildNumber"> |
| 11 | <!-- increment the build number, if there is no file tracking the build number, |
| 12 | this creates it and sets the build number to 0 --> |
| 13 | <buildnumber file="mybuild.number" /> |
| 14 | <echo>${build.number}</echo> |
| 15 | |
| 16 | <!-- make a copy of the application descriptor template file --> |
| 17 | <copy file="${app_descriptor_template}" |
| 18 | tofile="${app_descriptor}" |
| 19 | overwrite="true" /> |
| 20 | |
| 21 | <!-- the file that we just copied contains a placeholder for the version number. |
| 22 | Replace that placeholder with the version number. --> |
| 23 | <replace file="${app_descriptor}" |
| 24 | token="@@@ANT_BUILD_NUMBER@@@" |
| 25 | value="${mainversion.num}.0.${build.number}" /> |
| 26 | </target> |
| 27 | </project> |
The XML file in which the version/build number is set looks something like this:
| 01 | <?xml version="1.0" encoding="utf-8" ?> |
| 02 | |
| 03 | <application xmlns="http://ns.adobe.com/air/application/2.0"> |
| 04 | <id>game</id> |
| 05 | <version>@@@ANT_BUILD_NUMBER@@@</version> |
| 06 | <filename>game app</filename> |
| 07 | <name>game app</name> |
| 08 | ....etc.... |
| 09 | </application> |
| 10 | |
I added the buildnumber to my UI, so I could always see the version of the game:
| 1 | var descriptor : XML = NativeApplication.nativeApplication.applicationDescriptor; |
| 2 | var ns : Namespace = descriptor.namespaceDeclarations()[0] as Namespace; |
| 3 | var version : String = descriptor.ns::version; |
| 4 | var tf : TextField = new TextField(); |
| 5 | tf.text = version; |
| 6 | addChild(tf); |
Well, that’s all there is to it. You can also download this ANT build-file with the AIR application-descriptors to take a closer look at how this works and try it out yourself.