-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Mono.Android.Export] decorate types to improve trimming warnings #9300
Merged
jonpryor
merged 2 commits into
dotnet:main
from
jonathanpeppers:Mono.Android.Export-TrimmerWarnings
Sep 25, 2024
Merged
[Mono.Android.Export] decorate types to improve trimming warnings #9300
jonpryor
merged 2 commits into
dotnet:main
from
jonathanpeppers:Mono.Android.Export-TrimmerWarnings
Sep 25, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jonathanpeppers
added a commit
to jonathanpeppers/maui
that referenced
this pull request
Sep 12, 2024
Context: dotnet#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.
Ok wow
Until we get this one: |
rmarinho
pushed a commit
to dotnet/maui
that referenced
this pull request
Sep 20, 2024
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.
The entire `Mono.Android.Export.dll` assembly is not trimming safe, and never will be: it relies on many dynamic features. But it is possible to get the warning: warning IL2104: Assembly 'Mono.Android.Export' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries First, import `trim-analyzers.props` so we have the right analyzers set for `Mono.Android.Export.csproj`. We can also remove `$(EnableSingleFileAnalyzer)` as it is in `trim-analyzers.props`. This results in ~41 errors, which are mostly resolved by decorating every type with: [RequiresUnreferencedCode (MonoAndroidExport.DynamicFeatures)] This seems simpler than decorating methods, as there are quite a few more methods involved than classes. After this change, we are down to a handful of warnings: Mono.Android.Export\CallbackCode.cs(526,19): error IL3050: Using member 'System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Defining a dynamic assembly requires dynamic code. Mono.Android.Export\CallbackCode.cs(197,32): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\Mono.CodeGeneration\CodeCustomAttribute.cs(82,23): error IL3050: Using member 'System.Collections.ArrayList.ToArray(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. Mono.Android.Export\Mono.CodeGeneration\CodeCustomAttribute.cs(83,20): error IL3050: Using member 'System.Collections.ArrayList.ToArray(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. Mono.Android.Export\CallbackCode.cs(609,59): error IL3050: Using member 'System.Type.MakeGenericType(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\CallbackCode.cs(612,22): error IL3050: Using member 'System.Type.MakeGenericType(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\CallbackCode.cs(270,32): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\Mono.CodeGeneration\CodeModule.cs(48,35): error IL3050: Using member 'System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Defining a dynamic assembly requires dynamic code. Mono.Android.Export\CallbackCode.cs(426,32): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\CallbackCode.cs(643,13): error IL3050: Using member 'System.Reflection.Emit.DynamicMethod.DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Module, Boolean)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Creating a DynamicMethod requires dynamic code. Mono.Android.Export\CallbackCode.cs(336,32): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\CallbackCode.cs(336,154): error IL3050: Using member 'System.Type.MakeArrayType()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. Mono.Android.Export\CallbackCode.cs(341,32): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\CallbackCode.cs(346,32): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\CallbackCode.cs(368,32): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\CallbackCode.cs(373,32): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Mono.Android.Export\CallbackCode.cs(671,6): error IL3050: Using member 'System.Reflection.MethodInfo.MakeGenericMethod(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. This is a *different* analyzer for NativeAOT, which is IL3050. The types involved are a smaller list, I decorated each with: [RequiresDynamicCode (MonoAndroidExport.DynamicFeatures)] With these changes, there are no no warnings present any longer. Other changes: * Delete `CodeAdd.cs`, it only contains a comment
src/Xamarin.Android.NamingCustomAttributes/Java.Interop/ExportAttribute.cs(21,3): warning IL2026: Java.Interop.ExportAttribute.ExportAttribute(String): Using member 'Java.Interop.DynamicCallbackCodeGenerator..cctor()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Mono.Android.Export uses dynamic features.
jonathanpeppers
force-pushed
the
Mono.Android.Export-TrimmerWarnings
branch
from
September 24, 2024 00:21
645fc66
to
3c6ecd1
Compare
jonpryor
reviewed
Sep 25, 2024
|
||
static class MonoAndroidExport | ||
{ | ||
public const string DynamicFeatures = "Mono.Android.Export uses dynamic features."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would prefer:
[Java.Interop.ExportAttribute] requires dynamic features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed in meeting:
- We realized customers would only get a warning on
[ExportAttribute]
which says:[RequiresUnreferencedCode ("[ExportAttribute] uses dynamic features.")]
- The warning here should only be possibly seen by us, so it's probably fine as-is.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The entire
Mono.Android.Export.dll
assembly is not trimming safe, and never will be: it relies on many dynamic features. But it is possible to get the warning:First, import
trim-analyzers.props
so we have the right analyzers set forMono.Android.Export.csproj
. We can also remove$(EnableSingleFileAnalyzer)
as it is intrim-analyzers.props
.This results in ~41 errors, which are mostly resolved by decorating every type with:
This seems simpler than decorating methods, as there are quite a few more methods involved than classes.
After this change, we are down to a handful of warnings:
This is a different analyzer for NativeAOT, which is IL3050.
The types involved are a smaller list, I decorated each with:
With these changes, there are no no warnings present any longer.
Other changes:
CodeAdd.cs
, it only contains a comment