Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Automatic Version Increment for Windows Phone Apps

DZone's Guide to

Automatic Version Increment for Windows Phone Apps

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Version increment for Windows Phone projects currently has to be done manually, as developers are updating the version attribute in the WMAppManifest.xml file. Whenever I install the application I am currently working on, I am always wondering whether the Windows Phone device has the most recent build available. With a static version, this becomes a challenge because often changes are not visual, but are done in the background.

As there are no built-in version increment capabilities, I developed a very simple console application that is ran before the building process, modifying the current value in the WMAppManifest.xml file. Here is the source - it is as simple as it can get:

using System;
using System.Linq;
using System.Xml;

namespace WPVersionIncrementer
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Count() == 5)
            {
                // Separate all parameters. If there aren't five of them, ignore execution.
                string path = args[0];
                string incrementMajor = args[1];
                string incrementMiddle = args[2];
                string incrementMinor = args[3];
                string incrementBuild = args[4];

                XmlDocument document = new XmlDocument();
                document.Load(path);

                XmlNode node = document.DocumentElement;
                XmlNode appNode = node["App"];

                string rawVersion = appNode.Attributes["Version"].Value.ToString();
                string[] components = rawVersion.Split(new char[] { '.' });

                // Get integer values for all four version components for easy manipulation
                // read: increment
                int major,middle,minor,build;

                int.TryParse(components[0], out major);
                int.TryParse(components[1], out middle);
                int.TryParse(components[2], out minor);
                int.TryParse(components[3], out build);

                if (incrementMajor.ToLower() == "-i")
                {
                    major++;
                }

                if (incrementMiddle.ToLower() == "-i")
                {
                    middle++;
                }

                if (incrementMinor.ToLower() == "-i")
                {
                    minor++;
                }

                if (incrementBuild.ToLower() == "-i")
                {
                    build++;
                }

                // Compose the new version in a unified string and save it. The order of all components
                // is preserved.
                string compositeNewVersion = string.Format("{0}.{1}.{2}.{3}", major, middle, minor, build);
                appNode.Attributes["Version"].Value = compositeNewVersion;
                document.Save(path);

                Console.ReadLine();
            }
        }
    }
}

To actually perform the increment, right-click on the Windows Phone project in the Solution Explorer and select Properties. Select Build Events and use this for the Post-build event command line:

"C:\Users\Dennis\Documents\Visual Studio 2010\Projects\WPVersionIncrementer\WPVersionIncrementer\bin\Release\WPVersionIncrementer.exe" "$(ProjectDir)Properties\WMAppManifest.xml" -n -n -n -i

There are several things I have to mention here:

  • The path may vary - depending where WPVersionIncrementer is located, you might want to change the location in the command above. 
  • Use quotes for the path - if you know that the executable location contains folder names composed of two or more words separated by a space, you need the quotes for the path to be correctly recognized.
  • Use 5 parameters - even if you need to only increment the build number, indicate that you do not need to increment other indicators. For example, you can use a command like this: WPVersionIncrementer PATH_TO_XML -n -n -i -i Only the -i parameter is recognizes as a flag to increment the value. The parameters are structured in order of the version string: Major, Middle, Minor, Build.
  • Use the Properties folder to modify WMAppManifest.xml and NOT Debug or Release, where it is also found, but in fact - copied at build time from Properties.

After the command is executed and you return to Visual Studio, you will be notified that one file in the project was modified and needs to be reloaded:

Now that WMAppManifest has an updated version, you can always read it internally and display in the About screen or wherever you want in your application.

Recommended reading:  Read Local XML File in Windows Phone 7

You can download the experimental source code here.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}