-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.NET 9: Make .NET MAUI trimmer-friendly #18658
Comments
/cc: @simonrozsival |
Note: on NativeAOT these APIs should also produce a build/publish-time trim/AOT warning |
A few have tried: #16861 That's the 9th issue when sorting issues by number of comments: https://github.com/dotnet/maui/issues?q=is%3Aissue+is%3Aopen+sort%3Acomments-desc |
Would it be possible to write a source generator that fully replaces the bindings with code that the linker can reason about especially in if (source is ViewModel vm)
{
if (vm.Some is InnerViewModel i1vm)
{
if (i1vm.PropertyPath is SecondInnerViewModel i2vm)
{
//Obviously this goes on ...
}
}
} And I know I'm skipping over a lot of stuff here like hooking into intermediate INPC and converters and formatting and fallback values, etc... but if the source generated code is written in such a way that it's generically parameterized all the way down the linker can follow it. |
Either way, I think getting |
One issue that is a problem already today is, if you reference a 3rd party library that already did the work to support trimming, binding to any properties in that library is extremely error prone, because the binding expressions won't ensure the properties are preserved. |
@jonathanpeppers we hit some bindings which produce warnings even in the template app, but they're all defined in C# and not in XAML which should make it easier to deal with (for example in BaseShellItem). The |
Bindings in code like https://github.com/dotnet/maui/blob/main/src/Controls/src/Core/TypedBinding.cs#L72 This is a form of compiled bindings that can be used in some cases: That might be better in general, it replaces System.Reflection calls with just an anonymous method/lambda. But I bet it doesn't cover all cases like |
…19194) This introduces two new integration tests which are testing MAUI compatibility with NativeAOT on iOS. The added tests will enable us to catch early any regression in compatibility between MAUI iOS and NativeAOT, but also to guide us forward as we progress with: #18658 ### Changes - Introduced new test cases: 1. `PublishNativeAOT` - tests publishing a template MAUI iOS app with NativeAOT 2. `PublishNativeAOTCheckWarnings` - tests the generated warnings according to #19194 (comment) - Created a new utility method `DotnetInternal.ConstructBuildArgs` with a common logic for setting up build arguments which is shared between tests calling `Build` and `Publish` targets - Added `BuildWarningsUtilities.cs` which encapsulates all the logic for parsing, storing and comparing build warnings NOTE: This should be an initial set of tests verifying our progress in the effort of making MAUI trim-compatible (with NativeAOT). The set can and will be expanded to testing on device, covering support for other iOS-platforms with NativeAOT etc, as we progress.
…lyzers in Controls.Core.csproj (#21621) Contributes to #18658 We can use `[FeatureSwitchDefinition("...")]` attributes instead of `ILLink.Substitutions.xml` since dotnet/runtime#99338 has been merged and has flown into MAUI. * Use new FeatureSwitchDefinition attributes instead of ILLink substitutions * Update comment * Update Controls * Update Compatibility * Disable analyzers in Controls.Core on Tizen * Skip warning on Windows * Add RUC attributes to NativeBindingExtensions * Add RUC attributes to NativeBindingService * Add comment explaining why DoNothing moved from Binding to MultiBinding
We believe this is now "complete" in .NET 9 Preview 7, as of reaching:
We also opted into
In the future, we may consider doing this in the We have new alternatives for C# data-binding for NativeAOT-based apps, such as:
And lastly, the following feature-switches are automatically toggled for |
Congrats team!! It's been a long road. |
@jonathanpeppers is there any guidance for library owners to make their libraries I have quite a few libraries and not sure what I need to do or how to test it. |
Check out: |
Description
In .NET 6/7 timeframe, we were able to make a few .NET MAUI assemblies trimmable:
Essentials
Compatibility
The remaining assemblies (see #1962) were not able to easily become trimmable due to some of the dynamic features of .NET MAUI.
As we've shipped the NativeAOT experiment for iOS in .NET 8, this makes trimming even more important:
IsTrimmable=true
, and trims everything.Current Trimming Behavior
For .NET MAUI apps running on Mono (iOS, Android, Catalyst), we default to
TrimMode=partial
. This is the same as the "SDK-only" trimming mode that we got from the Xamarin days.Current behavior is:
IsTrimmable=true
are trimmedMono.Android.dll
)Microsoft.iOS.dll
)We also suppress most trimmer warnings in this mode, as the warnings shown for .NET MAUI, Android assemblies, etc. are not currently actionable.
Thus, most user code and third-party libraries are not trimmed at all. We get a reasonable small application, with a lot of IL removed, but not all that is possible.
.NET 6/7/8 apps can opt into "full trimming" with the likelihood of running into runtime crashes. Few customers (if any) are using this today.
.NET Ecosystem Concerns
There are many third-party .NET libraries on NuGet, etc. that are not implemented with "trimmability" in mind. Existing .NET MAUI applications are likely using these libraries.
How would we get large adoption of NativeAOT, if we don't get existing customers to opt into this setting? We need to find out what libraries are problematic so library authors can react.
Trimmability for Everyone
I propose as part of the "trimmable" effort for .NET MAUI, that we also adopt full trimming for all platforms running on Mono: this includes Android, iOS, MacCatalyst. Existing customers will also get the benefit of smaller app sizes.
If WindowsAppSDK (by luck?) also works without issue, we can include Windows platforms as well. This issue appears to have been solved, so it might work: microsoft/CsWinRT#373
Goal for .NET 9
Project templates (
dotnet new maui
,android
,ios
, etc.) will opt into:When in this mode, we should make sure all trimmer warnings are visible for users to react to. Project templates should show 0 warnings.
Greenfield apps will have an easier time adopting full trimming, as they can react individually as NuGet packages are added, etc. The setting can also be disabled by simply removing a line from the
.csproj
.Existing projects can also "opt into" full trimming, and find out if it works for them.
(Public) API Changes
Problematic MAUI APIs include:
Dynamic XAML
Bindings
The idea is we'd leave these APIs intact. The behavior in each case would be something like:
Usage Scenarios
Up to a 50% reduction in .NET MAUI app size?
@ivanpovazan has some examples here.
Most of the size improvements were merely from opting into full-trimming. NativeAOT may also make apps smaller, but trimming is the major win for app size.
Backward Compatibility
Existing .NET MAUI apps should not see a change in behavior.
Difficulty
High
The text was updated successfully, but these errors were encountered: