Skip to content

Commit

Permalink
Enable BuildApkArchive for .aab builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpobst committed Nov 26, 2024
1 parent 40f6309 commit 9109c70
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 72 deletions.
36 changes: 32 additions & 4 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildApkArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class BuildApkArchive : AndroidTask
[Required]
public string Abi { get;set; } = null!; // NRT enforced by [Required]

public string? AndroidPackageFormat { get; set; }

public string? ApkInputPath { get; set; }

[Required]
Expand All @@ -38,9 +40,7 @@ public class BuildApkArchive : AndroidTask
public string? ZipFlushSizeLimit { get; set; }

readonly HashSet<string> uncompressedFileExtensions;

// TODO: Make a property?
protected virtual CompressionMethod UncompressedMethod => CompressionMethod.Store;
readonly CompressionMethod uncompressedMethod = CompressionMethod.Store;

public BuildApkArchive ()
{
Expand All @@ -59,6 +59,10 @@ public BuildApkArchive ()

uncompressedFileExtensions.Add (ext);
}

// Nothing needs to be compressed with app bundles. BundleConfig.json specifies the final compression mode.
if (string.Compare (AndroidPackageFormat, "aab", true) == 0)
uncompressedMethod = CompressionMethod.Default;
}

public override bool RunTask ()
Expand Down Expand Up @@ -200,6 +204,9 @@ public override bool RunTask ()
apk.Archive.DeleteEntry (entry);
}

if (string.Compare (AndroidPackageFormat, "aab", true) == 0)
FixupArchive (apk);

return !Log.HasLoggedErrors;
}

Expand All @@ -219,8 +226,29 @@ bool AddFileToArchiveIfNewer (ZipArchiveEx apk, string file, string inArchivePat
return true;
}

/// <summary>
/// aapt2 is putting AndroidManifest.xml in the root of the archive instead of at manifest/AndroidManifest.xml that bundletool expects.
/// I see no way to change this behavior, so we can move the file for now:
/// https://github.com/aosp-mirror/platform_frameworks_base/blob/e80b45506501815061b079dcb10bf87443bd385d/tools/aapt2/LoadedApk.h#L34
/// </summary>
void FixupArchive (ZipArchiveEx zip)
{
if (!zip.Archive.ContainsEntry ("AndroidManifest.xml")) {
Log.LogDebugMessage ($"No AndroidManifest.xml. Skipping Fixup");
return;
}

var entry = zip.Archive.ReadEntry ("AndroidManifest.xml");
Log.LogDebugMessage ($"Fixing up AndroidManifest.xml to be manifest/AndroidManifest.xml.");

if (zip.Archive.ContainsEntry ("manifest/AndroidManifest.xml"))
zip.Archive.DeleteEntry (zip.Archive.ReadEntry ("manifest/AndroidManifest.xml"));

entry.Rename ("manifest/AndroidManifest.xml");
}

CompressionMethod GetCompressionMethod (string fileName)
{
return uncompressedFileExtensions.Contains (Path.GetExtension (fileName)) ? UncompressedMethod : CompressionMethod.Default;
return uncompressedFileExtensions.Contains (Path.GetExtension (fileName)) ? uncompressedMethod : CompressionMethod.Default;
}
}
4 changes: 2 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildApkTemporary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ bool _Debug {
HashSet<string> uncompressedFileExtensions;

// Do not use trailing / in the path
protected virtual string RootPath => "";
public string RootPath { get; set; } = "";

protected virtual string DalvikPath => "";
public string DalvikPath { get; set; } = "";

protected virtual CompressionMethod UncompressedMethod => CompressionMethod.Store;

Expand Down
105 changes: 39 additions & 66 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2069,76 +2069,29 @@ because xbuild doesn't support framework reference assemblies.
Inputs="$(_BuildApkEmbedInputs)"
Outputs="$(_BuildApkEmbedOutputs)"
Condition="'$(EmbedAssembliesIntoApk)' == 'True'">

<PropertyGroup Condition=" '$(AndroidPackageFormat)' != 'aab' ">
<_AndroidCreatePackagePerAbi>$(AndroidCreatePackagePerAbi)</_AndroidCreatePackagePerAbi>
<_ApkOutputPath>$(ApkFileIntermediate)</_ApkOutputPath>
</PropertyGroup>

<PropertyGroup Condition=" '$(AndroidPackageFormat)' == 'aab' ">
<_AndroidCreatePackagePerAbi>False</_AndroidCreatePackagePerAbi>
<_ApkOutputPath>$(_BaseZipIntermediate)</_ApkOutputPath>
<_RootPath>root/</_RootPath>
<_DalvikPath>dex/</_DalvikPath>
</PropertyGroup>

<!-- Put the assemblies and native libraries in the apk -->
<!--
NOTE: Adding Arguments to BuildApk or BuildBaseAppBundle
also need to have the args added to Xamarin.Android.Common.Debugging.targets
in monodroid.
-->
<BuildApkTemporary
Condition=" '$(AndroidPackageFormat)' != 'aab' "
AndroidNdkDirectory="$(_AndroidNdkDirectory)"
ApkInputPath="$(_PackagedResources)"
ApkOutputPath="$(ApkFileIntermediate)"
AppSharedLibrariesDir="$(_AndroidApplicationSharedLibraryPath)"
BundleNativeLibraries="$(_BundleResultNativeLibraries)"
EmbedAssemblies="$(EmbedAssembliesIntoApk)"
ResolvedUserAssemblies="@(_ShrunkUserAssemblies);@(_AndroidResolvedSatellitePaths)"
ResolvedFrameworkAssemblies="@(_ShrunkFrameworkAssemblies)"
FrameworkNativeLibraries="@(FrameworkNativeLibrary)"
NativeLibraries="@(AndroidNativeLibrary)"
ApplicationSharedLibraries="@(_ApplicationSharedLibrary)"
AdditionalNativeLibraryReferences="@(_AdditionalNativeLibraryReferences)"
EmbeddedNativeLibraryAssemblies="$(OutDir)$(TargetFileName);@(_ReferencePath);@(_ReferenceDependencyPaths)"
DalvikClasses="@(_DexFile)"
SupportedAbis="@(_BuildTargetAbis)"
CreatePackagePerAbi="$(AndroidCreatePackagePerAbi)"
Debug="$(AndroidIncludeDebugSymbols)"
EnableCompression="$(AndroidEnableAssemblyCompression)"
JavaSourceFiles="@(AndroidJavaSource)"
JavaLibraries="@(AndroidJavaLibrary)"
AndroidSequencePointsMode="$(_SequencePointsMode)"
LibraryProjectJars="@(ExtractedJarImports)"
TlsProvider="$(AndroidTlsProvider)"
UncompressedFileExtensions="$(AndroidStoreUncompressedFileExtensions)"
ProjectFullPath="$(MSBuildProjectFullPath)"
IncludeWrapSh="$(AndroidIncludeWrapSh)"
CheckedBuild="$(_AndroidCheckedBuild)"
RuntimeConfigBinFilePath="$(_BinaryRuntimeConfigPath)"
ExcludeFiles="@(AndroidPackagingOptionsExclude)"
IncludeFiles="@(AndroidPackagingOptionsInclude)"
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)"
ZipAlignmentPages="$(AndroidZipAlignment)"
UseAssemblyStore="$(AndroidUseAssemblyStore)"
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
IntermediateOutputPath="$(IntermediateOutputPath)">
<Output TaskParameter="OutputFiles" ItemName="ApkFiles" />
<Output TaskParameter="OutputApkFiles" ItemName="FilesToAddToApk" />
<Output TaskParameter="DSODirectoriesToDelete" ItemName="DSODirectoriesToDelete" />
</BuildApkTemporary>

<!-- This task uses MSBuild task batching. It will be called once for each unique '%(FilesToAddToApk.Abi)'. -->
<BuildApkArchive
Condition=" '$(AndroidPackageFormat)' != 'aab' "
Abi="%(FilesToAddToApk.Abi)"
ApkInputPath="$(_PackagedResources)"
ApkOutputPaths="@(ApkFiles)"
FilesToAddToApk="@(FilesToAddToApk)"
UncompressedFileExtensions="$(AndroidStoreUncompressedFileExtensions)"
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)" />

<!-- Hopefully this is temporary and doesn't actually need to be cleaned up. But for now let's not change existing behavior. -->
<RemoveDir
Condition=" '$(AndroidPackageFormat)' != 'aab' "
Directories="@(DSODirectoriesToDelete)" />

<BuildBaseAppBundle
Condition=" '$(AndroidPackageFormat)' == 'aab' "
AndroidNdkDirectory="$(_AndroidNdkDirectory)"
ApkInputPath="$(_PackagedResources)"
ApkOutputPath="$(_BaseZipIntermediate)"
ApkOutputPath="$(_ApkOutputPath)"
AppSharedLibrariesDir="$(_AndroidApplicationSharedLibraryPath)"
BundleNativeLibraries="$(_BundleResultNativeLibraries)"
EmbedAssemblies="$(EmbedAssembliesIntoApk)"
Expand All @@ -2148,10 +2101,10 @@ because xbuild doesn't support framework reference assemblies.
NativeLibraries="@(AndroidNativeLibrary)"
ApplicationSharedLibraries="@(_ApplicationSharedLibrary)"
AdditionalNativeLibraryReferences="@(_AdditionalNativeLibraryReferences)"
EmbeddedNativeLibraryAssemblies="$(OutDir)$(TargetFileName);@(ReferencePath);@(ReferenceDependencyPaths)"
EmbeddedNativeLibraryAssemblies="$(OutDir)$(TargetFileName);@(_ReferencePath);@(_ReferenceDependencyPaths)"
DalvikClasses="@(_DexFile)"
SupportedAbis="@(_BuildTargetAbis)"
CreatePackagePerAbi="False"
CreatePackagePerAbi="$(_AndroidCreatePackagePerAbi)"
Debug="$(AndroidIncludeDebugSymbols)"
EnableCompression="$(AndroidEnableAssemblyCompression)"
JavaSourceFiles="@(AndroidJavaSource)"
Expand All @@ -2171,9 +2124,29 @@ because xbuild doesn't support framework reference assemblies.
ZipAlignmentPages="$(AndroidZipAlignment)"
UseAssemblyStore="$(AndroidUseAssemblyStore)"
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
IntermediateOutputPath="$(IntermediateOutputPath)">
<Output TaskParameter="OutputFiles" ItemName="BaseZipFile" />
</BuildBaseAppBundle>
IntermediateOutputPath="$(IntermediateOutputPath)"
RootPath="$(_RootPath)"
DalvikPath="$(_DalvikPath)">
<Output TaskParameter="OutputFiles" ItemName="ApkFiles" />
<Output TaskParameter="OutputApkFiles" ItemName="FilesToAddToApk" />
<Output TaskParameter="DSODirectoriesToDelete" ItemName="DSODirectoriesToDelete" />
</BuildApkTemporary>

<!-- This task uses MSBuild task batching. It will be called once for each unique '%(FilesToAddToApk.Abi)'. -->
<BuildApkArchive
Abi="%(FilesToAddToApk.Abi)"
AndroidPackageFormat="$(AndroidPackageFormat)"
ApkInputPath="$(_PackagedResources)"
ApkOutputPaths="@(ApkFiles)"
FilesToAddToApk="@(FilesToAddToApk)"
UncompressedFileExtensions="$(AndroidStoreUncompressedFileExtensions)"
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)" />

<!-- Hopefully this is temporary and doesn't actually need to be cleaned up. But for now let's not change existing behavior. -->
<RemoveDir
Directories="@(DSODirectoriesToDelete)" />

<BuildAppBundle
Condition=" '$(AndroidPackageFormat)' == 'aab' "
ToolPath="$(JavaToolPath)"
Expand Down

0 comments on commit 9109c70

Please # to comment.