-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java.Interop.Tools.TypeNameMappings] fix trimmer warnings (#1194)
Context: https://github.com/xamarin/xamarin-android/blob/e987ac458536e59a8329a06d5c5d5f4d4ea2c6b6/src/Mono.Android/Mono.Android.csproj#L69-L71 In xamarin/xamarin-android's `Mono.Android.dll`, we [import][0] `Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs`, and unfortunately that causes some trimmer warnings: external\Java.Interop\src\Java.Interop.Tools.TypeNameMappings\Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs(182,9): error IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. From the code: if (!type.GetInterfaces ().Any (t => t.FullName == "Android.Runtime.IJavaObject")) It appears we can instead look for `IJavaPeerable` and use trim-safe behavior instead: if (Type.GetType ("Java.Interop.IJavaPeerable, Java.Interop", throwOnError: true) .IsAssignableFrom (type)) I also cache the `Type.GetType()` call with `Lazy<T>`. However, in some cases we run this code under an MSBuild context. In this case, we don't have `Java.Interop.dll` to load, so we should instead use a variation on the previous code: type.GetInterfaces ().Any (t => t.FullName == "Java.Interop.IJavaPeerable"); To catch warnings in this project going forward: * Multi-target to `netstandard2.0` and `net8.0` using `$(DotNetTargetFramework)`. `netstandard2.0` is used for MSBuild-task assemblies inside VS. * Enable trimmer warnings for `net8.0`. Additionally, as I targeted `net8.0`, various null-reference-type warnings appeared, which I also fixed. [0]: https://github.com/xamarin/xamarin-android/blob/e199d62210bfb666595d95ca60579c5c766be1d6/src/Mono.Android/Mono.Android.csproj#L69-L71
- Loading branch information
1 parent
c825dca
commit 56b7eeb
Showing
6 changed files
with
37 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
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
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