使用控制台构建Xamarin项目时,dotnet构建失败

5
当我尝试使用dotnet构建我的.csproj文件时,它会抛出错误,但是当我在Visual Studio 2019中构建项目时,它成功了。我需要使用dotnet进行构建,因为我的Azure管道作业需要它。这是错误信息:
dotnet build MyProject.Mobile.Droid.csproj --configuration Release --force

MyProject.Mobile.Droid.csproj(584,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\3.1.100\\Xamarin\Android\Xamarin.Android.CSharp.targets was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\3.1.100\\Xamarin\Android\Xamarin.Android.CSharp.targets" is correct, and the file exists on disk.

Build FAILED.

(也可以查看截图。)
如果我将.csproj中的路径参数设置为以下内容:
<MSBuildExtensionsPath>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild</MSBuildExtensionsPath>

然后错误发生了变化:
The reference assemblies for MonoAndroid,Version=v1.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks

有没有解决这个问题的建议?
2个回答

7

如今,您需要使用msbuild(例如msbuild MySolution.sln)来构建Xamarin项目。

您可以使用dotnet build构建单独的netstandard2.x项目,但是基于Mono平台的项目(即Android和iOS项目)需要使用msbuild进行构建。


1
尝试将 TargetFrameworkRootPath 设置为您安装 Xamarin 的位置。
<TargetFrameworkRootPath>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\</TargetFrameworkRootPath>

或者在命令行上。
dotnet build -clp:ErrorsOnly -p:MSBuildExtensionsPath="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild"/ -p:TargetFrameworkRootPath="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/ReferenceAssemblies/Microsoft/Framework/"

使用 macOS 而不是 Windows 怎么样? - knocte
根据ChatGPT的说法,macOS的等效命令应该是 dotnet build -clp:ErrorsOnly -p:MSBuildExtensionsPath="/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/msbuild/Current/bin" -p:TargetFrameworkRootPath="/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/xbuild-frameworks,但我还没有测试过。 - user1623521
1
@user1623521 很遗憾,上面的命令无法运行。 - pfedotovsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接