Skip to content

Commit 70fbb83

Browse files
[HybridWebView] fix trimmer warnings on Android (#24744)
Context: #23769 Context: dotnet/android#9300 026e046 introduced `HybridWebView`, which unfortunately introduces trimmer warnings in the `dotnet new maui` project template: > dotnet new maui > dotnet build -f net9.0-android -c Release -p:TrimMode=Full ... hellomaui succeeded with 1 warning(s) (7.9s) /_/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Android.cs(53,5): Trim analysis warning IL2026: Microsoft.Maui.Handlers.HybridWebViewHandler.HybridWebViewJavaScriptInterface.SendMessage(String): Using member 'Java.Interop.ExportAttribute.ExportAttribute(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. [ExportAttribute] uses dynamic features. This is due to usage of `Java.Interop.ExportAttribute`: private sealed class HybridWebViewJavaScriptInterface : Java.Lang.Object { //... [JavascriptInterface] [Export("sendMessage")] public void SendMessage(string message) `Java.Interop.ExportAttribute` makes heavy usage of unbounded System.Reflection, System.Reflection.Emit, etc. for it to be able to work. It brings in an additional assembly `Mono.Android.Export.dll` as well. It is inherently trimming unsafe, but how did it get through these tests? https://github.com/dotnet/maui/blob/08ff1246383ed4fdaef84a40d5b2ae8e6096bb56/src/TestUtils/src/Microsoft.Maui.IntegrationTests/AndroidTemplateTests.cs#L50-L61 This slipped through, unfortunately, as we had missed solving all the trimmer warnings in `Mono.Android.Export.dll`! dotnet/android#9300 aims to fix that. After dotnet/android#9300, new trimming warnings specific to .NET MAUI will surface such as the one above. But we can easily replace `[Export]` by: * Define a Java interface & create a binding for it in C#, we already have `maui.aar` setup for this. * We can simply implement the interface in C# and remove `[Export]`. Lastly, I fixed some of the defaults in `Metadata.xml`, it didn't look like we were automatically making Java interfaces `internal`. It looks like we probably made `ImageLoaderCallback` public by mistake.
1 parent 132b4fe commit 70fbb83

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.microsoft.maui;
2+
3+
import androidx.annotation.NonNull;
4+
5+
public interface HybridJavaScriptInterface {
6+
void sendMessage(@NonNull String message);
7+
}

src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Android.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected override AWebView CreatePlatformView()
3535
return platformView;
3636
}
3737

38-
private sealed class HybridWebViewJavaScriptInterface : Java.Lang.Object
38+
private sealed class HybridWebViewJavaScriptInterface : Java.Lang.Object, IHybridJavaScriptInterface
3939
{
4040
private readonly WeakReference<HybridWebViewHandler> _hybridWebViewHandler;
4141

@@ -47,7 +47,6 @@ public HybridWebViewJavaScriptInterface(HybridWebViewHandler hybridWebViewHandle
4747
private HybridWebViewHandler? Handler => _hybridWebViewHandler is not null && _hybridWebViewHandler.TryGetTarget(out var h) ? h : null;
4848

4949
[JavascriptInterface]
50-
[Export("sendMessage")]
5150
public void SendMessage(string message)
5251
{
5352
Handler?.VirtualView?.MessageReceived(message);

src/Core/src/Transforms/Metadata.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
<!-- Normalize the namespace for .NET -->
33
<attr path="//package[@name='com.microsoft.maui']" name="managedName">Microsoft.Maui</attr>
44

5-
<!-- Make all classes internal by default -->
5+
<!-- Make all types internal by default -->
66
<attr path="//class[@visibility='public']" name="visibility">internal</attr>
7-
<!-- Public classes -->
7+
<attr path="//interface[@visibility='public']" name="visibility">internal</attr>
8+
<!-- Public types -->
89
<attr path="//class[@name='PlatformAppCompatTextView']" name="visibility">public</attr>
910
<attr path="//class[@name='PlatformContentViewGroup']" name="visibility">public</attr>
1011
<attr path="//class[@name='PlatformWrapperView']" name="visibility">public</attr>
1112
<attr path="//class[@name='MauiViewGroup']" name="visibility">public</attr>
13+
<attr path="//interface[@name='ImageLoaderCallback']" name="visibility">public</attr>
1214
<!-- Internal methods & constructors -->
1315
<attr path="//class[@name='PlatformAppCompatTextView']/method" name="visibility">internal</attr>
1416
<attr path="//class[@name='PlatformAppCompatTextView']/constructor" name="visibility">internal</attr>

src/Core/src/maui.aar

1021 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)