|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.Android.Build.Tasks; |
| 8 | +using Microsoft.Build.Framework; |
| 9 | +using Xamarin.Android.Tools; |
| 10 | + |
| 11 | +namespace Xamarin.Android.Tasks; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Compresses assemblies using LZ4 compression before placing them in the APK. |
| 15 | +/// Note this is independent of whether they are stored compressed with ZIP in the APK. |
| 16 | +/// Our runtime bits will LZ4 decompress them at assembly load time. |
| 17 | +/// </summary> |
| 18 | +public class CompressAssemblies : AndroidTask |
| 19 | +{ |
| 20 | + public override string TaskPrefix => "CAS"; |
| 21 | + |
| 22 | + [Required] |
| 23 | + public string ApkOutputPath { get; set; } = ""; |
| 24 | + |
| 25 | + public bool EmbedAssemblies { get; set; } |
| 26 | + |
| 27 | + [Required] |
| 28 | + public bool EnableCompression { get; set; } |
| 29 | + |
| 30 | + public bool IncludeDebugSymbols { get; set; } |
| 31 | + |
| 32 | + [Required] |
| 33 | + public string ProjectFullPath { get; set; } = ""; |
| 34 | + |
| 35 | + [Required] |
| 36 | + public ITaskItem [] ResolvedFrameworkAssemblies { get; set; } = []; |
| 37 | + |
| 38 | + [Required] |
| 39 | + public ITaskItem [] ResolvedUserAssemblies { get; set; } = []; |
| 40 | + |
| 41 | + [Required] |
| 42 | + public string [] SupportedAbis { get; set; } = []; |
| 43 | + |
| 44 | + [Output] |
| 45 | + public ITaskItem [] ResolvedFrameworkAssembliesOutput { get; set; } = []; |
| 46 | + |
| 47 | + [Output] |
| 48 | + public ITaskItem [] ResolvedUserAssembliesOutput { get; set; } = []; |
| 49 | + |
| 50 | + public override bool RunTask () |
| 51 | + { |
| 52 | + if (IncludeDebugSymbols || !EnableCompression || !EmbedAssemblies) { |
| 53 | + ResolvedFrameworkAssembliesOutput = ResolvedFrameworkAssemblies; |
| 54 | + ResolvedUserAssembliesOutput = ResolvedUserAssemblies; |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + var compressed_assemblies_info = GetCompressedAssemblyInfo (); |
| 59 | + |
| 60 | + // Get all the user and framework assemblies we may need to compresss |
| 61 | + var assemblies = ResolvedFrameworkAssemblies.Concat (ResolvedUserAssemblies).Where (asm => !(ShouldSkipAssembly (asm))).ToArray (); |
| 62 | + var per_arch_assemblies = MonoAndroidHelper.GetPerArchAssemblies (assemblies, SupportedAbis, true); |
| 63 | + var compressed_output_dir = Path.GetFullPath (Path.Combine (Path.GetDirectoryName (ApkOutputPath), "..", "lz4")); |
| 64 | + |
| 65 | + foreach (var kvp in per_arch_assemblies) { |
| 66 | + Log.LogDebugMessage ($"Compressing assemblies for architecture '{kvp.Key}'"); |
| 67 | + |
| 68 | + foreach (var asm in kvp.Value.Values) { |
| 69 | + MonoAndroidHelper.LogIfReferenceAssembly (asm, Log); |
| 70 | + |
| 71 | + var compressed_assembly = AssemblyCompression.Compress (Log, asm, compressed_assemblies_info, compressed_output_dir); |
| 72 | + |
| 73 | + if (compressed_assembly.HasValue ()) { |
| 74 | + Log.LogDebugMessage ($"Compressed '{asm.ItemSpec}' to '{compressed_assembly}'."); |
| 75 | + asm.SetMetadata ("CompressedAssembly", compressed_assembly); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + ResolvedFrameworkAssembliesOutput = ResolvedFrameworkAssemblies; |
| 81 | + ResolvedUserAssembliesOutput = ResolvedUserAssemblies; |
| 82 | + |
| 83 | + return !Log.HasLoggedErrors; |
| 84 | + } |
| 85 | + |
| 86 | + IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>> GetCompressedAssemblyInfo () |
| 87 | + { |
| 88 | + var key = CompressedAssemblyInfo.GetKey (ProjectFullPath); |
| 89 | + Log.LogDebugMessage ($"Retrieving assembly compression info with key '{key}'"); |
| 90 | + |
| 91 | + var compressedAssembliesInfo = BuildEngine4.UnregisterTaskObjectAssemblyLocal<IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>> (key, RegisteredTaskObjectLifetime.Build); |
| 92 | + |
| 93 | + if (compressedAssembliesInfo is null) |
| 94 | + throw new InvalidOperationException ($"Assembly compression info not found for key '{key}'. Compression will not be performed."); |
| 95 | + |
| 96 | + return compressedAssembliesInfo; |
| 97 | + } |
| 98 | + |
| 99 | + bool ShouldSkipAssembly (ITaskItem asm) |
| 100 | + { |
| 101 | + var should_skip = asm.GetMetadataOrDefault ("AndroidSkipAddToPackage", false); |
| 102 | + |
| 103 | + if (should_skip) |
| 104 | + Log.LogDebugMessage ($"Skipping {asm.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' "); |
| 105 | + |
| 106 | + return should_skip; |
| 107 | + } |
| 108 | +} |
0 commit comments