Skip to content

Commit 7bcc88f

Browse files
grendellojonathanpeppers
authored andcommitted
Don't use Dictionary<K,V> to avoid duplicate value exception (dotnet#7340)
Fixes: dotnet#7302 Context: dotnet@7117414 When building .NET6 app on .NET7, sometimes the build fails with: C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7004: System.ArgumentException: An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at Xamarin.Android.Tasks.TypeMapGenerator.GenerateRelease(Boolean skipJniAddNativeMethodRegistrationAttributeScan, List`1 javaTypes, String outputDirectory, ApplicationConfigTaskState appConfState) at Xamarin.Android.Tasks.GenerateJavaStubs.WriteTypeMappings(List`1 types, TypeDefinitionCache cache) at Xamarin.Android.Tasks.GenerateJavaStubs.Run(DirectoryAssemblyResolver res) at Xamarin.Android.Tasks.GenerateJavaStubs.RunTask() at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 17 Avoid that by switching the data structure to `List<T>`, since we no longer use the Dictionary key for anything.
1 parent d6224ca commit 7bcc88f

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/Xamarin.Android.Build.Tasks/Utilities/TypeMapGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal sealed class ModuleReleaseData
6565
public byte[] MvidBytes;
6666
public AssemblyDefinition Assembly;
6767
public TypeMapReleaseEntry[] Types;
68-
public Dictionary<uint, TypeMapReleaseEntry> DuplicateTypes;
68+
public List<TypeMapReleaseEntry> DuplicateTypes;
6969
public string AssemblyName;
7070
public string AssemblyNameLabel;
7171
public string OutputFilePath;
@@ -382,7 +382,7 @@ bool GenerateRelease (bool skipJniAddNativeMethodRegistrationAttributeScan, List
382382
Assembly = td.Module.Assembly,
383383
AssemblyName = td.Module.Assembly.Name.Name,
384384
TypesScratch = new Dictionary<string, TypeMapReleaseEntry> (StringComparer.Ordinal),
385-
DuplicateTypes = new Dictionary<uint, TypeMapReleaseEntry> (),
385+
DuplicateTypes = new List<TypeMapReleaseEntry> (),
386386
};
387387
tempModules.Add (moduleUUID, moduleData);
388388
}
@@ -410,7 +410,7 @@ bool GenerateRelease (bool skipJniAddNativeMethodRegistrationAttributeScan, List
410410
// build) and has no value for the end user. The message is left here because it may be useful to us
411411
// in our devloop at some point.
412412
//logger ($"Warning: duplicate Java type name '{entry.JavaName}' in assembly '{moduleData.AssemblyName}' (new token: {entry.Token}).");
413-
moduleData.DuplicateTypes.Add (entry.Token, entry);
413+
moduleData.DuplicateTypes.Add (entry);
414414
} else
415415
moduleData.TypesScratch.Add (entry.JavaName, entry);
416416
}

src/Xamarin.Android.Build.Tasks/Utilities/TypeMappingReleaseNativeAssemblyGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void WriteMapModules (NativeAssemblyGenerator generator, StreamWriter mapOutput,
237237
if (mapOutput != null) {
238238
WriteManagedMaps (generator, mapOutput, mapName, data.Types);
239239
if (data.DuplicateTypes.Count > 0) {
240-
WriteManagedMaps (generator, mapOutput, duplicateMapName, data.DuplicateTypes.Values);
240+
WriteManagedMaps (generator, mapOutput, duplicateMapName, data.DuplicateTypes);
241241
}
242242
}
243243
}

0 commit comments

Comments
 (0)