繁体   English   中英

Xamarin.iOS 次构建,最显着的是链接和 AOT 编译,在调试配置中耗时超过 2 分钟

[英]Xamarin.iOS builds, most notably Linking and AOT-compilation, take over 2 minutes in debug configurations

我有一个 Xamarin.iOS 项目(作为跨平台解决方案的一部分),随着时间的推移它已经增长了很多。 然而,只有 iOS 构建花费了如此多的时间(Windows 桌面构建需要几秒钟,而 Android 很少超过 30 秒,其中 24 秒是安装过程)。 经过大量调整后,我可以将 iOS 设备/模拟器的构建时间减少到大约 02:21,这仍然远远不利于调试。

下面是这次实现的工程文件:

<Project Sdk="Microsoft.NET.Sdk">
    
    <PropertyGroup>
        <TargetFramework>net6.0-ios</TargetFramework>
        <SingleProject>true</SingleProject>
        <OutputType>Exe</OutputType>
        <RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
        <IsUnoHead>true</IsUnoHead>
        <SupportedOSPlatformVersion>14.2</SupportedOSPlatformVersion>
        <Nullable>enable</Nullable>
        <UseMauiEssentials>True</UseMauiEssentials>
        <ProvisioningType>manual</ProvisioningType>
        <MtouchInterpreter>-all</MtouchInterpreter>
    </PropertyGroup>

    <PropertyGroup>
        <MtouchExtraArgs>$(MtouchExtraArgs) --setenv=MONO_GC_PARAMS=soft-heap-limit=512m,nursery-size=64m,evacuation-threshold=66,major=marksweep,concurrent-sweep</MtouchExtraArgs>
        <MtouchExtraArgs>$(MtouchExtraArgs) --marshal-objectivec-exceptions:disable</MtouchExtraArgs>
        <MtouchExtraArgs>$(MtouchExtraArgs) --marshal-managed-exceptions:default</MtouchExtraArgs>
        <MtouchExtraArgs>$(MtouchExtraArgs) --registrar:static</MtouchExtraArgs>
        <MtouchExtraArgs>$(MtouchExtraArgs) --time --time</MtouchExtraArgs>
        <MtouchExtraArgs>$(MtouchExtraArgs) -v -v -v -v</MtouchExtraArgs>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
        <MtouchLink>Full</MtouchLink>
        <Optimize>false</Optimize>
    </PropertyGroup>
    
    <PropertyGroup Condition="'$(Configuration)' != 'Debug'">
        <MtouchUseLlvm>true</MtouchUseLlvm>
        <MtouchLink>SdkOnly</MtouchLink>
        <BuildIpa>true</BuildIpa>
        <Optimize>true</Optimize>
        <CodesignKey>***</CodesignKey>
        <CodesignProvision>***</CodesignProvision>
    </PropertyGroup>
    
    <ItemGroup>
<!--project references-->
    </ItemGroup>
    
</Project>

环境:

  • Windows 11 机器上的 Visual Studio 22 (17.4.3);
  • 远程连接到运行 Xcode 14.2 的 Mac Mini 2018;
  • Xamarin.iOS 16.1.1.27

不过,在其中任何一个的各自较旧版本上,该问题既没有变坏也没有变好。

与通过 Visual Studio 远程构建相比,直接从 Mac 构建没有显着改进。

我尝试篡改一些项目属性以尽可能缩短构建时间:

  1. MtouchLink
  • 设置“SdkOnly”或“Full”并没有像预期的那样产生太大的差异。 当在物理设备上运行时,前者将构建时间提高了大约 10 秒。 在模拟器上,总构建时间通常没有变化。 考虑到 ILLink 所需的运行时间(见下文),这让我感到惊讶。
  • 设置“无”不起作用,因为它会导致Failed to AOT compile Microsoft.iOS.dll, the AOT compiler exited with code 134错误。
  1. MtouchInterpreter
  • 禁用时,键入 generics 会在运行时导致错误,但启用它对构建时间没有任何可衡量的影响。
  1. MtouchUseLlvm
  • 我仅将其用于发布版本,因为它将构建时间增加到10分钟以上(对于偶尔的发布版本来说是可以忍受的)。

省略部分或全部MtouchExtraArgs也无济于事。

有一些任务占用了大部分时间到 binlog:

  • Target_UnpackLibraryResources (~3s);
  • Target_CoreCompile (~3s);
  • Target_FindAotCompiler(~4s);
  • Target_RunILLink (~46s, ~53s with SdkOnly), 几乎完全专用于 Xamarin.iOS.Task.ILLink;
  • Target_AOTCompile (~52 秒),CompileNativeCode 耗时~38 秒;
  • Target_CompileNativeExecutable(~3s);
  • Target_GenerateDSym (~5s);
  • Target_SayGoodbye(~7 秒)

Xamarin.iOS 默认使用 AOT :Apple 设置了 iOS 的安全限制,不允许在设备上执行动态生成的代码。 为确保我们遵守这些安全协议,Xamarin.iOS 使用提前 (AOT) 编译器来编译托管代码。

AOT 编译是一种用于提高启动性能的优化技术,但它也会影响应用程序的构建时间。 Xamarin AOT文档中介绍:

构建时间——AOT 编译比 JIT 慢得多,并且会减慢使用它的构建。 这种减慢的范围从几秒到一分钟或更长时间,具体取决于编译的程序集的大小和数量。

链接 Xamarin.iOS Apps 文档描述(这也是构建时间更长的原因):

linker 使用 static 分析来确定您的应用程序可接受遵循的不同代码路径。 它有点重,因为它必须 go 通过每个组件的每个细节,以确保没有删除任何可发现的内容。 它在模拟器构建中默认不启用,以在调试时加快构建时间。

您可以尝试关闭 Visual Studio,然后从您的项目中删除 bin 和 obj,这将缓解构建缓慢的问题。 更多信息可以参考: iOS Build Mechanics

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM