Visual Studio 2022 community edition preview is my preferred working IDE for my .NET Maui app. As I was following the instructions for deploying to internal testing on google play store using the following directions: Publish a .NET MAUI Android app for Google Play distribution – .NET MAUI | Microsoft Learn. I realized that I was having to manually edit the android version code in the manifest file using the properities editor on the project. What I really wanted was an automated method for updating the version number in the AndroidManifest.xml file.
To accomplish this I utilized an extension called ‘Automatic Versions 3’ the extension has a number of settings that you control updating the Assembly Version number. I set the build part of the version number to always increment. So every time I do a debug or release build the build number increases.
So that gets the assembly version number set automatically when building. In order to update the androidmanifest.xml file I use a pre-build event and execute the following PowerShell script. I’ll give credit to github copilot (or maybe it was the edge sidebar copilot that generated the code for me.
param([string]$projectPath, [string]$androidManifestPath)
$assemblyVersion = [System.Text.RegularExpressions.Regex]::Match((Get-Content $projectPath -Raw), '<AssemblyVersion>(.*?)</AssemblyVersion>').Groups[1].Value
$versionParts = $assemblyVersion.Split('.')
$versionCode = $versionParts[2]
$versionName = $assemblyVersion
$androidManifest = [xml](Get-Content $androidManifestPath)
$manifest = $androidManifest.SelectSingleNode("/manifest")
$manifest.SetAttribute("android:versionCode", $versionCode)
$manifest.SetAttribute("android:versionName", $versionName)
$androidManifest.Save($androidManifestPath)
I added the prebuild target in my project file.
<Target Name="UpdateVersionInformation" BeforeTargets="BeforeBuild">
<Exec Command="powershell -File $(ProjectDir)UpdateAndroidManifest.ps1 "$(ProjectDir)MyNextBook.csproj" "$(ProjectDir)Platforms\Android\AndroidManifest.xml"" />
</Target>
And that’s it. Use an extension to update the assembly version. A prebuild command that executes a PowerShell script, that updates the androidmanifest.xml file pulling the information from the assembly version information.
Now deploying to the google play store for internal testing. Is one step easier.