当前位置: 首页 > 工具软件 > GitHub Linker > 使用案例 >

Linker 分析器

章誉
2023-12-01

前言

哈哈,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之间进行比较,以查看在 assemblylinker 之间的区别。这对优化我们的应用程序特别有用。

分析类型

我们还可以使用 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/

 类似资料: