Skip to content

Add DynamicDependency to MauiMediaElement constructors#3116

Merged
TheCodeTraveler merged 12 commits intoCommunityToolkit:mainfrom
ne0rrmatrix:FixMediaElementParamterlessConstructorBug
Mar 12, 2026
Merged

Add DynamicDependency to MauiMediaElement constructors#3116
TheCodeTraveler merged 12 commits intoCommunityToolkit:mainfrom
ne0rrmatrix:FixMediaElementParamterlessConstructorBug

Conversation

@ne0rrmatrix
Copy link
Copy Markdown
Member

Description of Change

Added [DynamicDependency] to MauiMediaElement constructors to preserve MediaElement public constructors during linking/trimming.

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

Fixes parameter-less constructor error and crash in Android when using MediaElement.

Added [DynamicDependency] to MauiMediaElement constructors to preserve MediaElement public constructors during linking/trimming.
Comment thread src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.android.cs Outdated
ne0rrmatrix and others added 3 commits February 24, 2026 13:39
…ng linking. Updated OnDetachedFromWindow to use [DynamicDepedancy]
…android.cs

Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>
…ng linking. Updated OnDetachedFromWindow to use [DynamicDepedancy]
@ne0rrmatrix
Copy link
Copy Markdown
Member Author

@pictos I tried removing the [DynamicDependancy] after your suggested changes and it crashed. But adding those constructors won't hurt so I added them and and the attribute back. I am having no issues with running mediaElement with the test sample provided by developer now. Ty for the idea.

@ne0rrmatrix ne0rrmatrix requested a review from pictos February 24, 2026 22:27
@ne0rrmatrix
Copy link
Copy Markdown
Member Author

@pictos This as is now fixes the issue that was brought up. The OP has confirmed this fixes it.

Copy link
Copy Markdown
Member

@pictos pictos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to let you know, this is more a bandwidth than a proper fix. Those constructors are called from Java world when the managed object is collected, so at some point, the code tries to access a disposed MediaElement, would be good to get the stack trace and see the real issue

@ne0rrmatrix
Copy link
Copy Markdown
Member Author

Just to let you know, this is more a bandwidth than a proper fix. Those constructors are called from Java world when the managed object is collected, so at some point, the code tries to access a disposed MediaElement, would be good to get the stack trace and see the real issue

I agree 100 percent with that. I have already started investigating the issue. I see this as a workaround until a proper fix can be done. I just don't want to leave mediaElement in a non working state for some users.

Copilot AI review requested due to automatic review settings March 7, 2026 04:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent .NET 10 trimming/linking from removing MediaElement public constructors by adding trimming annotations on the Android MauiMediaElement implementation (to address the Android crash/regression in #3114).

Changes:

  • Added [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MediaElement))] annotations to Android MauiMediaElement members.
  • Introduced additional Android MauiMediaElement constructor overloads and adjusted the JNI handle constructor to call the appropriate base constructor.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.android.cs Outdated
@pictos
Copy link
Copy Markdown
Member

pictos commented Mar 11, 2026

@ne0rrmatrix is this one ready for review?

@ne0rrmatrix
Copy link
Copy Markdown
Member Author

@ne0rrmatrix is this one ready for review?

Yes it is ready for review

Copy link
Copy Markdown
Member

@pictos pictos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the green anymore, but it LGTM

@TheCodeTraveler TheCodeTraveler enabled auto-merge (squash) March 12, 2026 02:17
@TheCodeTraveler TheCodeTraveler merged commit 6c93703 into CommunityToolkit:main Mar 12, 2026
10 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Mar 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants