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
906 stars 19 forks source link

Enable .NET IL trimming to reduce app size #54

Closed JasonWei512 closed 1 year ago

JasonWei512 commented 1 year ago

Solve #14 .

Currently the TrimMode is set to partial, since full is too aggressive. Need some time to test and make sure it doesn't break app's functionality.

Size comparision

App size is reduced by 50%.

Before (MB) After (MB)
Unpackaged 146 65
MSIX 57 29

Note

TaskScheduler is removed from dependencies because it uses COM to interact with Windows Task Scheduler and is incompatible with IL trimming. Instead I use schtasks get the status of the admin startup schedule task.

JasonWei512 commented 1 year ago

Error when clicking "Contact the developer" button in settings page:

2023/03/15 21:13:42.991 | ERROR | Unhandled exception|System.TypeInitializationException: The type initializer for 'System.Management.WmiNetUtilsHelper' threw an exception.
 ---> System.TypeLoadException: Could not load type 'System.StubHelpers.InterfaceMarshaler' from assembly 'System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
   at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointerInternal(IntPtr, Type)
   at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[TDelegate](IntPtr)
   at System.Management.WmiNetUtilsHelper.LoadDelegate[TDelegate](TDelegate&, IntPtr, String)
   at System.Management.WmiNetUtilsHelper..cctor()
   --- End of inner exception stack trace ---
   at System.Management.MTAHelper.IsNoContextMTA()
   at System.Management.MTAHelper.CreateInMTA(Type)
   at System.Management.ManagementPath.CreateWbemPath(String)
   at System.Management.ManagementObjectSearcher..ctor(String, String, EnumerationOptions)
   at Hardware.Info.Windows.HardwareInfoRetrieval.GetOs()
   at Hardware.Info.HardwareInfo..ctor(Boolean useAsteriskInWMI, Nullable`1 timeoutInWMI)
   at EnergyStarX.ViewModels.SettingsViewModel.<>c.<.ctor>b__40_0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper, Boolean)
   at System.Lazy`1.CreateValue()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread, ExecutionContext, ContextCallback, Object)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread, ExecutionContext, ContextCallback, Object)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task&, Thread )
--- End of stack trace from previous location ---
   at EnergyStarX.ViewModels.SettingsViewModel.ContactTheDeveloper()
   at CommunityToolkit.Mvvm.Input.AsyncRelayCommand.AwaitAndThrowIfFailed(Task)
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object)
   at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0()
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32)
   at ABI.Windows.ApplicationModel.Core.IUnhandledErrorMethods.Propagate(IObjectReference)
   at Microsoft.AppCenter.Utils.ApplicationLifecycleHelperWinUI.<.ctor>b__0_3(Object sender, UnhandledErrorDetectedEventArgs eventArgs)
JasonWei512 commented 1 year ago

Hardware.Info uses System.Management, which is incompatible with trimming. Related: https://github.com/dotnet/runtime/issues/61960

JasonWei512 commented 1 year ago

Replaced Hardware.Info with WmiLight for fetching hardware info.

JasonWei512 commented 1 year ago

Visual Studio App Center SDK uses Newtonsoft.Json, which is not trimming friendly. Not sure if telemetry will be affected.

JasonWei512 commented 1 year ago

Reverted because it breaks App Center telemetry. https://github.com/JasonWei512/EnergyStarX/issues/14#issuecomment-1477142429