Rafael
Rafael

Reputation: 2009

How to update Android and iOS MAUI app version via Azure DevOps build script

I have a MAUI application with Android and iOS projects. When I build this application using Azure DevOps script I would like to automatically change application version.

How can I do that?

Upvotes: 3

Views: 2172

Answers (1)

Rafael
Rafael

Reputation: 2009

In your MAUI app .csproj file, add the following directives:

<Target Name="UpdateAndroidAppVersion">
   <XmlPoke XmlInputPath="Platforms\Android\AndroidManifest.xml" Namespaces="&lt;Namespace Prefix='android' Uri='http://schemas.android.com/apk/res/android' /&gt;" Query="manifest/@android:versionCode" Value="$(VersionCode)" />
   <XmlPoke XmlInputPath="Platforms\Android\AndroidManifest.xml" Namespaces="&lt;Namespace Prefix='android' Uri='http://schemas.android.com/apk/res/android' /&gt;" Query="manifest/@android:versionName" Value="$(VersionNumber).$(BuildNumber)" />
</Target>

<Target Name="UpdateIOSAppVersion">
   <XmlPoke XmlInputPath="Platforms/iOS/Info.plist" Query="//dict/key[. = 'CFBundleVersion']/following-sibling::string[1]" Value="$(BuildNumber)" />
   <XmlPoke XmlInputPath="Platforms/iOS/Info.plist" Query="//dict/key[. = 'CFBundleShortVersionString']/following-sibling::string[1]" Value="$(VersionNumber).$(BuildNumber)" />
</Target>

In your azure-pipelines.yml script, at the top level, declare variables, to control version representation (particular variable values are shown just for example):

variables:
  - name: versionNumber 
    value: "2023.1"
  - name: buildNumber
    value: $[counter(variables['versionNumber'], 0)]
  - name: versionCode
    value: $[counter('versionCode', 1)]

Then, in your yml-script, separately for android and ios jobs, declare the following MSBuild directives:

Android job directive:

- task: MSBuild@1
  displayName: "Update Android App Version"
  inputs:
    solution: '**/MyMAUIApplication.csproj' 
    msbuildArguments: '/t:UpdateAndroidAppVersion /p:VersionNumber="$(versionNumber)" /p:VersionCode="$(versionCode)" /p:BuildNumber="$(buildNumber)"'

iOS job directive:

- task: MSBuild@1
  displayName: "Update iOS App Version"
  inputs:
    solution: '**/MyMAUIApplication.csproj' 
    msbuildArguments: '/t:UpdateIOSAppVersion /p:VersionNumber="$(versionNumber)" /p:VersionCode="$(versionCode)" /p:BuildNumber="$(buildNumber)"'

Upvotes: 8

Related Questions