[Proposal] Add Support for NativeAOT
Feature name
Add Support for NativeAOT
Link to discussion
https://github.com/CommunityToolkit/Maui/issues/1460#issuecomment-1804383350
Progress tracker
- [ ] Android Implementation
- [ ] iOS Implementation
- [ ] MacCatalyst Implementation
- [ ] Windows Implementation
- [ ] Tizen Implementation
- [ ] Unit Tests
- [ ] Samples
- [ ] Documentation
Summary
This Proposal seeks to make all .NET MAUI CommunityToolkit libraries safe to use with NativeAOT.
This requires the library to be safe to use with the .NET Trimmer:
The
IsTrimmableproperty defaults to true when configuring a project as AOT-compatible with <IsAotCompatible>true</IsAotCompatible>
https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming?pivots=dotnet-8-0#enable-project-specific-trimming
Motivation
In .NET 9, .NET MAUI will add NativeAOT support: https://github.com/dotnet/maui/issues/18658.
This give us approximately one year until .NET 9 releases in November 2024 to accomplish this Proposal.
Detailed Design
Directory.Build.props
We will make two changes to Directory.Build.proprs:
- Replace the
<IsTrimmable>falseproperty with<IsAotCompatible>true
- Add
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<IsAotCompatible>true</IsAotCompatible>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
Resolve Trim Warnings
Once <EnableTrimAnalyzer> has been enabled, we will then need to resolve every Trimmer warning. We can use the Microsoft docs to help us accomplish it: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming?pivots=dotnet-8-0#resolve-trim-warnings
Usage Syntax
To use .NET Trimming, the developer will add <TrimMode>full</TrimMode> to their CSProj. The .NET MAUI team will be adding <TrimMode>full</TrimMode> to the .NET MAUI Templates in .NET 9: https://github.com/dotnet/maui/issues/18658
Drawbacks
We may not fix all of the bugs introduced by the Trimmer in our first PR that adds NativeAOT Compatibility, and that's ok.
As new Trimmer-related bugs are reported, we will prioritize them and provide hot fixes.
Alternatives
There are no alternatives.
Now that .NET MAUI will be adding support for NativeAOT in .NET 9, every .NET MAUI developer is required to ensure their library is Trimmer safe.
Unresolved Questions
No response
Additional Information
How to make libraries compatible with native AOT: https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/
@brminnick you can use this as reference too:
- https://github.com/dotnet/maui/pull/3161
Hello! What is the state of this proposal? Is somebody working on it already or could I help getting with this issue? I tried enabling the analyzers and seeing how much code needs fixing, and it seems that the codebase is mostly fine (https://github.com/CommunityToolkit/Maui/compare/main...simonrozsival:CommunityToolkit.Maui:mark-relevant-assemblies-aot-compatible).
I have some comments regarding this issue:
- I'm not sure it is a good idea to place the
<IsAotCompatible>true</IsAotCompatible>directly into Directory.Build.props. The trimming analyzers aren't compatible withnetstandard2.*projects (all analyzers and source generators) and they also probably shouldn't be enabled for unit test projects. It might be better to add that property to only the projects where it should be really enabled. - When
IsAotCompatibleis set, the trim analyzers and all the other analyzers are enabled by default, there's no need to enable the analyzers manually. - The current default for
TrimModein MAUI apps ispartialand so all assemblies that are marked asIsTrimmablewill be trimmed. Once the community toolkit is trimmable, all apps will benefit from it, not just NativeAOT apps 😄 On the other hand, this might break code which accesses some internal types from the toolkit via reflection. This is why we're not marking MAUI itself as trim- or AOT-compatible, but we're just enabling the analyzers for now (see https://github.com/dotnet/maui/pull/21076#discussion_r1516064057). I'm not sure if this applies to the community toolkit though. NativeAOT doesn't care what the value ofTrimModeis. It will trim any assembly, even if it is not marked as trimmable or AOT compatible.
Thanks @simonrozsival! Great write up 💯
My plan was to begin working on this when the .NET 9 RC debuts since MAUI isn't adding AOT/Trimming until .NET 9. But, it sounds like you've already gotten a head start on it!
I'd love it if you could submit a draft PR with the work you've done so far! I think the best plan will be to keep your PR open (unmerged) until the .NET 9 RC so that we can continue to iterate on it as the MAUI team continues to iterate on their AOT/Trimming support.
@brminnick sounds good, I'll open a draft PR