Skip to content

Commit

Permalink
Almost there, TBC
Browse files Browse the repository at this point in the history
  • Loading branch information
grendello committed Oct 10, 2024
1 parent 2537790 commit 7436fbf
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
<CreateEmbeddedAssemblyStore
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
AppSharedLibrariesDir="$(_AndroidApplicationSharedLibraryPath)"
CompressedAssembliesDir="$(_AndroidCompressedAssembliesDir)\test\"
Debug="$(AndroidIncludeDebugSymbols)"
EnableCompression="$(AndroidEnableAssemblyCompression)"
ProjectFullPath="$(MSBuildProjectFullPath)"
ResolvedUserAssemblies="@(_ShrunkUserAssemblies);@(_AndroidResolvedSatellitePaths)"
ResolvedFrameworkAssemblies="@(_ShrunkFrameworkAssemblies)">
ResolvedFrameworkAssemblies="@(_ShrunkFrameworkAssemblies)"
SupportedAbis="@(_BuildTargetAbis)">
<Output TaskParameter="EmbeddedObjectFiles" ItemName="_NativeAssemblyTarget" />
<Output TaskParameter="NativeAssemblySources" ItemName="_EmbeddedAssemblyStoreSourceFiles" />
</CreateEmbeddedAssemblyStore>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ properties that determine build ordering.
<_PrepareBuildApkDependsOnTargets>
_SetLatestTargetFrameworkVersion;
_GetLibraryImports;
_RemoveRegisterAttribute;
_ResolveAssemblies;
_ResolveSatellitePaths;
_CreatePackageWorkspace;
Expand Down
8 changes: 5 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public class BuildApk : AndroidTask
[Required]
public string ProjectFullPath { get; set; }

[Required]
public string CompressedAssembliesDir { get; set; }

[Output]
public ITaskItem[] OutputFiles { get; set; }

Expand Down Expand Up @@ -348,7 +351,7 @@ public override bool RunTask ()

if (compress) {
string key = CompressedAssemblyInfo.GetKey (ProjectFullPath);
Log.LogDebugMessage ($"Retrieving assembly compression info with key '{key}'");
Log.LogDebugMessage ($"[{TaskPrefix}] Retrieving assembly compression info with key '{key}'");
compressedAssembliesInfo = BuildEngine4.UnregisterTaskObjectAssemblyLocal<IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>> (key, RegisteredTaskObjectLifetime.Build);
if (compressedAssembliesInfo == null)
throw new InvalidOperationException ($"Assembly compression info not found for key '{key}'. Compression will not be performed.");
Expand Down Expand Up @@ -424,7 +427,6 @@ void AddAssemblies (ZipArchiveEx apk, bool debug, bool compress, IDictionary<And
{
string sourcePath;
AssemblyCompression.AssemblyData compressedAssembly = null;
string compressedOutputDir = Path.GetFullPath (Path.Combine (Path.GetDirectoryName (ApkOutputPath), "..", "lz4"));
AssemblyStoreBuilder? storeBuilder = null;

if (UseAssemblyStore) {
Expand Down Expand Up @@ -507,7 +509,7 @@ string CompressAssembly (ITaskItem assembly)
return assembly.ItemSpec;
}

return AssemblyCompression.Compress (Log, assembly, compressedAssembliesInfo, compressedOutputDir);
return AssemblyCompression.Compress (Log, assembly, compressedAssembliesInfo, CompressedAssembliesDir);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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;
Expand All @@ -15,12 +18,27 @@ public class CreateEmbeddedAssemblyStore : AndroidTask
[Required]
public string AppSharedLibrariesDir { get; set; }

[Required]
public string CompressedAssembliesDir { get; set; }

[Required]
public bool Debug { get; set; }

[Required]
public bool EnableCompression { get; set; }

[Required]
public string ProjectFullPath { get; set; }

[Required]
public ITaskItem[] ResolvedUserAssemblies { get; set; }

[Required]
public ITaskItem[] ResolvedFrameworkAssemblies { get; set; }

[Required]
public string [] SupportedAbis { get; set; }

[Output]
public ITaskItem[] NativeAssemblySources { get; set; }

Expand All @@ -29,6 +47,42 @@ public class CreateEmbeddedAssemblyStore : AndroidTask

public override bool RunTask ()
{
bool compress = !Debug && EnableCompression;
IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>? compressedAssembliesInfo = null;

if (compress) {
string key = CompressedAssemblyInfo.GetKey (ProjectFullPath);
Log.LogDebugMessage ($"[{TaskPrefix}] Retrieving assembly compression info with key '{key}'");
compressedAssembliesInfo = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>>> (key, RegisteredTaskObjectLifetime.Build);
if (compressedAssembliesInfo == null) {
throw new InvalidOperationException ($"Assembly compression info not found for key '{key}'. Compression will not be performed.");
}
}

var storeBuilder = new AssemblyStoreBuilder (Log);

// Add user assemblies
AssemblyPackagingHelper.AddAssembliesFromCollection (Log, SupportedAbis, ResolvedUserAssemblies, DoAddAssembliesFromArchCollection);

// Add framework assemblies
AssemblyPackagingHelper.AddAssembliesFromCollection (Log, SupportedAbis, ResolvedFrameworkAssemblies, DoAddAssembliesFromArchCollection);

return !Log.HasLoggedErrors;

void DoAddAssembliesFromArchCollection (TaskLoggingHelper log, AndroidTargetArch arch, ITaskItem assembly)
{
string sourcePath = CompressAssembly (assembly);
storeBuilder.AddAssembly (sourcePath, assembly, includeDebugSymbols: Debug);
return;
}

string CompressAssembly (ITaskItem assembly)
{
if (!compress) {
return assembly.ItemSpec;
}

return AssemblyCompression.Compress (Log, assembly, compressedAssembliesInfo, CompressedAssembliesDir);
}
}
}
11 changes: 7 additions & 4 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.

<_AndroidFastDeployEnvironmentFiles Condition=" '$(_AndroidFastDeployEnvironmentFiles)' == '' And '$(EmbedAssembliesIntoApk)' == 'False' ">True</_AndroidFastDeployEnvironmentFiles>
<_AndroidFastDeployEnvironmentFiles Condition=" '$(_AndroidFastDeployEnvironmentFiles)' == '' ">False</_AndroidFastDeployEnvironmentFiles>

<_AndroidCompressedAssembliesDir>$(IntermediateOutputPath)android\lz4</_AndroidCompressedAssembliesDir>
</PropertyGroup>

<Choose>
Expand Down Expand Up @@ -1869,6 +1869,7 @@ because xbuild doesn't support framework reference assemblies.
<PropertyGroup>
<_CompileToDalvikDependsOnTargets>
_CompileJava;
_RemoveRegisterAttribute;
_CreateApplicationSharedLibraries;
_GetMonoPlatformJarPath;
_GetLibraryImports;
Expand Down Expand Up @@ -1999,7 +2000,7 @@ because xbuild doesn't support framework reference assemblies.
</Target>

<Target Name="_CompileNativeAssemblySources"
DependsOnTargets="_CreateEmbeddedAssemblyStore;_PrepareNativeAssemblyItems;_GenerateCompressedAssembliesNativeSourceFiles"
DependsOnTargets="_PrepareNativeAssemblyItems;_GenerateCompressedAssembliesNativeSourceFiles;_CreateEmbeddedAssemblyStore"
Inputs="@(_TypeMapAssemblySource);@(_TypeMapAssemblyInclude);@(_EnvironmentAssemblySource);@(_CompressedAssembliesAssemblySource);@(_MarshalMethodsAssemblySource);@(_AndroidRemapAssemblySource)"
Outputs="@(_NativeAssemblyTarget)">
<CompileNativeAssembly
Expand Down Expand Up @@ -2113,7 +2114,8 @@ because xbuild doesn't support framework reference assemblies.
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)"
ZipAlignmentPages="$(AndroidZipAlignment)"
UseAssemblyStore="$(AndroidUseAssemblyStore)">
UseAssemblyStore="$(AndroidUseAssemblyStore)"
CompressedAssembliesDir="$(_AndroidCompressedAssembliesDir)">
<Output TaskParameter="OutputFiles" ItemName="ApkFiles" />
</BuildApk>
<BuildBaseAppBundle
Expand Down Expand Up @@ -2151,7 +2153,8 @@ because xbuild doesn't support framework reference assemblies.
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)"
ZipAlignmentPages="$(AndroidZipAlignment)"
UseAssemblyStore="$(AndroidUseAssemblyStore)">
UseAssemblyStore="$(AndroidUseAssemblyStore)"
CompressedAssembliesDir="$(_AndroidCompressedAssembliesDir)">
<Output TaskParameter="OutputFiles" ItemName="BaseZipFile" />
</BuildBaseAppBundle>
<BuildAppBundle
Expand Down

0 comments on commit 7436fbf

Please sign in to comment.