Linker 分析器

前言

哈哈,mono linker, Xamarin 开发人员最大的敌人之一。不过不用害怕,欲先取之,必先予之。我们现在就来分析一下 Linker 吧

什么是 Linker Analyzer?

Linker Analyzer 是一个命令行工具,用于分析在 LinkAssemblies 步骤中记录的依赖关系。它将显示哪些 items 已被标记并导致链接的程序集。

The Linker Analyzer is a command line tool that analyzes dependencies which are recorded during the LinkAssemblies step. It will show you what items were marked and resulted in the linked assembly.

入门

您可以针对你的 Xamarin.Android app 使用以下命令:

msbuild /p:LinkerDumpDependencies=true /p:Configuration=Release YourAppProject.csproj

msbuild 环境变量配置参考:https://www.jianshu.com/p/88885c57bd82

这将生成一个 linker-dependencies.xml.gz 文件,解压缩之后可以提取 linker-dependencies.xml .

The linker-dependencies.xml file

该 linker-dependencies.xml 文件是一个 xml,里面包括所有被标记要保存在你的 App 中的 item。您可以在编辑器中打开此文件,例如 Notepad++

The linker-dependencies.xml file is a xml that includes every item that was marked to keep in your application.

对比 linker-dependencies.xml 文件

Now one of the best tools in our toolkit is the ability to generate a linker-dependencies.xml file with each of the linker options enabled:

  • Don’t Link (Small file)
  • Link SDK Assemblies (Medium file)
  • Link All Assemblies (Large file)

然后我们可以使用比较工具,例如:

https://www.scootersoftware.com/download.php(Windows)

https://www.kaleidoscopeapp.com/(Mac)

在每个 linker option之间进行比较,以查看在 assembly 中 linker 之间的区别。这对优化我们的应用程序特别有用。

分析类型

我们还可以使用 Mono 自带的 linkeranalyzer.exe 工具来 analyze types

注意: 这是要配置 PATH 环境变量的

  • C:\Program Files\Mono\lib\mono\4.5 (Windows)
  • /Library/Frameworks/Mono.framework/Versions/{Version}/lib/mono/4.5 (Mac)

You can then use this tool to determine why a type was marked by the linker. For example if we wanted to see why our custom application was marked by the linker, we might first start with the parent type to see dependencies:

linkeranalyzer.exe -t Android.App.Application linker-dependencies.xml.gz

Output:

Loading dependency tree from: linker-dependencies.xml.gz

--- Type dependencies: 'Android.App.Application' --------------------

--- TypeDef:Android.App.Application dependencies --------------------
Dependency #1
        TypeDef:Android.App.Application
        | TypeDef:mayday.Droid.MaydayApplication [2 deps]
        | Assembly:mayday.Droid, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null [1 deps]
        | Other:Mono.Linker.Steps.ResolveFromAssemblyStep

We can then see that mayday.Droid.MaydayApplication is our dependency as it is based on the Application type. Let’s analyze that type now:

linkeranalyzer.exe -t mayday.Droid.MaydayApplication linker-dependencies.xml.gz

Output:

Loading dependency tree from: linker-dependencies.xml.gz

--- Type dependencies: 'mayday.Droid.MaydayApplication' -------------

--- TypeDef:mayday.Droid.MaydayApplication dependencies -------------
Dependency #1
        TypeDef:mayday.Droid.MaydayApplication
        | Assembly:mayday.Droid, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null [1 deps]
        | Other:Mono.Linker.Steps.ResolveFromAssemblyStep
Dependency #2
        TypeDef:mayday.Droid.MaydayApplication
        | TypeDef:mayday.Droid.MaydayApplication/<>c [2 deps]
        | TypeDef:mayday.Droid.MaydayApplication [2 deps]
        | Assembly:mayday.Droid, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null [1 deps]
        | Other:Mono.Linker.Steps.ResolveFromAssemblyStep

Linker 统计分析

您还可以获取程序集中链接内容的统计信息:

linkeranalyzer.exe --stat --verbose linker-dependencies.xml.gz

Output:

Loading dependency tree from: linker-dependencies.xml.gz

--- Statistics ------------------------------------------------------
Vertex type:    Other           count:18
Vertex type:    Assembly        count:3
Vertex type:    TypeDef         count:4606
Vertex type:    Method          count:40101
Vertex type:    Field           count:25680
Vertex type:    ExportedType    count:1251
Vertex type:    MemberRef       count:7672
Vertex type:    Property        count:27
Vertex type:    Module          count:45

Total vertices: 79403

--- Root vertices ---------------------------------------------------
Other:Mono.Linker.Steps.ResolveFromAssemblyStep
Other:Mono.Linker.Steps.ResolveFromXmlStep
Other:Mono.Tuner.SubStepDispatcher
Other:MonoDroid.Tuner.MonoDroidMarkStep

Total root vertices: 4

总结

This is only a surface level of how to use this tool to help diagnose linker issues in your application. This tool is extremely useful for seeing what is ultimately making your linked assemblies.

非常感谢Radek Doulik(https://github.com/radekdoulik)提供精彩的工具和文档!

Further documentation:

https://github.com/mono/mono/tree/master/mcs/tools/linker-analyzer

原文链接: https://www.jon-douglas.com/2017/09/22/linker-analyzer/

参考链接

https://stackoverflow.com/questions/29124520/xamarin-linker-removing-reference-to-third-party-dll
https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker
https://xamarinhelp.com/xamarin-linker/

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,489评论 25赞 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,587评论 2赞 59
  • 一、课程介绍 英文原文:C# is the best language for mobile app develo...
    跟着阿笨一起玩NET阅读 3,882评论 1赞 11
  • 世人追寻花并蒂, 枝繁叶茂不是我。 一枝双生两世愁, 醉卧世间感情深。
    Yoyo月丫弯弯阅读 517评论 1赞 0
  • 楼下的面包店新装修后开张了,路过顺便进去看看。晃了一圈,都是现在流行的手工软欧包,奶酪包之类的。装修前的招牌吐司也...
    阿亚子阅读 115评论 0赞 0

友情链接更多精彩内容