Click here to Skip to main content
15,917,964 members
Articles / Mobile Apps / Android
Technical Blog

.NET MAUI Automated version numbers for AndroidManifest.xml

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
22 May 2024CPOL1 min read 1.7K   2   1
In this article I make an automated method for updating the version number in the AndroidManifest.xml file.

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.

Image 1

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.

Image 2

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.

Shell
param([string]$projectPath, [string]$androidManifestPath)

# Extract AssemblyVersion from .csproj file
$assemblyVersion = [System.Text.RegularExpressions.Regex]::Match((Get-Content $projectPath -Raw), '<AssemblyVersion>(.*?)</AssemblyVersion>').Groups[1].Value

# Split AssemblyVersion into parts
$versionParts = $assemblyVersion.Split('.')

# Set versionCode as the third number in AssemblyVersion
$versionCode = $versionParts[2]

# Set versionName as the full AssemblyVersion
$versionName = $assemblyVersion

# Load AndroidManifest.xml as an XML document
$androidManifest = [xml](Get-Content $androidManifestPath)

# Update android:versionCode and android:versionName
$manifest = $androidManifest.SelectSingleNode("/manifest")
$manifest.SetAttribute("android:versionCode", $versionCode)
$manifest.SetAttribute("android:versionName", $versionName)

# Save the updated AndroidManifest.xml
$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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
hCentive
United States United States
https://www.linkedin.com/in/bradychambers

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA25-May-24 23:01
professionalȘtefan-Mihai MOGA25-May-24 23:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.