From 1736671bca9d8a55df2561ded23b4af678d44c71 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Wed, 9 Oct 2024 12:46:57 +0200 Subject: [PATCH 1/5] Move all of assembly store building code to a separate class This makes it possible to build assembly stores easily outside `BuildApk` --- .../Tasks/BuildApk.cs | 111 +++++++----------- .../Utilities/AssemblyStoreBuilder.cs | 45 +++++++ 2 files changed, 87 insertions(+), 69 deletions(-) create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/AssemblyStoreBuilder.cs diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs index 46c60436728..e0ad6a53dcb 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs @@ -425,16 +425,12 @@ void AddAssemblies (ZipArchiveEx apk, bool debug, bool compress, IDictionary assemblyStorePaths = storeGenerator.Generate (AppSharedLibrariesDir); + Dictionary assemblyStorePaths = storeBuilder.Generate (AppSharedLibrariesDir); if (assemblyStorePaths.Count == 0) { throw new InvalidOperationException ("Assembly store generator did not generate any stores"); @@ -498,53 +494,35 @@ void DoAddAssembliesFromArchCollection (AndroidTargetArch arch, Dictionary Generate (string outputDirectoryPath) => storeGenerator.Generate (outputDirectoryPath); +} From d4414aca2a79a7933603b1362a3a3dd97936cb95 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Wed, 9 Oct 2024 17:38:21 +0200 Subject: [PATCH 2/5] Doh --- .../Tasks/BuildApk.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs index e0ad6a53dcb..27a04902f28 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs @@ -515,15 +515,17 @@ void DoAddAssembliesFromArchCollection (AndroidTargetArch arch, Dictionary Date: Wed, 9 Oct 2024 18:03:38 +0200 Subject: [PATCH 3/5] Move some of assembly packaging code to a helper class --- .../Tasks/BuildApk.cs | 26 ++------------ .../Utilities/AssemblyPackagingHelper.cs | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/AssemblyPackagingHelper.cs diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs index 27a04902f28..5bfefcb6245 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs @@ -432,10 +432,10 @@ void AddAssemblies (ZipArchiveEx apk, bool debug, bool compress, IDictionary> perArchAssemblies = MonoAndroidHelper.GetPerArchAssemblies ( - assemblies, - SupportedAbis, - validate: true, - shouldSkip: (ITaskItem asm) => { - if (bool.TryParse (asm.GetMetadata ("AndroidSkipAddToPackage"), out bool value) && value) { - Log.LogDebugMessage ($"Skipping {asm.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' "); - return true; - } - - return false; - } - ); - - foreach (var kvp in perArchAssemblies) { - Log.LogDebugMessage ($"Adding assemblies for architecture '{kvp.Key}'"); - DoAddAssembliesFromArchCollection (kvp.Key, kvp.Value); - } - } - void DoAddAssembliesFromArchCollection (AndroidTargetArch arch, Dictionary assemblies) { // In the "all assemblies are per-RID" world, assemblies, pdb and config are disguised as shared libraries (that is, diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/AssemblyPackagingHelper.cs b/src/Xamarin.Android.Build.Tasks/Utilities/AssemblyPackagingHelper.cs new file mode 100644 index 00000000000..78abaa2b200 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/AssemblyPackagingHelper.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; + +using Microsoft.Android.Build.Tasks; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks; + +static class AssemblyPackagingHelper +{ + public static void AddAssembliesFromCollection (TaskLoggingHelper Log, ICollection SupportedAbis, ICollection assemblies, Action> doAddAssemblies) + { + Dictionary> perArchAssemblies = MonoAndroidHelper.GetPerArchAssemblies ( + assemblies, + SupportedAbis, + validate: true, + shouldSkip: (ITaskItem asm) => { + if (bool.TryParse (asm.GetMetadata ("AndroidSkipAddToPackage"), out bool value) && value) { + Log.LogDebugMessage ($"Skipping {asm.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' "); + return true; + } + + return false; + } + ); + + foreach (var kvp in perArchAssemblies) { + Log.LogDebugMessage ($"Adding assemblies for architecture '{kvp.Key}'"); + doAddAssemblies (kvp.Key, kvp.Value); + } + } +} From 31c04e9496ac33687cb61af872603f5f6008f94f Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Wed, 9 Oct 2024 18:25:26 +0200 Subject: [PATCH 4/5] Move more assembly packaging code to the helper class --- .../Tasks/BuildApk.cs | 70 ++++++++----------- .../Utilities/AssemblyPackagingHelper.cs | 15 +++- 2 files changed, 41 insertions(+), 44 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs index 5bfefcb6245..607b14dd380 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs @@ -459,60 +459,46 @@ void AddAssemblies (ZipArchiveEx apk, bool debug, bool compress, IDictionary assemblies) + void DoAddAssembliesFromArchCollection (TaskLoggingHelper log, AndroidTargetArch arch, ITaskItem assembly) { // In the "all assemblies are per-RID" world, assemblies, pdb and config are disguised as shared libraries (that is, // their names end with the .so extension) so that Android allows us to put them in the `lib/{ARCH}` directory. // For this reason, they have to be treated just like other .so files, as far as compression rules are concerned. // Thus, we no longer just store them in the apk but we call the `GetCompressionMethod` method to find out whether // or not we're supposed to compress .so files. - foreach (ITaskItem assembly in assemblies.Values) { - if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec, Log)) { - Log.LogCodedWarning ("XA0107", assembly.ItemSpec, 0, Properties.Resources.XA0107, assembly.ItemSpec); - } - - sourcePath = CompressAssembly (assembly); - if (UseAssemblyStore) { - storeBuilder.AddAssembly (sourcePath, assembly, includeDebugSymbols: debug); - continue; - } - - // Add assembly - (string assemblyPath, string assemblyDirectory) = GetInArchiveAssemblyPath (assembly); - string wrappedSourcePath = DSOWrapperGenerator.WrapIt (arch, sourcePath, Path.GetFileName (assemblyPath), this); - AddFileToArchiveIfNewer (apk, wrappedSourcePath, assemblyPath, compressionMethod: GetCompressionMethod (assemblyPath)); + sourcePath = CompressAssembly (assembly); + if (UseAssemblyStore) { + storeBuilder.AddAssembly (sourcePath, assembly, includeDebugSymbols: debug); + return; + } - // Try to add config if exists - var config = Path.ChangeExtension (assembly.ItemSpec, "dll.config"); - AddAssemblyConfigEntry (apk, arch, assemblyDirectory, config); + // Add assembly + (string assemblyPath, string assemblyDirectory) = GetInArchiveAssemblyPath (assembly); + string wrappedSourcePath = DSOWrapperGenerator.WrapIt (arch, sourcePath, Path.GetFileName (assemblyPath), this); + AddFileToArchiveIfNewer (apk, wrappedSourcePath, assemblyPath, compressionMethod: GetCompressionMethod (assemblyPath)); - // Try to add symbols if Debug - if (!debug) { - continue; - } + // Try to add config if exists + var config = Path.ChangeExtension (assembly.ItemSpec, "dll.config"); + AddAssemblyConfigEntry (apk, arch, assemblyDirectory, config); - string symbols = Path.ChangeExtension (assembly.ItemSpec, "pdb"); - if (!File.Exists (symbols)) { - continue; - } + // Try to add symbols if Debug + if (!debug) { + return; + } - string archiveSymbolsPath = assemblyDirectory + MonoAndroidHelper.MakeDiscreteAssembliesEntryName (Path.GetFileName (symbols)); - string wrappedSymbolsPath = DSOWrapperGenerator.WrapIt (arch, symbols, Path.GetFileName (archiveSymbolsPath), this); - AddFileToArchiveIfNewer ( - apk, - wrappedSymbolsPath, - archiveSymbolsPath, - compressionMethod: GetCompressionMethod (archiveSymbolsPath) - ); + string symbols = Path.ChangeExtension (assembly.ItemSpec, "pdb"); + if (!File.Exists (symbols)) { + return; } - } - void EnsureCompressedAssemblyData (string sourcePath, uint descriptorIndex) - { - if (compressedAssembly == null) - compressedAssembly = new AssemblyCompression.AssemblyData (sourcePath, descriptorIndex); - else - compressedAssembly.SetData (sourcePath, descriptorIndex); + string archiveSymbolsPath = assemblyDirectory + MonoAndroidHelper.MakeDiscreteAssembliesEntryName (Path.GetFileName (symbols)); + string wrappedSymbolsPath = DSOWrapperGenerator.WrapIt (arch, symbols, Path.GetFileName (archiveSymbolsPath), this); + AddFileToArchiveIfNewer ( + apk, + wrappedSymbolsPath, + archiveSymbolsPath, + compressionMethod: GetCompressionMethod (archiveSymbolsPath) + ); } string CompressAssembly (ITaskItem assembly) diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/AssemblyPackagingHelper.cs b/src/Xamarin.Android.Build.Tasks/Utilities/AssemblyPackagingHelper.cs index 78abaa2b200..1599a8e581e 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/AssemblyPackagingHelper.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/AssemblyPackagingHelper.cs @@ -10,7 +10,7 @@ namespace Xamarin.Android.Tasks; static class AssemblyPackagingHelper { - public static void AddAssembliesFromCollection (TaskLoggingHelper Log, ICollection SupportedAbis, ICollection assemblies, Action> doAddAssemblies) + public static void AddAssembliesFromCollection (TaskLoggingHelper Log, ICollection SupportedAbis, ICollection assemblies, Action doAddAssembly) { Dictionary> perArchAssemblies = MonoAndroidHelper.GetPerArchAssemblies ( assemblies, @@ -28,7 +28,18 @@ public static void AddAssembliesFromCollection (TaskLoggingHelper Log, ICollecti foreach (var kvp in perArchAssemblies) { Log.LogDebugMessage ($"Adding assemblies for architecture '{kvp.Key}'"); - doAddAssemblies (kvp.Key, kvp.Value); + DoAddAssembliesFromArchCollection (Log, kvp.Key, kvp.Value, doAddAssembly); + } + } + + static void DoAddAssembliesFromArchCollection (TaskLoggingHelper Log, AndroidTargetArch arch, Dictionary assemblies, Action doAddAssembly) + { + foreach (ITaskItem assembly in assemblies.Values) { + if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec, Log)) { + Log.LogCodedWarning ("XA0107", assembly.ItemSpec, 0, Properties.Resources.XA0107, assembly.ItemSpec); + } + + doAddAssembly (Log, arch, assembly); } } } From 8aa8f247c1e87f85a496fd1ec9871d9836cde327 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Wed, 9 Oct 2024 18:34:22 +0200 Subject: [PATCH 5/5] Update --- src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs index 607b14dd380..192fc5767a8 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs @@ -501,6 +501,14 @@ void DoAddAssembliesFromArchCollection (TaskLoggingHelper log, AndroidTargetArch ); } + void EnsureCompressedAssemblyData (string sourcePath, uint descriptorIndex) + { + if (compressedAssembly == null) + compressedAssembly = new AssemblyCompression.AssemblyData (sourcePath, descriptorIndex); + else + compressedAssembly.SetData (sourcePath, descriptorIndex); + } + string CompressAssembly (ITaskItem assembly) { if (!compress) {