From 0adc5bae99d93535ced18afc143c30f71ea0d8a9 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Fri, 20 Aug 2021 11:27:50 -0500 Subject: [PATCH] [Java.Interop] fix .NET 6 linker warnings Context: https://github.com/xamarin/xamarin-android/issues/5652 If you build a .NET 6 Android app: dotnet new android dotnet build -c Release -bl -p:SuppressTrimAnalysisWarnings=false You get warnings like: external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniMarshalMemberBuilder.cs(53,4): warning IL2026: Java.Interop.JniRuntime.SetMarshalMemberBuilder(JniRuntime.CreationOptions): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed. Adding this is not sufficient to fix the warning: [DynamicDependency (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, "Java.Interop.MarshalMemberBuilder", "Java.Interop.Export")] You also need to suppress `IL2026`: [UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "DynamicDependency should preserve the constructor.")] Then, because `Java.Interop.Export.dll` is not always included in apps, we also need to suppress: [UnconditionalSuppressMessage ("Trimming", "IL2035", Justification = "Java.Interop.Export.dll is not always present.")] --- .../Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs index ab0356e31..11fabae92 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text; namespace Java.Interop { @@ -27,6 +28,11 @@ public JniMarshalMemberBuilder MarshalMemberBuilder { } [System.Diagnostics.CodeAnalysis.SuppressMessage ("Design", "CA1031:Do not catch general exception types", Justification = "the *.Export assemblies are optional, so we don't care when they cannot be loaded (they are presumably missing)")] +#if NET + [DynamicDependency (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, "Java.Interop.MarshalMemberBuilder", "Java.Interop.Export")] + [UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "DynamicDependency should preserve the constructor.")] + [UnconditionalSuppressMessage ("Trimming", "IL2035", Justification = "Java.Interop.Export.dll is not always present.")] +#endif partial void SetMarshalMemberBuilder (CreationOptions options) { if (!options.UseMarshalMemberBuilder) {