Skip to content

Commit f27956c

Browse files
jonathanpeppersjpobst
authored andcommitted
[Java.Interop] fix .NET 6 linker warnings (#870)
Context: dotnet/android#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 [warning `IL2026`][0]: [UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "DynamicDependency should preserve the constructor.")] Then, because `Java.Interop.Export.dll` is not always included in apps [^0], we also need to suppress [warning IL2035][1]: [UnconditionalSuppressMessage ("Trimming", "IL2035", Justification = "Java.Interop.Export.dll is not always present.")] [0]: https://docs.microsoft.com/en-us/dotnet/core/deploying/trim-warnings/il2026 [1]: https://github.com/mono/linker/blob/66fd7119cd5744dd8bd37442ac74d2a326085406/docs/error-codes.md#il2035-unresolved-assembly-assemblyname-in-dynamicdependencyattribute [^0]: `Java.Interop.Export.dll` is not shipped as in Xamarin.Android as a "MonoAndroid-profile" assembly -- and likely shouldn't be shipped *at all* right now -- and thus will only reliably exist for the few people building xamarin/Java.Interop.
1 parent acf9f50 commit f27956c

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Linq.Expressions;
88
using System.Reflection;
9+
using System.Runtime.CompilerServices;
910
using System.Text;
1011

1112
namespace Java.Interop {
@@ -27,6 +28,11 @@ public JniMarshalMemberBuilder MarshalMemberBuilder {
2728
}
2829

2930
[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)")]
31+
#if NET
32+
[DynamicDependency (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, "Java.Interop.MarshalMemberBuilder", "Java.Interop.Export")]
33+
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "DynamicDependency should preserve the constructor.")]
34+
[UnconditionalSuppressMessage ("Trimming", "IL2035", Justification = "Java.Interop.Export.dll is not always present.")]
35+
#endif
3036
partial void SetMarshalMemberBuilder (CreationOptions options)
3137
{
3238
if (!options.UseMarshalMemberBuilder) {

0 commit comments

Comments
 (0)