0

Is it possible in any way, or, what is the best scenario to programatically update the version code and the version name in an Android Mobile app?

For a mobile app we use:

  • Xamarin for Android development
  • Visual Studio Team Services for version control (Git), building, testing and releasing.
  • Google play for distribution.

our continuous integration flow goes like:

Visual studio (xamarin) ->  
Git(Code) -> 
Build -> 
Test(Xamarin testcloud)  -> 
Release Google Play (Alpha/Beta/Production)

Currently I have to update the version code/name manually before committing to Git, otherwise, on the other end of the flow, Google will complain the version number of the newly released apk is less or equal than the apk already in the Play Store.

However, since we commit a lot and automatically build-release, we only want to increase the version-code/name only on a successful release (in case google rejects it). This caused this little dilemma: Because then the AndroidManifest, where the versioning resides is already packaged in a signed and zipped APK.

Another thing is: we are working with multiple developers on the project so the manual update thing can cause racing conditions, so the best way is (what we think) to update the version numbers just before release.

Things I thought of:

  • storing the version number somewhere in VSTS
  • using the build number as the version number
  • getting the current version number via the developer API from Google Play

But still a bit stuck and looking for a common / best practice

Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
  • Perhaps Andy's answer on this might help: http://stackoverflow.com/questions/27058172/xamarin-mobile-app-version-number-scheme-across-3-platforms specifically https://github.com/soltechinc/soltechxf/blob/master/UpdateVersionInfo/Program.cs#L68-L77 – Jon Douglas Dec 12 '16 at 20:31

1 Answers1

1

Caspar,

I took the approach of modifying the manifest before building/packaging the application, because once you start to build, package, sign, zip align, it's to late in the process.

In your build definition, as a first task, before the build task, add a task that will update the manifest:

  • In the Task catalog/Utility, add a PowerShell Script
  • Set it as Inline

Arguments:

-filePath '$(FilePath)' -oldValue '$(OldValue)' -newValue '$(NewValue)'

Script:

param ([string] $filePath, [string] $oldValue, [string] $newValue)

Write-Host "---Replacing value $oldValue with new value $newValue in file $filePath---"

$content = [System.IO.File]::ReadAllText($filePath).Replace("$oldValue",  $newValue)

[System.IO.File]::WriteAllText($filePath, $content)

$filePath parameter is the path to your manifest such as:

$(Build.SourcesDirectory)\MyAndroidApp\Properties\AndroidManifest.xml

$oldValue parameter would be the version code property:

android:versionCode="1"

$newValue parameter would be the new version code property:

android:versionCode="$(AndroidVersionCode)"

"$(AndroidVersionCode)" could be a variable in your build definition, allowed at queue time or the build number.


I gave you the simple path but for maintainability and convenience I took advantage of the VSTS Task groups. At the end my script to replace text in file is a task group and I can use it in any build definition.

You end up with something like this: Build definition

Vivien Chevallier
  • 1,492
  • 1
  • 11
  • 14
  • Hi Vivien, this looks exactly like what I spent all day yesterday on with powershell scripts. AndroidVersionCode is the key part here. Google play seems to demand that it is the next consecutive integer from the last successful release. Did you set this up in anger and use it successfully? – Davros Spignot Sep 28 '17 at 10:38
  • hmmm. found this [link](https://stackoverflow.com/questions/9629125/versioncode-vs-versionname-in-android-manifest) `An internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions. This is not the version number shown to users; that number is set by the versionName attribute. The value must be set as an integer, such as "100". You can define it however you want, as long as each successive version has a higher number` – Davros Spignot Sep 28 '17 at 10:41