System.Runtime.CompilerServices.Unsafe 在 Xamarin Forms 中产生意外行为

System.Runtime.CompilerServices.Unsafe produce unexpected behavior in Xamarin Forms

我构建了一个完整的 Xamarin From 应用程序。当我将构建模式更改为 Release 时,我收到此错误消息:

Severity Code Description Project File Line Suppression State Error The "LinkAssemblies" task failed unexpectedly. Mono.Linker.MarkException: Error processing method: 'System.Void Microsoft.Extensions.Primitives.InplaceStringBuilder::Append(System.String,System.Int32,System.Int32)' in assembly: 'Microsoft.Extensions.Primitives.dll' ---> Mono.Cecil.ResolutionException: Failed to resolve System.Void System.Runtime.CompilerServices.Unsafe::CopyBlockUnaligned(System.Void*,System.Void*,System.UInt32) at Mono.Linker.Steps.MarkStep.HandleUnresolvedMethod(MethodReference reference) at Mono.Linker.Steps.MarkStep.MarkMethod(MethodReference reference) at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction instruction)
at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body) at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
at Mono.Linker.Steps.MarkStep.ProcessQueue() --- End of inner exception stack trace --- at Mono.Linker.Steps.MarkStep.ProcessQueue() at Mono.Linker.Steps.MarkStep.ProcessEntireQueue() at Mono.Linker.Steps.MarkStep.Process() at Mono.Linker.Steps.MarkStep.Process(LinkContext context) at Mono.Linker.Pipeline.Process(LinkContext context) at MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context) at Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res) at Xamarin.Android.Tasks.LinkAssemblies.Execute() at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext() EClinicForDoctor.Android

在网上搜索后,我发现这个 solution 上面写着:

此问题的解决方法是确保您拥有支持的最新 Android SDK 版本,并将目标框架设置为使用最新安装的平台。还建议您将目标 Android 版本设置为使用目标框架版本,并将最低 Android 版本设置为 API 15 或更高版本。这被认为是受支持的配置。

因此我更新 Android SDK,设置目标框架并将最低 Android 版本设置为 API 15。

但是,我仍然遇到同样的错误。另一个解决方案说我应该更新 NuGet Packages。我发现包 System.Runtime.CompilerServices.Unsafe 的版本是 4.3.0 并且有针对这个包的更新。更新包时,错误不再出现,但出现另一个问题。当应用程序部署时,它 运行 并在没有任何错误消息的情况下停止。手机只是退出应用程序而不显示任何错误消息。当软件包 System.Runtime.CompilerServices.Unsafe 的版本高于 4.3.0 时会发生这种情况。我试图删除包,同样的问题再次发生。我该如何解决这个问题?

我现在已经在几个不同的位置看到了这个问题弹出窗口。围绕它进行了一些讨论,如下所示:https://github.com/xamarin/xamarin-android/issues/1196

这个 Github 问题还列出了一些(可能的)解决方案,反馈最积极的似乎是这个:

Here's a functioning workaround. It's hacky and ugly but works with minimum fuss. Put this in an XML file and <Import> it in your Android csproj file at the end. It works at least for System.Buffers

<!-- Workaround for https://github.com/xamarin/xamarin-android/issues/1162 -->
<Project>
  <Target Name="ReplaceRefAssemblies" AfterTargets="_ResolveAssemblies">
    <ItemGroup>
      <ResolvedAssembliesFixedWindows Include="@(ResolvedAssemblies->Replace('\ref\','\lib\'))" />
      <ResolvedAssembliesFixedUnix Include="@(ResolvedAssemblies->Replace('/ref/','/lib/'))" />
      <ResolvedAssembliesFixed Include="@(ResolvedAssembliesFixedWindows)" Condition="@(ResolvedAssembliesFixedWindows) != @(ResolvedAssemblies)" />
      <ResolvedAssembliesFixed Include="@(ResolvedAssembliesFixedUnix)" Condition="@(ResolvedAssembliesFixedUnix) != @(ResolvedAssemblies)" />
      <ResolvedAssemblies Remove="@(ResolvedAssemblies)" />
      <ResolvedAssemblies Include="@(ResolvedAssembliesFixed)" />
    </ItemGroup>
  </Target>
</Project>

另一个使用上面的 XML 并说:

  1. I've just copied the content from the link above and saved as UnsafeFix.xml file in the Android project folder.
  2. Then just added: <Import Project="UnsafeFix.xml" /> at the end of android .csproj file, just before
  3. Cleared everything, recompiled and it finally worked!

终于找到解决办法了。 @Gerald Versluis 提供的 issue 中的评论之一解决了问题:

I follow these steps of hongliyu2002 https://forums.realm.io/t/could-not-load-assembly-system-runtime-compilerservices-unsafe-during-startup-registration/974/4

  1. Go to C:\Users%user%.nuget\system.runtime.compilerservices.unsafe.4.0, and delete "ref" folder then make a copy of "lib" folder and rename the copy back to "ref".
  2. Cleanup all the "bin" and "obj" folders in the projects.
  3. Rebuild and run..

This simple workaround , works for me :) My current version is Xamarin.Forms 2.5.1.444934