Skip to content

Commit f64dd7b

Browse files
committed
[NET8] Fix assembly count when satellite assemblies are present
Context: #8790 The old method of satellite assembly counting relied on the `RelativePath` MSBuild item metadata, which appears to have gone missing somewhere in .NET8+. Update the code to check for presence of the following metadata, in order given, to determine assembly's culture, if any: * `Culture` * `RelativePath` * `DestinationSubDirectory` Failure to count satellite assemblies can, and sometimes will, result in a segfault since we generate a number of native code arrays based on the assembly count and the runtime assumes that what the build told it is true.
1 parent 5c04944 commit f64dd7b

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,16 @@ void AddEnvironment ()
262262
HashSet<string> archAssemblyNames = null;
263263
HashSet<string> uniqueAssemblyNames = new HashSet<string> (StringComparer.OrdinalIgnoreCase);
264264
Action<ITaskItem> updateAssemblyCount = (ITaskItem assembly) => {
265-
// We need to use the 'RelativePath' metadata, if found, because it will give us the correct path for satellite assemblies - with the culture in the path.
266-
string? relativePath = assembly.GetMetadata ("RelativePath");
267-
string assemblyName = String.IsNullOrEmpty (relativePath) ? Path.GetFileName (assembly.ItemSpec) : relativePath;
265+
string? culture = MonoAndroidHelper.GetAssemblyCulture (assembly);
266+
string fileName = Path.GetFileName (assembly.ItemSpec);
267+
tring assemblyName;
268+
269+
if (String.IsNullOrEmpty (culture)) {
270+
assemblyName = fileName;
271+
} else {
272+
assemblyName = $"{culture}/{fileName}";
273+
}
274+
268275
if (!uniqueAssemblyNames.Contains (assemblyName)) {
269276
uniqueAssemblyNames.Add (assemblyName);
270277
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,5 +643,37 @@ public static string GetNativeLibsRootDirectoryPath (string androidBinUtilsDirec
643643
string relPath = GetToolsRootDirectoryRelativePath (androidBinUtilsDirectory);
644644
return Path.GetFullPath (Path.Combine (androidBinUtilsDirectory, relPath, "lib"));
645645
}
646+
647+
public static string? GetAssemblyCulture (ITaskItem assembly)
648+
{
649+
// The best option
650+
string? culture = assembly.GetMetadata ("Culture");
651+
if (!String.IsNullOrEmpty (culture)) {
652+
return TrimSlashes (culture);
653+
}
654+
655+
// ...slightly worse
656+
culture = assembly.GetMetadata ("RelativePath");
657+
if (!String.IsNullOrEmpty (culture)) {
658+
return TrimSlashes (Path.GetDirectoryName (culture));
659+
}
660+
661+
// ...not ideal
662+
culture = assembly.GetMetadata ("DestinationSubDirectory");
663+
if (!String.IsNullOrEmpty (culture)) {
664+
return TrimSlashes (culture);
665+
}
666+
667+
return null;
668+
669+
string? TrimSlashes (string? s)
670+
{
671+
if (String.IsNullOrEmpty (s)) {
672+
return null;
673+
}
674+
675+
return s.TrimEnd ('/').TrimEnd ('\\');
676+
}
677+
}
646678
}
647679
}

0 commit comments

Comments
 (0)