Skip to content

Move HybridWebView trimmer attributes from class-level to method-level#35363

Open
jonathanpeppers wants to merge 2 commits intonet11.0from
remove-aot-root-all-test
Open

Move HybridWebView trimmer attributes from class-level to method-level#35363
jonathanpeppers wants to merge 2 commits intonet11.0from
remove-aot-root-all-test

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Member

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

Move [RequiresUnreferencedCode] and [RequiresDynamicCode] from class-level to method-level on HybridWebViewHandler and remove them entirely from platform types that are trim-safe.

Problem

The iOS/macCatalyst Managed Registrar generates code in <Module>..cctor() that references all NSObject subclasses, including SchemeHandler and WebViewScriptMessageHandler. These nested classes had class-level [RequiresUnreferencedCode], which caused IL2026 errors from the registrar's type registration code — errors that could never be resolved via feature switches.

Similarly, Android's MauiHybridWebViewClient (extending WebViewClient) had the same problem.

This caused failures in:

  • RunOniOS_MauiReleaseTrimFull integration test (TrimMode=full)
  • PublishNativeAOTRootAllMauiAssemblies integration test (rooting all assemblies)

Fix

Only three methods on HybridWebViewHandler are actually trim-unsafe (they use reflection and runtime JSON serialization):

  • InvokeDotNetAsync
  • InvokeDotNetMethodAsync
  • CreateInvokeResult

All other code (message receiving, URL scheme handling for static files, platform view wrappers) is trim-safe — it only uses source-generated JSON contexts.

Changes:

  1. Removed class-level RUCA from HybridWebViewHandler, MauiHybridWebViewClient, MauiHybridWebView (Android/Windows), and all nested platform classes
  2. Added method-level RUCA to the three methods that actually need it
  3. Guarded InvokeDotNetAsync calls on all platforms with RuntimeFeature.IsHybridWebViewSupported — a [FeatureGuard] for RequiresUnreferencedCode. When the switch is false (TrimMode=full or PublishAot=true), the trimmer removes the unsafe code path entirely.

Remove [RequiresUnreferencedCode] and [RequiresDynamicCode] from:
- HybridWebViewHandler class (move to InvokeDotNetAsync, CreateInvokeResult,
  InvokeDotNetMethodAsync — the only methods with actual trim-unsafe code)
- WebViewScriptMessageHandler and SchemeHandler nested classes (iOS)
- HybridWebView2Proxy nested class (Windows)
- MauiHybridWebViewClient (Android)
- MauiHybridWebView platform views (Android, Windows)

These types are preserved by platform managed registrars (NSObject/WebViewClient
subclasses), so class-level RUCA caused IL2026 errors from <Module>..cctor()
that could never be resolved via feature switches.

Guard InvokeDotNetAsync calls with RuntimeFeature.IsHybridWebViewSupported
(a FeatureGuard for RequiresUnreferencedCode) on iOS, Android, and Windows.
When the switch is false (TrimMode=full or PublishAot=true), the trimmer
removes the unsafe code path entirely.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 35363

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 35363"

@jonathanpeppers jonathanpeppers marked this pull request as ready for review May 8, 2026 20:05
Copilot AI review requested due to automatic review settings May 8, 2026 20:05
@jonathanpeppers
Copy link
Copy Markdown
Member Author

The warning that is appearing on the one test failure seems unrelated/different:

/Users/cloudtest/vss/_work/_temp/test-dir/Test8b873e10b051464550798/obj/Release/net11.0-ios/ios-arm64/linker-cache/apply-preserve-attribute.xml(796,8): warning IL2009: Could not find method 'System.Void Activated(T)' on type 'UIKit.UIGestureRecognizer.Callback`1'. [/Users/cloudtest/vss/_work/_temp/test-dir/Test8b873e10b051464550798/Test8b873e10b051464550798.csproj::TargetFramework=net11.0-ios]
Build succeeded.
/Users/cloudtest/vss/_work/_temp/test-dir/Test8b873e10b051464550798/obj/Release/net11.0-ios/ios-arm64/linker-cache/apply-preserve-attribute.xml(796,8): warning IL2009: Could not find method 'System.Void Activated(T)' on type 'UIKit.UIGestureRecognizer.Callback`1'. [/Users/cloudtest/vss/_work/_temp/test-dir/Test8b873e10b051464550798/Test8b873e10b051464550798.csproj::TargetFramework=net11.0-ios]
    1 Warning(s)
    0 Error(s)

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 refines trimming/AOT annotations for HybridWebView by moving [RequiresUnreferencedCode] / [RequiresDynamicCode] from broad class-level usage to the specific reflection/dynamic-serialization methods that require it, and by gating the InvokeDotNet endpoint behind RuntimeFeature.IsHybridWebViewSupported to avoid trim-unsafe code paths when disabled.

Changes:

  • Removed class-level Requires* attributes from HybridWebViewHandler and several platform helper types/nested classes that are otherwise trim-safe.
  • Added method-level Requires* attributes to the three trim-unsafe methods in HybridWebViewHandler.
  • Added RuntimeFeature.IsHybridWebViewSupported guards to the InvokeDotNet request handling paths on Android/iOS/Windows.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Core/src/Platform/Windows/MauiHybridWebView.cs Removes class-level trimming attributes from the Windows platform WebView wrapper.
src/Core/src/Platform/Android/MauiHybridWebViewClient.cs Removes class-level trimming attributes; gates InvokeDotNet endpoint behind RuntimeFeature.IsHybridWebViewSupported.
src/Core/src/Platform/Android/MauiHybridWebView.cs Removes class-level trimming attributes from the Android platform WebView wrapper.
src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs Gates InvokeDotNet handling and removes trimming attributes from the WebView2 proxy type.
src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.iOS.cs Removes trimming attributes from nested registrar-referenced types; gates InvokeDotNet handling.
src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.cs Moves trimming attributes from class-level to the specific trim-unsafe methods.
Comments suppressed due to low confidence (1)

src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.iOS.cs:129

  • After removing the Requires* attributes from the nested handler types, using System.Diagnostics.CodeAnalysis; is unused in this file and will trigger CS8019 (and fail the build under TreatWarningsAsErrors). Please remove the unused using.

		private sealed class WebViewScriptMessageHandler : NSObject, IWKScriptMessageHandler
		{
			private readonly WeakReference<HybridWebViewHandler?> _webViewHandler;

Comment thread src/Core/src/Platform/Android/MauiHybridWebViewClient.cs
Comment thread src/Core/src/Platform/Android/MauiHybridWebView.cs
Comment thread src/Core/src/Platform/Windows/MauiHybridWebView.cs
Comment thread src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs
Comment thread src/Core/src/Platform/Android/MauiHybridWebViewClient.cs
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@MauiBot
Copy link
Copy Markdown
Collaborator

MauiBot commented May 8, 2026

⚠️ Merge Conflict Detected — This PR has merge conflicts with its target branch. Please rebase onto the target branch and resolve the conflicts.

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