[英]Why does loading a Xamarin Android .csproj into a Microsoft.Build.Evaluation.Project throw InvalidPropectFileException due to missing .targets file?
I am attempting to open a Xamarin Android.csproj file in a .NET 5.0 console application using an instance of Microsoft.Build.Evaluation.Project
.我正在尝试使用
Microsoft.Build.Evaluation.Project
的实例在 .NET 5.0 控制台应用程序中打开 Xamarin Android.csproj 文件。 Additionally I have also attempted to do the same with a .NET Framework 4.6.1 project.此外,我还尝试对 .NET Framework 4.6.1 项目执行相同的操作。
The code for loading is:加载代码为:
var fileName = @"C:\Users\vchel\source\repos\BlankAndroid\BlankAndroid\BlankAndroid.csproj";
var project = new global::Microsoft.Build.Evaluation.Project(fileName, null, null, new ProjectCollection());
When I attempt to instantiate the Project
object, I get the following exception:当我尝试实例化
Project
object 时,出现以下异常:
Microsoft.Build.Exceptions.InvalidProjectFileException:
The imported project "C:\Users\vchel\source\repos\ProjectLoadTest\ProjectLoadTest\bin\Debug\net5.0\Xamarin\Android\Xamarin.Android.CSharp.targets" was not found.
Confirm that the expression in the Import declaration "C:\Users\vchel\source\repos\ProjectLoadTest\ProjectLoadTest\bin\Debug\net5.0\Xamarin\Android\Xamarin.Android.CSharp.targets" is correct, and that the file exists on disk. C:\Users\vchel\source\repos\BlankAndroid\BlankAndroid\BlankAndroid.csproj
The Xamarin.Android.CSharp.targets file referenced in the error above appears in the.csproj that I am loading as shown in the following snippet:上面错误中引用的 Xamarin.Android.CSharp.targets 文件出现在我正在加载的 .csproj 中,如以下代码片段所示:
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
Notice that the.csproj references the Xamarin.Android.CSharp.targtets file with the $(MSBuildExtensionPath)
prefix, but at runtime this is swapped with the location of the running console application.exe rather than请注意,.csproj 引用带有
$(MSBuildExtensionPath)
前缀的 Xamarin.Android.CSharp.targtets 文件,但在运行时,它与正在运行的控制台 application.exe 的位置交换,而不是
The Android project which I am attempting to load opens correctly in Visual Studio and compiles/runs with no problems.我尝试加载的 Android 项目在 Visual Studio 中正确打开并且编译/运行没有问题。
Why does this Android project open without any problems in Visual Studio, yet it fails when attempting to open in a Microsoft.Build.Evaluation.Project
object?为什么这个 Android 项目在 Visual Studio 中打开没有任何问题,但在
Microsoft.Build.Evaluation.Project
object 中尝试打开时却失败了?
It seems I've actually run into this same problem before, but it wasn't on an Android project so I'll keep this question here to address Android-specific issues.看来我之前确实遇到过同样的问题,但它不在 Android 项目中,所以我将把这个问题留在这里以解决特定于 Android 的问题。
The problem in general is discussed here:一般的问题在这里讨论:
How to Load.csproj file into .NET Core 3.0 using MSBuild NuGet 如何使用 MSBuild NuGet 将.csproj 文件加载到 .NET Core 3.0
The summary is - the.target files are separate from the nuget packages being installed, and it seems like the Project class can't resolve $(MSBuildExtensionsPath).总结是——.target 文件与正在安装的 nuget 包是分开的,而且项目 class 似乎无法解析 $(MSBuildExtensionsPath)。 Therefore, to solve this problem the relevant.targets files should be added to the project and copied to the output (bin) folder.
因此,要解决此问题,应将相关的.targets文件添加到项目中并复制到output(bin)文件夹中。
On my machine, the original files can be found in:在我的机器上,可以在以下位置找到原始文件:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Xamarin\Android
I had to add the following files to my app.我必须将以下文件添加到我的应用程序中。 Notice they are added to the
Xamarin/Android/
folder:请注意,它们已添加到
Xamarin/Android/
文件夹中:
Items can be marked as Copy if newer :如果较新,项目可以标记为复制:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.