JasonWei512 / EnergyStarX

🔋 Improve your Windows 11 device's battery life. A WinUI 3 GUI for https://github.com/imbushuo/EnergyStar.
https://www.microsoft.com/store/productId/9NF7JTB3B17P
GNU General Public License v3.0
952 stars 18 forks source link

Enable IL trimming to reduce app size / 启用裁剪以减小应用体积 #14

Open JasonWei512 opened 1 year ago

JasonWei512 commented 1 year ago

The problem you want to solve / 你想要解决的问题

App size is too big (150 MB). 应用大小太大了(150 MB)。

Possible solution / 可能的解决方法

Windows App SDK 1.2 now supports .NET IL trimming, which can help reduce binary size. https://learn.microsoft.com/windows/apps/windows-app-sdk/stable-channel#trimming-for-apps-developed-with-net

However when I enabled trimming, this app will crash on launch. https://github.com/microsoft/WindowsAppSDK/issues/2478 https://github.com/microsoft/CsWinRT/issues/373


Windows App SDK 1.2 现在支持 .NET IL 裁剪了,这可以帮助减小二进制大小。 https://learn.microsoft.com/windows/apps/windows-app-sdk/stable-channel#trimming-for-apps-developed-with-net

但是当我启用裁剪时,应用启动时会崩溃。 https://github.com/microsoft/WindowsAppSDK/issues/2478 https://github.com/microsoft/CsWinRT/issues/373

Additional context / 其他信息

There's also a bug with TeachingTip in Windows App SDK 1.2. https://github.com/microsoft/microsoft-ui-xaml/issues/7937 https://github.com/microsoft/microsoft-ui-xaml/issues/8003

Windows App SDK 1.2 的 TeachingTip 也有 Bug。 https://github.com/microsoft/microsoft-ui-xaml/issues/7937 https://github.com/microsoft/microsoft-ui-xaml/issues/8003

JasonWei512 commented 1 year ago
Build log ``` 已启动重新生成... 1>------ 已启动全部重新生成: 项目: EnergyStarX.Core, 配置: Release x64 ------ 已还原 D:\users\USER\Code\EnergyStarX\EnergyStarX.Core\EnergyStarX.Core.csproj (用时 1 ms)。 已还原 D:\users\USER\Code\EnergyStarX\EnergyStarX\EnergyStarX.csproj (用时 226 ms)。 1>EnergyStarX.Core -> D:\users\USER\Code\EnergyStarX\EnergyStarX.Core\bin\x64\Release\net7.0\EnergyStarX.Core.dll 2>------ 已启动全部重新生成: 项目: EnergyStarX, 配置: Release x64 ------ 2>D:\users\USER\Code\EnergyStarX\EnergyStarX\App.xaml.cs(91,13,91,117): warning IL2026: Using member 'Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure(IServiceCollection, IConfiguration)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. TOptions's dependent types may have their members trimmed. Ensure all required members are preserved. 2>EnergyStarX -> D:\users\USER\Code\EnergyStarX\EnergyStarX\bin\x64\Release\net7.0-windows10.0.22000.0\win10-x64\EnergyStarX.dll 2>正在优化程序集以调整大小。此过程可能需要一段时间。 2>D:\users\USER\Code\EnergyStarX\EnergyStarX\App.xaml.cs(91,13): Trim analysis warning IL2026: EnergyStarX.App.<>c.<.ctor>b__9_0(HostBuilderContext, IServiceCollection): Using member 'Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure(IServiceCollection, IConfiguration)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. TOptions's dependent types may have their members trimmed. Ensure all required members are preserved. 2>D:\users\USER\Code\EnergyStarX\EnergyStarX\Helpers\FrameExtensions.cs(7,65): Trim analysis warning IL2075: EnergyStarX.Helpers.FrameExtensions.GetPageViewModel(Frame): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. 2>D:\users\USER\Code\EnergyStarX\EnergyStarX\obj\x64\Release\net7.0-windows10.0.22000.0\win10-x64\XamlTypeInfo.g.cs(5061,13): Trim analysis warning IL2059: EnergyStarX.EnergyStarX_XamlTypeInfo.XamlUserType.RunInitializer(): Unrecognized value passed to the parameter 'type' of method 'System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(RuntimeTypeHandle)'. It's not possible to guarantee the availability of the target static constructor. 2>D:\users\USER\Code\EnergyStarX\EnergyStarX.Core\Helpers\Json.cs(7,83): Trim analysis warning IL2026: EnergyStarX.Core.Helpers.Json.<>c__DisplayClass0_0.b__0(): Using member 'System.Text.Json.JsonSerializer.Deserialize(String, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. 2>D:\users\USER\Code\EnergyStarX\EnergyStarX.Core\Helpers\Json.cs(9,86): Trim analysis warning IL2026: EnergyStarX.Core.Helpers.Json.<>c__DisplayClass1_0.b__0(): Using member 'System.Text.Json.JsonSerializer.Serialize(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. 2>D:\users\USER\Code\EnergyStarX\EnergyStarX.Core\Services\FileService.cs(15,13): Trim analysis warning IL2026: EnergyStarX.Core.Services.FileService.Read(String, String): Using member 'System.Text.Json.JsonSerializer.Deserialize(String, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. 2>D:\users\USER\Code\EnergyStarX\EnergyStarX.Core\Services\FileService.cs(28,9): Trim analysis warning IL2026: EnergyStarX.Core.Services.FileService.Save(String, String, T): Using member 'System.Text.Json.JsonSerializer.Serialize(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. 2>ILLink : Trim analysis warning IL2026: System.Data.DataSet.System.Xml.Serialization.IXmlSerializable.GetSchema(): Using member 'System.Data.DataSet.WriteXmlSchema(DataSet, XmlWriter)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DataSet.GetSchema uses TypeDescriptor and XmlSerialization underneath which are not trimming safe. Members from serialized types may be trimmed if not referenced directly. 2>ILLink : Trim analysis warning IL2026: System.Data.DataSet.System.Xml.Serialization.IXmlSerializable.ReadXml(XmlReader): Using member 'System.Data.DataSet.ReadXmlSerializableInternal(XmlReader)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DataSet.ReadXml uses XmlSerialization underneath which is not trimming safe. Members from serialized types may be trimmed if not referenced directly. 2>ILLink : Trim analysis warning IL2026: System.Data.DataSet.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter): Using member 'System.Data.DataSet.WriteXmlInternal(XmlWriter)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DataSet.WriteXml uses XmlSerialization underneath which is not trimming safe. Members from serialized types may be trimmed if not referenced directly. 2>ILLink : Trim analysis warning IL2026: System.Data.DataTable.System.Xml.Serialization.IXmlSerializable.GetSchema(): Using member 'System.Data.DataTable.GetXmlSchema()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DataTable.GetSchema uses TypeDescriptor and XmlSerialization underneath which are not trimming safe. Members from serialized types may be trimmed if not referenced directly. 2>ILLink : Trim analysis warning IL2026: System.Data.DataTable.System.Xml.Serialization.IXmlSerializable.ReadXml(XmlReader): Using member 'System.Data.DataTable.ReadXmlSerializableInternal(XmlReader)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DataTable.ReadXml uses XmlSerialization underneath which is not trimming safe. Members from serialized types may be trimmed if not referenced directly. 2>ILLink : Trim analysis warning IL2026: System.Data.DataTable.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter): Using member 'System.Data.DataTable.WriteXmlInternal(XmlWriter)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DataTable.WriteXml uses XmlSerialization underneath which is not trimming safe. Members from serialized types may be trimmed if not referenced directly. 2>ILLink : Trim analysis warning IL2026: WinRT.TypeExtensions.<>c.b__14_0(Type): Using member 'System.Reflection.Assembly.GetType(String, Boolean)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed. 2>ILLink : Trim analysis warning IL2026: WinRT.Projections..cctor(): Using member 'System.Delegate.Delegate(Object, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The target method might be removed. 2>ILLink : Trim analysis warning IL2026: WinRT.Projections..cctor(): Using member 'System.Delegate.CreateDelegate(Type, Object, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The target method might be removed. 2>ILLink : Trim analysis warning IL2026: WinRT.Projections..cctor(): Using member 'System.Delegate.CreateDelegate(Type, Object, String, Boolean)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The target method might be removed. 2>ILLink : Trim analysis warning IL2026: WinRT.Projections..cctor(): Using member 'System.MulticastDelegate.MulticastDelegate(Object, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The target method might be removed. 2>ILLink : Trim analysis warning IL2026: WinRT.Projections..cctor(): Using member 'System.Delegate.CreateDelegate(Type, Object, String, Boolean, Boolean)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The target method might be removed. 2>C:\Users\USER\.nuget\packages\castle.core\4.4.1\lib\netstandard1.5\Castle.Core.dll : warning IL2104: Assembly 'Castle.Core' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\communitytoolkit.common\8.0.0\lib\net6.0\CommunityToolkit.Common.dll : warning IL2104: Assembly 'CommunityToolkit.Common' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\communitytoolkit.winui.ui.controls.core\7.1.2\lib\net5.0-windows10.0.18362\CommunityToolkit.WinUI.UI.Controls.Core.dll : warning IL2104: Assembly 'CommunityToolkit.WinUI.UI.Controls.Core' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\communitytoolkit.winui.ui.controls.markdown\7.1.2\lib\net5.0-windows10.0.18362\CommunityToolkit.WinUI.UI.Controls.Markdown.dll : warning IL2104: Assembly 'CommunityToolkit.WinUI.UI.Controls.Markdown' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\communitytoolkit.winui.ui\7.1.2\lib\net5.0-windows10.0.18362\CommunityToolkit.WinUI.UI.dll : warning IL2104: Assembly 'CommunityToolkit.WinUI.UI' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\communitytoolkit.winui\7.1.2\lib\net5.0-windows10.0.18362\CommunityToolkit.WinUI.dll : warning IL2104: Assembly 'CommunityToolkit.WinUI' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\flurl\3.0.6\lib\netstandard2.0\Flurl.dll : warning IL2104: Assembly 'Flurl' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\microsoft.appcenter\5.0.0\lib\net6.0-windows10.0.17763.0\Microsoft.AppCenter.dll : warning IL2104: Assembly 'Microsoft.AppCenter' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\microsoft.windows.sdk.net.ref\10.0.22000.27\lib\net6.0\Microsoft.Windows.SDK.NET.dll : warning IL2104: Assembly 'Microsoft.Windows.SDK.NET' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\microsoft.windows.sdk.net.ref\10.0.22000.27\lib\net6.0\WinRT.Runtime.dll : warning IL2104: Assembly 'WinRT.Runtime' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\microsoft.windowsappsdk\1.2.221116.1\lib\net6.0-windows10.0.18362.0\Microsoft.Windows.Widgets.Projection.dll : warning IL2104: Assembly 'Microsoft.Windows.Widgets.Projection' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\microsoft.xaml.behaviors.winui.managed\2.0.9\lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.dll : warning IL2104: Assembly 'Microsoft.Xaml.Interactions' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\newtonsoft.json\13.0.1\lib\netstandard2.0\Newtonsoft.Json.dll : warning IL2104: Assembly 'Newtonsoft.Json' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\nlog\5.1.0\lib\netstandard2.0\NLog.dll : warning IL2104: Assembly 'NLog' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\nucs.jsonsettings.autosave\2.0.0-alpha7\lib\netstandard2.0\JsonSettings.Autosave.dll : warning IL2104: Assembly 'JsonSettings.Autosave' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\nucs.jsonsettings\2.0.0-alpha7\lib\netstandard2.0\JsonSettings.dll : warning IL2104: Assembly 'JsonSettings' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\settingsui\2.2.1\lib\net7.0-windows10.0.19041\SettingsUI.dll : warning IL2104: Assembly 'SettingsUI' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\system.codedom\6.0.0\lib\net6.0\System.CodeDom.dll : warning IL2104: Assembly 'System.CodeDom' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\system.configuration.configurationmanager\6.0.0\lib\net6.0\System.Configuration.ConfigurationManager.dll : warning IL2104: Assembly 'System.Configuration.ConfigurationManager' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\system.management\6.0.0\runtimes\win\lib\net6.0\System.Management.dll : warning IL2104: Assembly 'System.Management' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>C:\Users\USER\.nuget\packages\winuiex\1.8.0\lib\net5.0-windows10.0.18362\WinUIEx.dll : warning IL2104: Assembly 'WinUIEx' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>已完成生成项目“EnergyStarX.csproj”的操作。 ========== “全部重新生成”: 2 成功,0 失败,0已跳过 ========== ========== 占用时间 01:25.546 ========== ```
hez2010 commented 1 year ago

CommunityToolkit is not compatible with trimming. Either removing usages to CommunityToolkit or rooting all assemblies from CommunityToolkit.

JasonWei512 commented 1 year ago

TaskScheduler is not compatible with IL trimming (because of COM™): https://github.com/dahall/TaskScheduler/issues/948

Energy Star X uses TaskScheduler to get the status of the admin startup task from Windows Task Scheduler. (MSIX's StartupTask doesn't allow you to run an app at startup AS ADMIN, so I have to use a schedule task for this.)

If I remove TaskScheduler from dependencies and set TrimMode to partial, then this app can run. No TrimmerRootAssembly needed.

Some experiments on optimize/IL-trimming branch:

Before (MB) After (MB)
Unpackaged 146 65
MSIX 57 29
Log ``` 23:17:00:822 引发的异常:“System.TypeLoadException”(位于 System.Private.CoreLib.dll 中) 23:17:00:822 引发的异常:“System.TypeInitializationException”(位于 System.Management.dll 中) 23:17:00:822 引发的异常:“System.TypeInitializationException”(位于 System.Management.dll 中) 23:17:00:822 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:00:822 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:00:822 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:00:822 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:00:822 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:00:822 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:03:073 引发的异常:“System.TypeInitializationException”(位于 System.Management.dll 中) 23:17:03:073 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:03:073 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:03:073 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) ...... 23:17:11:072 引发的异常:“System.NotSupportedException”(位于 Microsoft.Win32.TaskScheduler.dll 中) 23:17:11:072 引发的异常:“System.TypeInitializationException”(位于 Microsoft.Win32.TaskScheduler.dll 中) 23:17:11:072 引发的异常:“System.TypeInitializationException”(位于 System.Private.CoreLib.dll 中) 23:17:15:840 引发的异常:“System.Threading.Tasks.TaskCanceledException”(位于 System.Private.CoreLib.dll 中) ```
JasonWei512 commented 1 year ago

Looks like IL trimming breaks Visual Studio App Center SDK. https://github.com/microsoft/appcenter/issues/2517

VShawn commented 1 year ago

似乎trimming对带UI的.NET程序的支持性都不佳。