build(desktop): 默认开启裁剪,体积从 108 MB 降到 51 MB

桌面端以前发布出来是 107.8 MB(单文件 exe 90.3 MB),其中绝大部分是随包携带的
.NET 运行时里用不到的部分。这次让 `dotnet publish -r <rid>` 默认打开裁剪:

    默认(裁剪)      51.3 MB   exe 33.9 MB
    加压缩            36.9 MB   exe 19.5 MB
    -p:PublishTrimmed=false   107.7 MB   exe 90.3 MB(和以前一样)

选择不裁剪的方式就是发布时加 `-p:PublishTrimmed=false`,输出与改动前完全一致。
普通 `dotnet build`(不带 RID)不受影响,因为裁剪需要 RID 才有意义。

裁剪在这里不是免费的,新增的 ILLink.Descriptors.xml 就是为了付这笔账。有两处
东西是靠反射拿到的,直接裁剪会安静地消失:

1. DI 容器。AutoDependencyInjectExtensions 用 `Assembly.GetTypes()` 扫描
   ZonyLrcTools.Common,把实现 ITransientDependency / ISingletonDependency 的
   类型注册进去。裁剪器看不到这些类型被实例化,会把它们删掉,然后程序能启动、
   一进页面就抛 "No service for type ... has been registered"。

2. Avalonia 的自动化 peer。Windows UI Automation 是按名字请求节点的,裁剪掉之后
   窗口照常渲染,但 UIA 树是空的 —— 读屏软件、讲述人、以及任何自动化脚本看到的
   都是一个空壳。

第 2 点踩过一次坑值得记下来:只保留 Avalonia.Controls 不够。第一版裁出来的 exe
能启动、界面完整,但 UIA 枚举返回 0 个元素。真正的 UIA provider 在 Win32 后端里,
通过 COM 激活而不是静态引用,所以要额外保留 Avalonia.Win32 和 Avalonia.Base。
补上之后 UIA 树恢复成 39 个元素,与不裁剪版本完全一致。

TrimMode 用 partial:只丢弃整个未被引用的程序集,不裁剪方法体。体积收益主要来自
框架,这样做不会去赌"某个方法没被反射用到"。

另外 ILLink.Descriptors.xml 里写的是程序集名不是包名 —— MusicDecrypto.Library
这个包发出的程序集叫 libmusicdecrypto,写包名会得到 IL2007 警告且不生效。

CI 的 Verify 步骤加了一条 90 MB 上限断言:裁剪可以被环境变量或命令行静默关掉,
而关掉之后唯一的症状就是包大了 55 MB,没人会注意到。用宽阈值而不是精确值是给
框架版本升级留余量。

实测(win-x64 自包含单文件):

    项目            总大小    主 exe    UIA 元素   .kgg 解密   歌词下载
    不裁剪          107.8 MB  90.3 MB   39        3/3         3/3
    裁剪             51.3 MB  33.9 MB   39        3/3         3/3
    裁剪 + 压缩      36.9 MB  19.5 MB   39        3/3         —
    Trimmed=false   107.7 MB  90.3 MB   —         —           —

.kgg 解密产物逐字节相同(3371722 / 51650043 / 48354702 字节);设置写回仍然只改
config.yaml 里受影响的那几行,60 行、35 条注释全保留;五个页面在裁剪版下截图与
不裁剪版无差异。启动耗时略慢:1048 ms → 1223 ms,因为裁剪后的程序集元数据要从
打包数据里反序列化;换来的是下载体积少 56 MB。

这段代码由 AI 辅助编写,发布前已在本机逐项跑过上面这张表。
This commit is contained in:
aL1CesT committed 2026-11-09 05:02:46 +08:00
1 parent 76d8719659
commit 7379571e2d
3 files changed
+104 -1

No files matched your search

@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Root descriptor for the trimmer, used only when PublishTrimmed is on.
Everything listed here is code the trimmer cannot see being used, because it is
reached through reflection at runtime rather than through a static call.
See the comments in ZonyLrcTools.Desktop.csproj for why each assembly is here.
-->
<linker>
<!--
AutoDependencyInjectExtensions scans this assembly with Assembly.GetTypes() and registers
whatever implements ITransientDependency / ISingletonDependency. The trimmer cannot prove
those 20+ types are ever instantiated, so without this root a trimmed build starts fine and
then throws "No service for type ... has been registered" the moment a page asks for one.
-->
<assembly fullname="ZonyLrcTools.Common" preserve="all" />
<!--
Same reason, plus System.Text.Json deserialisation of NetEaseSession / KugouDatabaseSetting
in NetEaseSessionStore and KugouDatabaseStore: the trimmer would drop the setters it cannot
see being called.
-->
<assembly fullname="ZonyLrcTools.Desktop" preserve="all" />
<!--
Avalonia resolves automation peers by name when Windows UI Automation asks for a node.
Trimming removes those peer types because nothing statically references them, and the visible
symptom is a running window whose UIA tree is empty — screen readers, Narrator and every
UI-automation script see a blank shell. Preserving the peer namespace keeps accessibility and
testability intact.
-->
<assembly fullname="Avalonia.Controls" preserve="all" />
<assembly fullname="Avalonia.Controls.DataGrid" preserve="all" />
<!--
The UIA provider that Windows talks to lives in the Win32 backend, and it is reached through
COM activation rather than through a static reference, so it needs the same treatment as the
peers above. Without this the window still renders but Windows UI Automation enumerates zero
children, which silently breaks Narrator and every automation script.
-->
<assembly fullname="Avalonia.Win32" preserve="all" />
<assembly fullname="Avalonia.Base" preserve="all" />
<!--
The remaining libraries all build their behaviour on reflection over types the trimmer cannot
follow: YamlDotNet walks GlobalOptions by property name, Newtonsoft.Json (used by Common's
text providers) maps JSON onto model classes, TagLibSharp reads audio tags, MusicDecrypto
creates decryptors from a factory, and SQLitePCLRaw loads its provider by reflection.
These are small (a few hundred KB each) next to the framework assemblies that trimming really
does remove, so rooting them costs little and removes a whole class of "works until you use it"
failures.
-->
<assembly fullname="YamlDotNet" preserve="all" />
<assembly fullname="Newtonsoft.Json" preserve="all" />
<assembly fullname="TagLibSharp" preserve="all" />
<!-- The NuGet package is MusicDecrypto.Library, but the assembly it ships is libmusicdecrypto. -->
<assembly fullname="libmusicdecrypto" preserve="all" />
<assembly fullname="QRCoder" preserve="all" />
<assembly fullname="SQLitePCLRaw.core" preserve="all" />
<assembly fullname="SQLitePCLRaw.batteries_v2" preserve="all" />
<assembly fullname="SQLitePCLRaw.provider.e_sqlite3" preserve="all" />
</linker>
@@ -34,6 +34,39 @@
<PackageReference Include="QRCoder" />
</ItemGroup>
<!--
Trimming is ON by default for RID-specific publishes, taking the self-contained win-x64
output from ~108 MB to ~51 MB. Pass -p:PublishTrimmed=false to get the untrimmed build.
Trimming is not free here, which is why ILLink.Descriptors.xml exists. Two things in this
app are resolved by reflection and would silently vanish from a naive trim:
- the DI container, populated by GetTypes() over ZonyLrcTools.Common, and
- Avalonia's automation peers, which is what Windows UI Automation asks for by name.
Lose the first and the app starts then throws "No service for type ... has been registered"
the moment a page is opened; lose the second and the window renders fine while its UIA tree
is empty, taking Narrator and every automation script with it. Both are rooted in that file.
TrimMode=partial keeps method bodies intact and only drops whole unreferenced assemblies,
which is the conservative choice: the size win comes from the framework, not from risking
half an assembly that something reflects over.
The condition also keeps plain `dotnet build` (no RID) unaffected: trimming needs a runtime
identifier, so there is nothing to opt into there.
-->
<PropertyGroup Condition="'$(RuntimeIdentifier)' != '' AND '$(PublishTrimmed)' == ''">
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
<PropertyGroup Condition="'$(PublishTrimmed)' == 'true'">
<TrimMode>partial</TrimMode>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
</PropertyGroup>
<ItemGroup Condition="'$(PublishTrimmed)' == 'true'">
<TrimmerRootDescriptor Include="ILLink.Descriptors.xml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZonyLrcTools.Common\ZonyLrcTools.Common.csproj" />
</ItemGroup>