Skip to content

[Bug] Fluent UI Blazor components cause Trimming/AOT failures in .NET MAUI Android Release Builds #4004

@true-perfect-code

Description

@true-perfect-code

🐛 Bug Report

💻 Repro or Code Sample

  1. Create a new .NET MAUI Blazor project (e.g., using dotnet new maui-blazor in .NET 9.0).
  2. Integrate the Microsoft.FluentUI.AspNetCore.Components NuGet package.
  3. Add a Fluent UI Blazor component to a page (e.g., a simple usage of a component that might internally rely on DesignTokens.Swatch or similar complex types, or even a form that triggers serialization/deserialization logic).
  4. Configure the project for a Release build for Android.
    • In Visual Studio, navigate to Project -> Properties -> Android Build.
    • Ensure that AOT Compilation is enabled (which is the default for Release builds and, as observed, requires Trimming to be enabled).
    • Ensure Trimming is enabled (also default for Release builds).
  5. Build the application and create an Android App Bundle (.aab).
  6. Deploy the .aab to an Android device (either directly or via the Google Play Console internal testing track).
  7. Attempt to interact with the UI section or functionality that utilizes the Fluent UI Blazor component or initiate a user login process that interacts with this UI.

🤔 Expected Behavior

The application should run successfully in Release mode with AOT Compilation and Trimming enabled, and all Fluent UI Blazor components should function correctly without runtime errors. The login process should complete successfully.

😯 Current Behavior

The application crashes or displays a critical runtime error related to missing metadata for Fluent UI Blazor components, specifically Microsoft.FluentUI.AspNetCore.Components.DesignTokens.Swatch. This error prevents core application functionality, such as user login, from completing.

Full Error Message (Screenshot):
Image

Error Details:

Error
Description of error:
ConstructorContainsNullParameterNames,
Microsoft.FluentUI.AspNetCore.Components.DesignTokens.Swatch
SerializationNotSupportedParentType, System.Object Path: $., Class:
xyxy.Shared.Pages.BasisViewModel
+d_91, xyxy.Shared, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null

This error consistently occurs when AOT Compilation and Trimming are active in the Release build.

💁 Possible Solution

The error message (ConstructorContainsNullParameterNames, SerializationNotSupportedParentType) strongly indicates that the .NET Linker (Trimmer) is removing essential metadata (like constructors or serialization information) from Microsoft.FluentUI.AspNetCore.Components.DesignTokens.Swatch or related types, because it cannot determine at compile-time that these members are dynamically accessed (e.g., via reflection).

To ensure compatibility with Trimming, we suggest that the Fluent UI Blazor library:

  • Add [DynamicallyAccessedMembers] attributes to relevant types and members that are accessed via reflection. This informs the .NET Linker to preserve those members.
  • Alternatively, provide a TrimmerRoots.xml file within the NuGet package to explicitly instruct the linker which parts of the library should always be preserved.

This would allow MAUI Blazor applications to utilize AOT and Trimming for optimal performance and size without runtime failures.

🔦 Context

This issue significantly impacts the deployment of .NET MAUI Blazor applications to platforms like Android. AOT compilation and Trimming are crucial for achieving acceptable app startup performance and reducing the final app bundle size for mobile deployments.

Due to this bug, we are currently forced to deactivate both AOT Compilation and Trimming for our Android Release builds to make the application functional. This results in a substantial increase in the final Android App Bundle (.aab) size:

  • Original .aab size (with AOT/Trimming enabled): ~70 MB
  • Current .aab size (with AOT/Trimming deactivated): ~166.7 MB

This near-tripling of the app size (an increase of over 90 MB) directly affects download times, device storage consumption, and potentially user adoption. Addressing this trimming compatibility issue is vital for making Fluent UI Blazor a viable choice for optimized .NET MAUI Blazor applications.

🌍 Your Environment

  • OS & Device: Android on various devices (tested on physical Android device and reported by Google Play Console internal test track)
  • Browser: N/A (MAUI Blazor WebView)
  • .NET and Fluent UI Blazor library Version:
    • .NET 9 MAUI
    • Microsoft.FluentUI.AspNetCore.Components NuGet Package Version 4.11.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions