This page is an archive of my old blog. Please visit DavidTucker.net for my current blog.
This site is no longer being maintained and commenting is disabled.

AIR Tip 3 – What Version is My Application

Every AIR application has an Application Descriptor file which is an XML file that defines the properties of the application. This file is where the developer defines the application ID, application version, application name, and many other properties. As of AIR Beta 3 this file is accessible from NativeApplication.nativeApplication.applicationDescriptor in Actionscript and air.NativeApplication.nativeApplication.applicationDescriptor in JavaScript.

VERSION: This tutorial is current for AIR Beta 3.

Actionscript Example

By using this information, we can grab our application descriptor file as below:

Actionscript:
  1. private var airApplicationID:String = "";
  2. private var airApplicationVersion:String = "";
  3. private var airApplicationName:String = "";
  4.  
  5. private function getApplicationInformation():void {
  6.    
  7.     // Get the Application Descriptor File
  8.     var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
  9.  
  10.     // Define the Namespace (there is only one by default in the application descriptor file)
  11.     var air:Namespace = appXML.namespaceDeclarations()[0];
  12.    
  13.     // Use E4X To Extract the Needed Information
  14.     this.airApplicationID = appXML.air::id;
  15.     this.airApplicationVersion = appXML.air::version;
  16.     this.airApplicationName = appXML.air::name;
  17.     // Retrieve any additional information from the application descriptor file
  18.    
  19. }

Code Example 1 - Actionscript Example

This example defines three variables: one for the name, application ID, and version of the application. By using E4X, you can retrieve each of these values from the application descriptor file. However, none of these values can be returned in E4X until you tell it which namespace to use. The method namespaceDeclarations returns an array of all of the namespaces in an XML object. Currently, the application descriptor file only has a single namespace. By defining the namespace in this manner (as opposed to hard coding the value) you can ensure that this method will work in future AIR releases as well (unless the API is changed).

JavaScript Example

The Javascript example follows a slightly different approach. The application descriptor file is still retrieved from the NativeApplication class, but JavaScript views the returned value as a string (and not XML). You must first create a DOM Parser object and pass it the XML. Now you can get the values from the XML.

JavaScript:
  1. var airApplication = {};
  2.            
  3. function getVersion() {
  4.                
  5.     // Get Application Descriptor File
  6.     var appXML = air.NativeApplication.nativeApplication.applicationDescriptor;
  7.     // Parse the Application Descriptor File as XML
  8.     var xmlObject = (new DOMParser()).parseFromString(appXML, "text/xml");
  9.     // Get the Needed Values from the XML
  10.     airApplication.version = xmlObject.getElementsByTagName('version')[0].firstChild.nodeValue;
  11.     airApplication.applicationID = xmlObject.getElementsByTagName('id')[0].firstChild.nodeValue;
  12.     airApplication.name = xmlObject.getElementsByTagName('name')[0].firstChild.nodeValue;
  13.                                
  14. }

Code Example 2 - JavaScript Example

The following code samples illustrate how to create an application that retrieves and displays this information. As this is purely an example (and not an actual application), the AIR files are not provided.

Flex Application
Source Code

JavaScript Application
Source Code




7 Responses to “AIR Tip 3 – What Version is My Application”

  1. Adelbert says:

    there is something new:

    var appXml:XML = Shell.shell.applicationDescriptor;

  2. David Tucker says:

    Yes, you are correct. I have not updated these tutorials for Air Beta 2 – because something else is coming soon…

  3. Al says:

    Thanks for publishing this Dave.

    My old code would have failed silently which would have been a bit embarrassing.

  4. David says:

    This is exactly what i was looking for. The way i got the version before was NativeApplication.nativeApplication.applicationDescriptor.@version. But since the version property was taken out of the application properties, and made into it’s own tag, this no longer worked from command line sdk, but still worked on flex builder, which i thought was strange. Anyways, thanks so much for publishing this. =)

  5. [...] AIR Tip 3: What Version is My Application (AIR 1.0) [...]

  6. Thanks David. this was a massive help!