Skip to content

[Regression] .NET 10 Trimmer strips MediaElement constructors on Android despite preservation rules #3114

@kizwiz6

Description

@kizwiz6

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

Since updating to .NET 10 (MAUI 10.0.41), applications using CommunityToolkit.Maui.MediaElement crash on Android when trimming is enabled. The trimmer is incorrectly identifying essential constructors and JNI-callback methods as unused code and stripping them.

Expected Behavior

The MauiMediaElement should survive the .NET 10 trimming process so that the Android OS can properly invoke its constructors during lifecycle events (like OnDetachedFromWindow or XAML inflation) without throwing a MissingMethodException.

Steps To Reproduce

  1. Create a MAUI project targeting net10.0-android.
  2. Add CommunityToolkit.Maui.MediaElement v8.0.0.
  3. Configure the .csproj with the following aggressive trimming settings:
<PropertyGroup Condition="$(TargetFramework.Contains('-android'))">
    <AndroidLinkMode>Full</AndroidLinkMode>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>partial</TrimMode>
</PropertyGroup>
  1. Navigate to a page containing a MediaElement.

Link to public reproduction project repository

https://github.com/kizwiz6/MauiBug

Environment

- .NET MAUI CommunityToolkit: 8.0.0
- OS: Windows 11
- .NET MAUI: 10.0.41
- Link Mode: Full
- Trim Mode: partial (Full trim mode causes even deeper XAML parser failures)

Anything else?

Crash Log Evidence:
The following stack trace was captured by wrapping InitializeComponent() in a diagnostic try-catch. It confirms the trimmer has removed the constructor:

=== TRIMMER CRASH DETECTED ===
Microsoft.Maui.Controls.Xaml.XamlParseException: Position 15:10. Arg_NoDefCTor, CommunityToolkit.Maui.Views.MediaElement
 ---> System.MissingMethodException: Arg_NoDefCTor, CommunityToolkit.Maui.Views.MediaElement
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at Microsoft.Maui.Controls.Xaml.CreateValuesVisitor.Visit(ElementNode node, INode parentNode)

Workarounds Attempted (and Failed):
In a separate and larger production project, the following attempts were made to preserve the types, but the .NET 10 trimmer ignored them:

  • Added <TrimmerRootAssembly Include="CommunityToolkit.Maui.MediaElement" />
  • Added explicit Linker.xml descriptors for the MauiMediaElement type.
  • Added proguard.cfg rules.
    Result: The app still crashes with MissingMethodException (regarding Media3 listeners) or AbstractMethodError (regarding constructors).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions