Skip to content
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

[Java.Interop] fix .NET 6 linker warnings #870

Merged
merged 1 commit into from
Aug 25, 2021
Merged

Conversation

jonathanpeppers
Copy link
Member

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 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.")]

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 `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.")]
@jonpryor jonpryor merged commit 0c5d454 into main Aug 25, 2021
@jonpryor jonpryor deleted the dotnet-trimmer-warnings branch August 25, 2021 16:41
jpobst pushed a commit that referenced this pull request Sep 30, 2021
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.
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants