无法在发布模式下使用StructureMap编译Xamarin android项目

时间:2017-08-22 08:42:20

标签: xamarin xamarin.android structuremap

在Visual Studio中创建一个空白的android项目Blank App (Android),然后添加以下nuget包
NETStandard.Library 1.6.1
StructureMap 4.5.2

现在尝试在发布模式下编译项目。 我收到以下错误:

Using "LinkAssemblies" task from assembly "C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Build.Tasks.dll".
Task "LinkAssemblies"
  LinkAssemblies Task
    UseSharedRuntime: False
    MainAssembly: obj\Release\linksrc\App2.dll
    OutputDirectory: obj\Release\android\assets\
    OptionalDestinationDirectory:
    I18nAssemblies:
    LinkMode: SdkOnly
    LinkSkip:
    LinkDescriptions:
    ResolvedAssemblies:
      obj\Release\linksrc\App2.dll
      obj\Release\linksrc\Java.Interop.dll
      obj\Release\linksrc\Mono.Android.dll
      obj\Release\linksrc\System.Core.dll
      obj\Release\linksrc\System.dll
      obj\Release\linksrc\System.IO.Compression.dll
      obj\Release\linksrc\System.Net.Http.dll
      obj\Release\linksrc\System.Xml.dll
      obj\Release\linksrc\mscorlib.dll
      obj\Release\linksrc\StructureMap.dll
      obj\Release\linksrc\System.Runtime.Loader.dll
      obj\Release\linksrc\System.Runtime.dll
      obj\Release\linksrc\System.ComponentModel.Composition.dll
      obj\Release\linksrc\System.Threading.dll
      obj\Release\linksrc\System.Collections.dll
      obj\Release\linksrc\System.Collections.Concurrent.dll
      obj\Release\linksrc\System.Reflection.dll
      obj\Release\linksrc\System.Diagnostics.Debug.dll
      obj\Release\linksrc\System.Linq.dll
      obj\Release\linksrc\System.Runtime.InteropServices.dll
      obj\Release\linksrc\System.Runtime.Extensions.dll
      obj\Release\linksrc\System.Reflection.Extensions.dll
      obj\Release\linksrc\System.Runtime.Serialization.dll
      obj\Release\linksrc\System.ServiceModel.Internals.dll
      obj\Release\linksrc\Mono.Security.dll
      obj\Release\linksrc\System.Resources.ResourceManager.dll
      obj\Release\linksrc\System.IO.dll
      obj\Release\linksrc\System.Threading.Tasks.dll
      obj\Release\linksrc\System.Linq.Expressions.dll
      obj\Release\linksrc\System.Reflection.Emit.Lightweight.dll
      obj\Release\linksrc\System.Reflection.Emit.ILGeneration.dll
      obj\Release\linksrc\System.Reflection.TypeExtensions.dll
      obj\Release\linksrc\System.Reflection.Primitives.dll
      obj\Release\linksrc\System.Globalization.dll
      obj\Release\linksrc\System.AppContext.dll
      obj\Release\linksrc\System.IO.FileSystem.dll
      obj\Release\linksrc\System.Console.dll
    EnableProguard: False
    ProguardConfiguration:
    DumpDependencies: False
    LinkOnlyNewerThan:
    HttpClientHandlerType:
    TlsProvider:
C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018: The "LinkAssemblies" task failed unexpectedly.
C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018: Mono.Linker.MarkException: Error processing method: 'System.Boolean StructureMap.Graph.AssemblyFinder/<FindAssemblies>
  d__1::MoveNext()' in assembly: 'StructureMap.dll' ---> Mono.Cecil.ResolutionException: Failed to resolve System.Runtime.Loader.AssemblyLoadContext System.Runtime.Loader.AssemblyLoadContext::get_Default()
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.HandleUnresolvedMethod(MethodReference reference)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.MarkMethod(MethodReference reference)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction instruction)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.ProcessQueue()
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    --- End of inner exception stack trace ---
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.ProcessQueue()
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.Process()
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Mono.Linker.Pipeline.Process(LinkContext context)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res)
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Xamarin.Android.Tasks.LinkAssemblies.Execute()
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
  C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1577,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
Done executing task "LinkAssemblies" -- FAILED.
Done building target "_LinkAssembliesShrink" in project "App2.csproj" -- FAILED.

Build FAILED.

在调试模式下,它按预期编译,但我想LinkAssemblies任务不会运行。

我正在使用Xamarin 4.6.0.297和Visual Studio 2015。

这是StructureMapXamarin中的问题吗?

2 个答案:

答案 0 :(得分:3)

更新

链接器和NetStandard库的不同版本似乎存在问题。 StructureMap被设置为nuget中的支持.Net45,NetStandard 1.3和NetStandard 1.5,当添加NetStandard Lib 1.6并且除非您将软件包更新为NetStandard Lib 2.0 (和它的匹配库),您将收到Mono.Cecil错误:

Mono.Cecil.ResolutionException: Failed to resolve System.Runtime.Loader.AssemblyLoadContext System.Runtime.Loader.AssemblyLoadContext::get_Default()

注意:我不确定这是Mono.Cecil的问题,StructureMap的打包方式还是Xamarin处理nuget引用的方式......

LinkDescription

如下所示的链接器配置可以解决您的问题:

<linker>
        <assembly fullname="StructureMap">
                <namespace fullname="StructureMap.*" />
        </assembly>
</linker>

只需使用上面的内容将XML文件添加到项目中,并为其指定LinkDescription的构建操作。

注意:使用AndroidLinkSkip跳过程序集将无效...

答案 1 :(得分:0)

对我来说,使用LinkDescription添加AndroidLinkToSkip文件并更新到.NETStandard 2.0都不起作用。相反,我按照https://github.com/structuremap/structuremap/pull/597的建议将StructureMap降级到4.4.0。这似乎解决了这个问题