Azure DevOps Pipeline无法还原net8.0-ios与net8.0-android的MAUI包
问题概述
我的.NET 8 MAUI应用目标框架配置如下:
<TargetFrameworks>net8.0-android;net8.0-ios;</TargetFrameworks>
本地可正常构建、部署到物理设备,但在Azure DevOps Pipeline构建时,NuGet包还原失败,抛出NU1202错误:提示Microsoft.Maui.Controls.Compatibility 8.0.72等包与net80-android、net80-ios不兼容,这些包仅支持带具体版本的目标框架(如net80-android34.0、net80-ios17.2)。
我不想修改目标框架(TFWs),因为相同配置在GitHub Actions中可正常运行;即便修改,还会出现版本不匹配问题(如CommunityToolkit.Maui 9.0.3兼容net8.0-ios17.5,而Microsoft.Maui.Controls.Compatibility兼容net8.0-ios17.2)。已尝试将MAUI版本更新至最新稳定版8.0.82,问题依旧。
我的iOS Pipeline配置如下:
trigger: - develop pool: vmImage: macos-14 variables: - group: Maui steps: - task: Bash@3 displayName: Install MAUI inputs: targetType: 'inline' script: | dotnet nuget locals all --clear dotnet workload install maui-android maui-ios - task: InstallAppleCertificate@2 inputs: certSecureFile: 'bestshifts.p12' certPwd: $(p12Secret) keychain: 'temp' - task: InstallAppleProvisioningProfile@1 inputs: provisioningProfileLocation: 'secureFiles' provProfileSecureFile: 'bestishifts-appstore.mobileprovision' - task: NuGetCommand@2 displayName: 'Add custom NuGet source' inputs: command: 'custom' arguments: 'sources add -name DevExpress -source https://nuget.devexpress.com/$(devExpressApiKey)/api' - task: NuGetCommand@2 displayName: 'Restore NuGet packages' inputs: command: 'restore' feedsToUse: 'select' vstsFeed: 'https://nuget.devexpress.com/$(devExpressApiKey)/api' includeNuGetOrg: true - task: Bash@3 displayName: 'Build iOS App' inputs: targetType: 'inline' script: | dotnet build -f net8.0-ios -c Release -p:ArchiveOnBuild=true -p:RuntimeIdentifier=ios-arm64 -p:CodesignKey="iPhone Distribution" -p:CodesignProvision="Automatic" -o ./artifacts
解决方案
1. 改用dotnet restore替代NuGetCommand任务
Azure DevOps的NuGetCommand@2任务无法正确识别MAUI的目标框架别名(net8.0-android/net8.0-ios),改用dotnet restore可借助.NET SDK对MAUI workload的原生支持,自动解析兼容的框架版本。
替换原还原步骤为:
- task: Bash@3 displayName: 'Restore NuGet packages' inputs: targetType: 'inline' script: | dotnet restore --source https://nuget.devexpress.com/$(devExpressApiKey)/api --source https://api.nuget.org/v3/index.json
2. 确保MAUI workload安装匹配版本
安装workload时添加--from-rollback-file参数,强制安装与.NET 8 SDK官方匹配的MAUI workload版本,避免版本不兼容:
- task: Bash@3 displayName: Install MAUI inputs: targetType: 'inline' script: | dotnet nuget locals all --clear dotnet workload install maui-android maui-ios --from-rollback-file https://aka.ms/dotnet/maui/net8.0/rollback.json
3. 构建时显式指定目标平台版本
若上述方法无效,可在dotnet build命令中添加目标平台版本参数,帮助NuGet明确兼容包范围:
- task: Bash@3 displayName: 'Build iOS App' inputs: targetType: 'inline' script: | dotnet build -f net8.0-ios -c Release -p:ArchiveOnBuild=true -p:RuntimeIdentifier=ios-arm64 -p:CodesignKey="iPhone Distribution" -p:CodesignProvision="Automatic" -p:TargetPlatformVersion=17.2 -o ./artifacts
(注:TargetPlatformVersion可根据实际兼容版本调整,比如17.5)
4. 检查NuGet源配置
确保自定义源和nuget.org都被正确引用,避免NuGetCommand@2的vstsFeed参数覆盖默认源,改用dotnet restore的--source参数明确指定所有需要的源。
内容的提问来源于stack exchange,提问作者Gaburiere

