dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

[Mono.Android.Export] decorate types to improve trimming warnings #9300

Open jonathanpeppers opened 1 week ago

jonathanpeppers commented 1 week ago

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:

jonathanpeppers commented 1 week ago

Ok wow MAUI Integration test lane will fail with:

D:\a\_work\1\s\maui\src\Core\src\Handlers\HybridWebView\HybridWebViewHandler.Android.cs(50,5): error IL2026: Using member 'Java.Interop.ExportAttribute.ExportAttribute(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. [ExportAttribute] uses dynamic features. [D:\a\_work\1\s\maui\src\Core\src\Core.csproj::TargetFramework=net9.0-android35.0]
    68 Warning(s)
    1 Error(s)

Until we get this one: