You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Azure DevOps构建脚本更新Android与iOS MAUI应用版本

在Azure DevOps构建MAUI应用时自动修改Android和iOS版本的实现方案

核心思路

利用Azure DevOps内置变量(如构建ID、自定义版本变量),通过脚本在构建前修改MAUI项目中Android和iOS的版本配置文件,或直接通过项目属性注入版本值,实现版本自动更新。

一、Android版本自动修改

方式1:通过MAUI项目文件(.csproj)配置

在MAUI项目的.csproj文件中,替换原硬编码版本值为变量引用:

<PropertyGroup>
  <!-- 版本名,可从Azure DevOps变量传入 -->
  <AndroidVersionName>$(VersionName)</AndroidVersionName>
  <!-- 构建号,直接使用Azure DevOps自增构建ID -->
  <AndroidVersionCode>$(Build.BuildId)</AndroidVersionCode>
</PropertyGroup>

构建时只需在Azure DevOps中定义VersionName变量(如1.0.0),AndroidVersionCode会自动取当前构建ID作为自增版本号。

方式2:直接修改AndroidManifest.xml

如果需要更灵活的控制,可在构建前用PowerShell脚本修改配置文件:

$manifestPath = "$(Build.SourcesDirectory)/YourMauiApp/Platforms/Android/AndroidManifest.xml"
$versionName = "$(VersionName)"
$versionCode = "$(Build.BuildId)"

# 替换versionName属性
(Get-Content $manifestPath) -replace 'android:versionName=".*?"', "android:versionName=""$versionName""" | Set-Content $manifestPath
# 替换versionCode属性
(Get-Content $manifestPath) -replace 'android:versionCode=".*?"', "android:versionCode=""$versionCode""" | Set-Content $manifestPath

二、iOS版本自动修改

方式1:通过MAUI项目文件(.csproj)配置

在.csproj中添加iOS版本属性:

<PropertyGroup>
  <!-- 对应iOS的CFBundleVersion(构建号) -->
  <ApplicationVersion>$(Build.BuildId)</ApplicationVersion>
  <!-- 对应iOS的CFBundleShortVersionString(版本名) -->
  <ApplicationDisplayVersion>$(VersionName)</ApplicationDisplayVersion>
</PropertyGroup>

注意:苹果商店要求CFBundleVersion必须为递增的整数或点分隔整数,用Build.BuildId完全符合要求。

方式2:直接修改Info.plist

用PowerShell脚本修改iOS配置文件:

$plistPath = "$(Build.SourcesDirectory)/YourMauiApp/Platforms/iOS/Info.plist"
$versionName = "$(VersionName)"
$buildNumber = "$(Build.BuildId)"

# 替换CFBundleShortVersionString(版本名)
(Get-Content $plistPath) -replace '<key>CFBundleShortVersionString</key>\s*<string>.*?</string>', "<key>CFBundleShortVersionString</key>`n    <string>$versionName</string>" | Set-Content $plistPath
# 替换CFBundleVersion(构建号)
(Get-Content $plistPath) -replace '<key>CFBundleVersion</key>\s*<string>.*?</string>', "<key>CFBundleVersion</key>`n    <string>$buildNumber</string>" | Set-Content $plistPath

三、Azure DevOps构建流水线配置

  1. 定义变量:在流水线「变量」选项中添加VersionName变量,设置默认值(如1.0.0),后续可手动修改或通过其他逻辑传入。
  2. 添加脚本任务:在MAUI构建任务(如dotnet build)之前,新增「PowerShell脚本」任务,选择「内联脚本」,粘贴对应平台的修改脚本,注意替换项目路径为实际路径。
  3. 构建任务执行:MAUI构建任务会自动读取修改后的配置文件或项目属性,无需额外调整。

额外注意事项

  • Android的AndroidVersionCode必须为整数,Azure DevOps的Build.BuildId是自增整数,完全适配需求。
  • 如果需要基于Git标签生成版本号,可扩展脚本解析Git标签,将解析结果赋值给VersionName变量。
  • 确保脚本任务的执行权限足够修改项目文件。

内容的提问来源于stack exchange,提问作者Rafael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 11:35:19