Skip to content

Conversation

ivanpovazan
Copy link
Collaborator

@ivanpovazan ivanpovazan commented Apr 26, 2024

Description

This PR updates the app to .NET9, but more importantly, it makes necessary changes to achieve AOT/trim compatibility making the app compatible with NativeAOT (and in general TrimMode=Full mode) when targeting iOS platforms.

Initial state

The app initially produced 124 warnings (list of warnings: https://gist.github.com/ivanpovazan/8349040163c30f481b4d4f48ceb301a6) , due to the fact that it did not use:

  • Source-generated JSON serialization
  • IQueryAttributable interface instead of QueryPropertyAttribute
  • Typed bindings

When doing the code review, you can follow the commit history as I was fixing each warning group one-by-one.

Current state

  • When using dotnet SDK: 9.0.100-preview.3.24204.13
    and MAUI version: 9.0.0-preview.3.10457/9.0.100-preview.3

    The app produces 9 warnings, where all of the warnings come from the framework it self:

    Recipes net9.0-ios succeeded with 9 warning(s) (34.2s) → bin/Release/net9.0-ios/ios-arm64/Recipes.dll
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(323): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Reflection.TypeInfo.GetDeclaredMethod(String)'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart)' 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.
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(324): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Reflection.TypeInfo.GetDeclaredMethod(String)'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart)' 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.
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(371): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.TypeInfo.GetDeclaredProperty(String)'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart)' 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.
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(390): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Reflection.TypeInfo.GetDeclaredField(String)'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart)' 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.
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(391): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Reflection.TypeInfo.ImplementedInterfaces.get'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.SetupPart(TypeInfo,BindingExpression.BindingExpressionPart)' 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.
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(256): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.GetIndexer(TypeInfo,String,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.TypeInfo.DeclaredProperties.get'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.GetIndexer(TypeInfo,String,String)' 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.
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(269): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.GetIndexer(TypeInfo,String,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.TypeInfo.DeclaredProperties.get'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.GetIndexer(TypeInfo,String,String)' 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.
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(280): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.GetIndexer(TypeInfo,String,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.TypeInfo.DeclaredProperties.get'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.GetIndexer(TypeInfo,String,String)' 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.
    D:\a\_work\1\s\src\Controls\src\Core\BindingExpression.cs(295): Trim analysis warning IL2070: Microsoft.Maui.Controls.BindingExpression.GetIndexer(TypeInfo,String,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Reflection.TypeInfo.ImplementedInterfaces.get'. The parameter '#0' of method 'Microsoft.Maui.Controls.BindingExpression.GetIndexer(TypeInfo,String,String)' 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.
    
  • When using the MAUI at: dotnet/maui@000f24b

    The app produces 1 warning:

    Recipes net9.0-ios succeeded with 1 warning(s) (33.5s) → bin/Release/net9.0-ios/ios-arm64/Recipes.dll
    /Users/ivan/repos/maui/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs(25): Trim analysis warning IL2026: Microsoft.Maui.Controls.Xaml.BindingExtension.Microsoft.Maui.Controls.Xaml.IMarkupExtension<Microsoft.Maui.Controls.BindingBase>.ProvideValue(IServiceProvider): Using member 'Microsoft.Maui.Controls.Binding.Binding(String,BindingMode,IValueConverter,Object,String,Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Using bindings with string paths is not trim safe. Use expression-based binding instead.
    

Testing

I tested the app on iOS device with both runtimes:

  • MonoAOT: dotnet publish -f net9.0-ios -r ios-arm64 -p:TrimmerSingleWarn=false -t:Run
  • NativeAOT: dotnet publish -f net9.0-ios -r ios-arm64 -p:TrimmerSingleWarn=false -p:PublishAot=true -p:PublishAotUsingRuntimePack=true -t:Run

Final note

I marked the PR as DRAFT as I am not that familiar with the code it self, but tried to adapt it to get rid of all the trim warnings, so please take that into account when doing the review.

The goal is to reach 0 warnings and verify the app functionality (manually or through automated UI testing).

Size perf

Recipes.app - iOS .NET9 Mono TrimMode=partial Mono TrimMode=full NativeAOT NativeAOT opt
Size on disk (MB) 41,46 30,3 13,26 11,9
.ipa (MB) 14,27 10,66 5,48 4,96

NOTE: For NativeAOT opt measurements the following optimization options were used -p:StackTraceSupport=false -p:OptimizationPreference=Size


/cc: @PureWeen @simonrozsival

@simonrozsival
Copy link

The last warning should disappear when/if dotnet/maui#21927 is merged

@ivanpovazan
Copy link
Collaborator Author

@simonrozsival thanks for the confirmation. I will retest once that PR gets merged in.

@ivanpovazan ivanpovazan requested review from rachelkang and PureWeen May 6, 2024 08:26
@ivanpovazan
Copy link
Collaborator Author

If there isn't anything alarming with the changes, I would suggest merging this in and as a follow-up we can set up some UI testing on the MAUI repo net9.0 branch to verify the changes automatically.

@ivanpovazan ivanpovazan marked this pull request as ready for review May 6, 2024 10:30
@PureWeen PureWeen merged commit dad7c8e into rachelkang:net9.0 May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants