From 19f6a56f7388770ce60ed7325dd6b8c573188c6a Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Thu, 20 Oct 2022 20:16:49 +0100 Subject: [PATCH] [Xamarin.Android.Build.Tasks] Don't report duplicate type if the mvid is the same. Fixes #7473 With the new build system under `dotnet` we can end up with duplicate assemblies for different `RuntimeIdentifiers`. This can cause the `GenerateJavaStubs` task to mistakenly report duplicate types. ``` warning XA4214: The managed type `Microsoft.UI.Xaml.Controls.AnimatedIcon` exists in multiple assemblies: Uno.UI, Uno.UI, Uno.UI, Uno.UI. Please refactor the managed type names in these assemblies so that they are not identical. [C:\a\1\s\UnoAppAll\UnoAppAll.Mobile\UnoAppAll.Mobile.csproj::TargetFramework=net6.0-android] error XA4215: The Java type `crc64a5a37c43dff01024.GridViewHeaderItem` is generated by more than one managed type. Please change the [Register] attribute so that the same Java type is not emitted. [C:\a\1\s\UnoAppAll\UnoAppAll.Mobile\UnoAppAll.Mobile.csproj::TargetFramework=net6.0-android] ``` We should ignore these duplicates is the `mvid` of the the type module is the same. If it is then they will be from the same assembly. --- .../Tasks/GenerateJavaStubs.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs index 4053a0f9b1e..548634595d4 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs @@ -217,7 +217,6 @@ void Run (DirectoryAssemblyResolver res, bool useMarshalMethods) if ((!useMarshalMethods && !userAssemblies.ContainsKey (td.Module.Assembly.Name.Name)) || JavaTypeScanner.ShouldSkipJavaCallableWrapperGeneration (td, cache)) { continue; } - javaTypes.Add (td); } @@ -272,6 +271,7 @@ void Run (DirectoryAssemblyResolver res, bool useMarshalMethods) using (var acw_map = MemoryStreamPool.Shared.CreateStreamWriter ()) { foreach (TypeDefinition type in javaTypes) { + string mvid = type.Module.Mvid.ToString (); string managedKey = type.FullName.Replace ('/', '.'); string javaKey = JavaNativeTypeManager.ToJniName (type, cache).Replace ('/', '.'); @@ -283,16 +283,20 @@ void Run (DirectoryAssemblyResolver res, bool useMarshalMethods) TypeDefinition conflict; bool hasConflict = false; if (managed.TryGetValue (managedKey, out conflict)) { - if (!managedConflicts.TryGetValue (managedKey, out var list)) - managedConflicts.Add (managedKey, list = new List { conflict.GetPartialAssemblyName (cache) }); - list.Add (type.GetPartialAssemblyName (cache)); + if (!conflict.Module.Mvid.Equals(mvid)) { + if (!managedConflicts.TryGetValue (managedKey, out var list)) + managedConflicts.Add (managedKey, list = new List { conflict.GetPartialAssemblyName (cache) }); + list.Add (type.GetPartialAssemblyName (cache)); + } hasConflict = true; } if (java.TryGetValue (javaKey, out conflict)) { - if (!javaConflicts.TryGetValue (javaKey, out var list)) - javaConflicts.Add (javaKey, list = new List { conflict.GetAssemblyQualifiedName (cache) }); - list.Add (type.GetAssemblyQualifiedName (cache)); - success = false; + if (!conflict.Module.Mvid.Equals(mvid)) { + if (!javaConflicts.TryGetValue (javaKey, out var list)) + javaConflicts.Add (javaKey, list = new List { conflict.GetAssemblyQualifiedName (cache) }); + list.Add (type.GetAssemblyQualifiedName (cache)); + success = false; + } hasConflict = true; } if (!hasConflict) {