James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code

Share


Tags


James Montemagno

PSA: Android Pie Reports "9" for Release Version instead of "9.0"

Let's break down a version number together, or as standard documentation says, it "represents the version number of an assembly, operating system, or the common language runtime" and that "the format of the version number is as follows (optional components are shown in square brackets ([ and ]):

major.minor[.build[.revision]] 

In the world of .NET this has always been a staple of how we version anything. Major and Minor are required and the other bits are optional, which makes it very easy to parse a string of "9.0" into Major = 9 and Minor = 0. That is why this morning it caught me by surprise when my Android Pixel 2 XML that is running Android Pie reported back 0.0 to me.

I was puzzled because every other version of Android, iOS, and Windows has always reported back the correct number. Take for example this code to parse the Android version number.:

static string VersionString => Build.VERSION.Release;
static string Version => ParseVersion(VersionString);

internal static Version ParseVersion(string version)
{
    if (Version.TryParse(version, out var number))
        return number;

    return new Version(0, 0);
}

My phone should be running Android 9.0 Pie... so it should return "9.0" just like Android 8.0 Oreo reported "8.0". I mean Google wouldn't change their versioning 10 years later? Oh wait they did! Build.VERSION.Release actually reports "9"... which fails parsing!

Google tried to tell us this if you subtly look at the documentation:

versions

So, what to do? Well I do think that Version.TryParse should be smart enough to figure it out, but it isn't. This means that we have to try to parse the version, then parse just an int, and use 0 for the minor version:

static string VersionString => Build.VERSION.Release;
static string Version => ParseVersion(VersionString);

internal static Version ParseVersion(string version)
{
    if (Version.TryParse(version, out var number))
        return number;

    if (int.TryParse(version, out var major))
        return new Version(major, 0);

    return new Version(0, 0);
}

Now off to update Xamarin.Essentials and my DeviceInfo plugin!

Copyright © James Montemagno 2018 All rights reserved. Privacy Policy

View Comments