前言
哈哈,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 環境變量配置參考:http://www.lxweimin.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/