Skip to content

Commit d77f69b

Browse files
committed
Don't use Dictionary<K,V> to avoid duplicate value exception
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 863cfc5 commit d77f69b

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
@@ -56,7 +56,7 @@ internal sealed class ModuleReleaseData
5656
public byte[] MvidBytes;
5757
public AssemblyDefinition Assembly;
5858
public TypeMapReleaseEntry[] Types;
59-
public Dictionary<uint, TypeMapReleaseEntry> DuplicateTypes;
59+
public List<TypeMapReleaseEntry> DuplicateTypes;
6060
public string AssemblyName;
6161
public string OutputFilePath;
6262

@@ -368,7 +368,7 @@ bool GenerateRelease (bool skipJniAddNativeMethodRegistrationAttributeScan, List
368368
Assembly = td.Module.Assembly,
369369
AssemblyName = td.Module.Assembly.Name.Name,
370370
TypesScratch = new Dictionary<string, TypeMapReleaseEntry> (StringComparer.Ordinal),
371-
DuplicateTypes = new Dictionary<uint, TypeMapReleaseEntry> (),
371+
DuplicateTypes = new List<TypeMapReleaseEntry> (),
372372
};
373373
tempModules.Add (moduleUUID, moduleData);
374374
}
@@ -392,7 +392,7 @@ bool GenerateRelease (bool skipJniAddNativeMethodRegistrationAttributeScan, List
392392
// build) and has no value for the end user. The message is left here because it may be useful to us
393393
// in our devloop at some point.
394394
//logger ($"Warning: duplicate Java type name '{entry.JavaName}' in assembly '{moduleData.AssemblyName}' (new token: {entry.Token}).");
395-
moduleData.DuplicateTypes.Add (entry.Token, entry);
395+
moduleData.DuplicateTypes.Add (entry);
396396
} else
397397
moduleData.TypesScratch.Add (entry.JavaName, entry);
398398
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ uint GetJavaEntryIndex (string javaTypeName)
281281
TypeMapModule module = moduleInstance.Obj;
282282
PrepareMapModuleData (module.MapSymbolName, module.Data.Types, allMapModulesData);
283283
if (module.Data.DuplicateTypes.Count > 0) {
284-
PrepareMapModuleData (module.DuplicateMapSymbolName, module.Data.DuplicateTypes.Values, allMapModulesData);
284+
PrepareMapModuleData (module.DuplicateMapSymbolName, module.Data.DuplicateTypes, allMapModulesData);
285285
}
286286
}
287287

0 commit comments

Comments
 (0)