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

Azure DevOps构建Xamarin.Android时提示需引用MonoAndroid v12.0(目标v13)的解决方法

Xamarin.Android Azure DevOps构建报错:缺少MonoAndroid v12.0引用

错误日志

##[error]C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\NuGet\16.0\Microsoft.NuGet.targets(198,5): Error : Your project does not reference "MonoAndroid,Version=v12.0" framework. Add a reference to "MonoAndroid,Version=v12.0" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.
##[debug]Processed: ##vso[task.logissue type=Error;sourcepath=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\NuGet\16.0\Microsoft.NuGet.targets;linenumber=198;columnnumber=5;code=;]Your project does not reference "MonoAndroid,Version=v12.0" framework. Add a reference to "MonoAndroid,Version=v12.0" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\NuGet\16.0\Microsoft.NuGet.targets(198,5): error : Your project does not reference "MonoAndroid,Version=v12.0" framework. Add a reference to "MonoAndroid,Version=v12.0" in the "TargetFrameworks" property of your project file and then re-run NuGet restore. [D:\a\1\s\MyProject.Xamarin\MyProject.Android\MyProject.Android.csproj]
##[debug]Processed: ##vso[task.logdetail id=e408d669-be6f-49f1-b429-2e88049c5407;parentid=b84e3e4f-4467-4eae-9902-6f94de004f74;type=Build;result=Failed;finishtime=2022-08-31T13:01:13.7014902Z;progress=100;state=Completed;parentid=b84e3e4f-4467-4eae-9902-6f94de004f74;name=;]
Done Building Project "D:\a\1\s\MyProject.Xamarin\MyProject.Android\MyProject.Android.csproj" (PackageForAndroid target(s)) -- FAILED.

Build FAILED.

背景与尝试操作

  • 原构建脚本在旧版Android环境下正常运行,升级后触发上述错误
  • 本地调试时将项目TargetFramework设为v13(计划分发版本),构建仍报错;改为v12后错误依旧
  • 已尝试操作:手动指定JDK 11、尝试切换至最新Visual Studio(但日志显示仍使用2019)、使用最新MSBuild并设置UseLatestPlatformSdk=true,问题未解决

构建脚本片段

task: VSBuild@1
  inputs:
    solution: '**/*.sln'
    msbuildArgs: '/t:Restore'
    vsVersion: 'latest'

task: XamarinAndroid@1
  inputs:
    projectFile: '**/*droid.csproj'
    outputDirectory: '$(outputDirectory)'
    configuration: '$(buildConfiguration)'
    msbuildVersionOption: 'latest'
    msbuildArguments: '/p:increaseBuildNumber=1'
    jdkOption: 'JDKVersion'
    jdkVersionOption: '1.11'

疑问

  1. 构建脚本操作有误的地方在哪里?
  2. 能否在Azure DevOps代理上手动设置Mono.Android版本?

解决方案

1. 核心问题:Visual Studio版本不匹配

Xamarin.Android 13需要Visual Studio 2022支持,日志显示代理仍使用VS2019,这是关键矛盾。即使设置vsVersion: 'latest',如果代理机器未安装VS2022,依然会 fallback 到VS2019。

2. 修正构建脚本

  • 替换VSBuild@1为NuGetCommand@2单独执行还原操作,避免VS版本冲突
  • 在XamarinAndroid@1任务中明确指定使用VS2022的MSBuild,同时添加TargetFramework参数对齐项目配置
  • 确保还原和构建使用一致的TargetFramework

修正后的脚本示例:

task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'
    vstsFeed: 'your-feed-name' # 根据实际情况替换

task: XamarinAndroid@1
  inputs:
    projectFile: '**/*droid.csproj'
    outputDirectory: '$(outputDirectory)'
    configuration: '$(buildConfiguration)'
    msbuildVersionOption: 'specific'
    msbuildVersion: '17.0' # VS2022对应MSBuild版本
    msbuildArguments: '/p:increaseBuildNumber=1 /p:TargetFramework=MonoAndroid13.0 /p:UseLatestPlatformSdk=true'
    jdkOption: 'JDKVersion'
    jdkVersionOption: '1.11'

3. 项目文件确认

检查.csproj文件,确保TargetFramework节点正确设置:

<TargetFramework>MonoAndroid13.0</TargetFramework>
<!-- 如果是多目标框架,格式如下: -->
<!-- <TargetFrameworks>MonoAndroid12.0;MonoAndroid13.0</TargetFrameworks> -->

4. 代理机器环境配置

如果使用自托管代理:

  • 安装Visual Studio 2022并勾选Xamarin.Android开发组件
  • 确认已安装Android 13 SDK(API Level 33)
  • 确保JDK 11已正确配置并在PATH中

如果使用微软托管代理:

  • 选择windows-2022代理池,该池默认包含VS2022、Xamarin.Android 13及对应SDK

关于手动设置Mono.Android版本

可以通过MSBuild参数强制指定,在msbuildArguments中添加/p:MonoAndroidVersion=13.0,但前提是代理环境已安装对应版本的Xamarin.Android SDK。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 17:39:33