在使用Jenkins构建Xamarin.Forms项目时,出现了缺少netstandard 2.0引用的问题。

huangapple go评论86阅读模式
英文:

Missing netstandard 2.0 reference in Xamarin.Forms project when building with Jenkins

问题

我遇到了超过1500个类似的错误,当我尝试使用Jenkins构建更新的Xamarin.Forms项目时:

>App.xaml.cs(24,32): error CS0012: 类型“Object”在未引用的程序集中定义。您必须添加对程序集“netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51”的引用。

如果我在同一台机器上使用Visual Studio 2017构建项目,就不会出现问题(与我的本地机器上的情况相同)。用于Jenkins的构建命令如下:

"C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild" /p:ReferencePath=C:\jenkins\workspace\somelibrary\ SomeProject/SomeProject/SomeProject.csproj /t:Restore /p:Configuration=Release /t:Build 

之前,我更新到了.NET Standard 2.0Xamarin.Forms 3.4.0.1039999。构建服务器本身运行在<s>Windows 10 1703</s>Windows 10 1909上。<s>我的Xamarin.Forms项目中包含UWP,但我实际上没有使用它。</s>(我阅读说,如果要与UWP一起使用.NET Standard 2.0,至少需要Windows 10 1709。)

该项目还引用了一个NuGet包,该包尚不兼容.NET Standard 2.0。这里我得到了警告:

>Warning NU1701: 包“xxx”是使用“.NETFramework, Version=v4.6.1”而不是项目目标框架“.NETStandard, Version=v2.0”恢复的。此包可能与您的项目不完全兼容。

这可能是一些问题的原因,因为它不是完全兼容

我尝试过的事项:

  • 清除构建,删除binobj文件夹
  • 尝试使用&lt;Reference Include="netstandard" /&gt;,但在Jenkins中无法成功构建。对NETStandard.Library的引用会在我的本地构建环境中引发警告(但已安装)
  • 更新了Visual Studio Build Tools 2017Visual Studio 2017到最新版本(15.9.18)
  • 在构建服务器上安装了.NET Core SDK.NET Core Build Tools.NET Framework 4.7.2 SDK
  • 使用C:\Program Files\dotnet扩展了PATH环境变量,将全局环境变量MSBuildSDKsPath设置为C:\Program Files\dotnet\sdk\2.1.509\Sdks
  • 删除了UWP项目(仍然存在于文件夹中,但不在*.sln中)
  • 对来自其他用户的类似问题进行了一些研究,但没有找到解决问题的方法

我观察到的情况:

  • 使用Visual Studio 2017在构建服务器上的Windows 10 1703上构建项目正常
  • 使用Visual Studio 2017在我本地机器上的Windows 10 1809上构建项目正常
  • 在构建服务器上使用msbuild和命令行构建项目正常(NuGet包正确引用了netstandard2.0而不是netstandard1.3等)

我可以尝试的事项:

  • <s>将构建服务器升级到Windows 10 1709或更高版本</s>
  • 替换/更新仍然使用.NET 4.6.1的库
  • 升级到Visual Studio 2019
  • 从头开始创建一个.NET Standard项目并复制所有文件(但会失去GIT历史记录)

但我不明白的是,在Visual Studio中一切正常,但在msbuild/Jenkins中不行。似乎缺少引用或某些内容没有安装。

我应该怎么办?

英文:

I'm getting over 1500 errors like this, when I try to build an updated Xamarin.Forms project with Jenkins:

>App.xaml.cs(24,32): error CS0012: Der Typ "Object" ist in einer nicht referenzierten Assembly definiert. Fügen Sie einen Verweis auf die Assembly "netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" hinzu.

Translated error:

>CS0012: The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

If I build the project with the same machine, but within Visual Studio 2017 it works without problems (as on my local machine). The build command for Jenkins looks like this

&quot;C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild&quot; /p:ReferencePath=C:\jenkins\workspace\somelibrary\ SomeProject/SomeProject/SomeProject.csproj /t:Restore /p:Configuration=Release /t:Build 

Previously I updated to .NET Standard 2.0 and Xamarin.Forms 3.4.0.1039999. The build server itself is running on <s>Windows 10 1703</s>Windows 10 1909. <s>I have UWP in my Xamarin.Forms project, but I'm not really using it.</s> (I read that you need at least Windows 10 1709 if you want to use .NET Standard 2.0 together with UWP.)

The project is also referencing a NuGet package, which isn't compatible with .NET Standard 2.0 yet. Here I get the warning

>Warning NU1701 Package 'xxx' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.

This could be a reason why there are some problems, because it is not fully compatible.

What I tried:

  • clean build, deleted bin and obj folder
  • use of &lt;Reference Include=&quot;netstandard&quot; /&gt; without success at building with Jenkins. Reference to NETStandard.Library throws a warning in my local build environment (but it is installed)
  • updated Visual Studio Build Tools 2017 and Visual Studio 2017 to the latest version (15.9.18)
  • installed .NET Core SDK, .NET Core Build Tools, .NET Framework 4.7.2 SDK on the build server
  • extended PATH environment variable with C:\Program Files\dotnet, set global environment variable MSBuildSDKsPath to C:\Program Files\dotnet\sdk\2.1.509\Sdks
  • deleted UWP project (still exists on folder, but not in *.sln)
  • made some research after similar problems from other users, but didn't find something that solved the issue

What I observed:

  • building the project works fine with Visual Studio 2017 on the build server with Windows 10 1703
  • building the project works fine with Visual Studio 2017 on my local machine with Windows 10 1809
  • building the project works fine with msbuild and command line on the build server (NuGet packages references correctly netstandard2.0 instead of netstandard1.3 and so on in the NuGetFallbackFolder)

What I could try:

  • <s>update the build server to Windows 10 1709 or higher</s>
  • replace/update the library, which still uses .NET 4.6.1
  • upgrade to Visual Studio 2019
  • create a .NET Standard project from scratch and copy all files over (and loosing GIT history)

But what I don't get is that it is working fine in Visual Studio but not with msbuild/Jenkins. Seems that a reference is missing or something is not installed.

What should I do?

Edit:

Extract of the .csproj from the Xamarin.Forms part of the project

&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
  &lt;PropertyGroup&gt;
    &lt;TargetFramework&gt;netstandard2.0&lt;/TargetFramework&gt;
    &lt;NeutralLanguage&gt;en&lt;/NeutralLanguage&gt;
    &lt;AssemblySearchPaths&gt;
        $(AssemblySearchPaths);
        $(ReferencePath);
    &lt;/AssemblySearchPaths&gt;
  &lt;/PropertyGroup&gt;
  &lt;ItemGroup&gt;
    &lt;None Remove=&quot;picture.png&quot; /&gt;
  &lt;/ItemGroup&gt;

  &lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;Plugin&quot; Version=&quot;5.2.0&quot; /&gt;
  &lt;/ItemGroup&gt;

  &lt;ItemGroup&gt;
    &lt;Reference Include=&quot;SomeLibrary&quot;&gt;
      &lt;HintPath&gt;pat\to\library\netstandard2.0\SomeLibrary.dll&lt;/HintPath&gt;
    &lt;/Reference&gt;
  &lt;/ItemGroup&gt;

    &lt;ItemGroup&gt;
        &lt;Compile Update=&quot;Common\Localization\AppResources.Designer.cs&quot;&gt;
            &lt;AutoGen&gt;True&lt;/AutoGen&gt;
            &lt;DesignTime&gt;True&lt;/DesignTime&gt;
            &lt;DependentUpon&gt;AppResources.resx&lt;/DependentUpon&gt;
        &lt;/Compile&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
        &lt;EmbeddedResource Update=&quot;Common\Localization\AppResources.de.resx&quot;&gt;
            &lt;SubType&gt;Designer&lt;/SubType&gt;
        &lt;/EmbeddedResource&gt;
        &lt;EmbeddedResource Update=&quot;Common\Localization\AppResources.resx&quot;&gt;
            &lt;Generator&gt;PublicResXFileCodeGenerator&lt;/Generator&gt;
            &lt;LastGenOutput&gt;AppResources.Designer.cs&lt;/LastGenOutput&gt;
            &lt;SubType&gt;Designer&lt;/SubType&gt;
        &lt;/EmbeddedResource&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;None Update=&quot;App.xaml&quot;&gt;
        &lt;Generator&gt;MSBuild:Compile&lt;/Generator&gt;
      &lt;/None&gt;
      &lt;None Update=&quot;SomePage.xaml&quot;&gt;
        &lt;Generator&gt;MSBuild:Compile&lt;/Generator&gt;
      &lt;/None&gt;
    &lt;/ItemGroup&gt;

&lt;/Project&gt;

答案1

得分: 0

我认为我用这个解决了它:

"C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild" SomeProject/SomeProject/SomeProject.csproj /t:Restore /p:Configuration=Release

"C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild" /p:ReferencePath=C:\jenkins\workspace\somelibrary\ SomeProject/SomeProject/SomeProject.csproj /p:Configuration=Release /t:Build

所以解决方案是进行两次调用:一次用于restore,一次用于build。我分别为Xamarin.Forms项目和特定平台项目执行此操作。

英文:

I think I solved it with this

&quot;C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild&quot; SomeProject/SomeProject/SomeProject.csproj /t:Restore /p:Configuration=Release

&quot;C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild&quot; /p:ReferencePath=C:\jenkins\workspace\somelibrary\ SomeProject/SomeProject/SomeProject.csproj /p:Configuration=Release /t:Build

So the solution is to make two calls: one for restore and one for build. I do this for the Xamarin.Forms project and platform specific project separately.

huangapple
  • 本文由 发表于 2020年1月3日 20:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定