Maui: [BUG] Snackbar InvalidOperationException when using dotnet trimmer

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the “Reporting a bug” section on Contributing file?

Current Behavior

When attempting to show a Snackbar on a Maui app for Android with dotnet trimmer full mode enabled, the following exception occurs:

System.InvalidOperationException: Unable to find Snackbar text view
  at CommunityToolkit.Maui.Alerts.Snackbar.SetMessageForView(View&, IFontManager)
  at CommunityToolkit.Maui.Alerts.Snackbar.ShowPlatform(CancellationToken )
  at MauiAppSnackbar.MainPage.OnCounterClicked(Object sender, EventArgs e)

Expected Behavior

Instead of throwing an exception, the Snackbar should be displayed.

Workaround: do not trim CommunityToolkit.Maui

<ItemGroup Condition="'$(Configuration)|$([MSBuild]::GetTargetPlatformIdentifier($(TargetFramework)))'=='Release|android'">
	<TrimmerRootAssembly Include="CommunityToolkit.Maui" RootMode="All" />
</ItemGroup>

Steps To Reproduce

  1. Open the repro project
  2. Launch in release mode (dotnet trimmer full mode)
  3. Click on button: a dialog will appear, catching the exception that is raised by CommunityToolkit.Maui

Link to public reproduction project repository

https://github.com/tranb3r/Issues/tree/main/MauiAppSnackbar

Environment

- .NET MAUI CommunityToolkit: 6.0.0
- OS: Android
- .NET MAUI: 8.0 rc2

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Comments: 19 (4 by maintainers)

Most upvoted comments

It could be related to this, which will be fixed in .NET 8 GA: https://github.com/xamarin/xamarin-android/commit/03018e02d0e33eb37309ae83fd9c6ad549fe1ce5

There might be an intermediate release before GA, where we have a chance to ship this change.

Note that setting IsTrimmable=false is not a long-term solution. It will not work on NativeAOT for iOS, for example, as NativeAOT has to trim everything. It will completely ignore this setting in this case.

Details at: https://github.com/dotnet/maui/issues/18658

if the MCT is not trimmer ready, maybe it should simply have its IsTrimmable attribute set

@VladislavAntonyuk - could you add <IsTrimmable>false</IsTrimmable> to #1367?

<PropertyGroup>
    <IsTrimmable>false</IsTrimmable>
</PropertyGroup>

Here’s the .NET 8 docs for it: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming?pivots=dotnet-8-0#enable-project-specific-trimming