-
Notifications
You must be signed in to change notification settings - Fork 534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[XABT] Use ZipArchive to build APKs. #9623
Open
jpobst
wants to merge
4
commits into
main
Choose a base branch
from
use-ziparchive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6194d12
[XABT] Use ZipArchive to build APKs.
jpobst 8a42abe
Add `$(_AndroidUseLibZipSharp)` fallback flag.
jpobst fc4cb32
Fallback to LibZipSharp if we need to store files uncompressed on .NE…
jpobst efef036
Log debug messages for 'ShouldFallbackToLibZipSharp` outcomes.
jpobst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Android.Build.Tasks; | ||
using Microsoft.Build.Framework; | ||
using Xamarin.Tools.Zip; | ||
|
@@ -32,6 +33,8 @@ public class BuildArchive : AndroidTask | |
|
||
public string? UncompressedFileExtensions { get; set; } | ||
|
||
public bool UseLibZipSharp { get; set; } | ||
|
||
public string? ZipFlushFilesLimit { get; set; } | ||
|
||
public string? ZipFlushSizeLimit { get; set; } | ||
|
@@ -43,8 +46,10 @@ public class BuildArchive : AndroidTask | |
|
||
public override bool RunTask () | ||
{ | ||
var is_aab = string.Compare (AndroidPackageFormat, "aab", true) == 0; | ||
|
||
// Nothing needs to be compressed with app bundles. BundleConfig.json specifies the final compression mode. | ||
if (string.Compare (AndroidPackageFormat, "aab", true) == 0) | ||
if (is_aab) | ||
uncompressedMethod = CompressionMethod.Default; | ||
|
||
var refresh = true; | ||
|
@@ -57,7 +62,7 @@ public override bool RunTask () | |
refresh = false; | ||
} | ||
|
||
using var apk = new ZipArchiveEx (ApkOutputPath, FileMode.Open); | ||
using var apk = ZipArchiveDotNet.Create (Log, ApkOutputPath, System.IO.Compression.ZipArchiveMode.Update, ShouldFallbackToLibZipSharp ()); | ||
|
||
// Set up AutoFlush | ||
if (int.TryParse (ZipFlushFilesLimit, out int flushFilesLimit)) { | ||
|
@@ -73,10 +78,9 @@ public override bool RunTask () | |
var existingEntries = new List<string> (); | ||
|
||
if (refresh) { | ||
for (var i = 0; i < apk.Archive.EntryCount; i++) { | ||
var entry = apk.Archive.ReadEntry ((ulong) i); | ||
Log.LogDebugMessage ($"Registering item {entry.FullName}"); | ||
existingEntries.Add (entry.FullName); | ||
foreach (var entry in apk.GetAllEntryNames ()) { | ||
Log.LogDebugMessage ($"Registering item {entry}"); | ||
existingEntries.Add (entry); | ||
} | ||
} | ||
|
||
|
@@ -98,6 +102,11 @@ public override bool RunTask () | |
Log.LogDebugMessage ($"Fixing up malformed entry `{entry.FullName}` -> `{entryName}`"); | ||
} | ||
|
||
if (entryName == "AndroidManifest.xml" && is_aab) { | ||
Log.LogDebugMessage ("Renaming AndroidManifest.xml to manifest/AndroidManifest.xml"); | ||
entryName = "manifest/AndroidManifest.xml"; | ||
} | ||
|
||
Log.LogDebugMessage ($"Deregistering item {entryName}"); | ||
existingEntries.Remove (entryName); | ||
|
||
|
@@ -106,24 +115,28 @@ public override bool RunTask () | |
continue; | ||
} | ||
|
||
if (apk.Archive.ContainsEntry (entryName)) { | ||
ZipEntry e = apk.Archive.ReadEntry (entryName); | ||
if (apk.ContainsEntry (entryName)) { | ||
var e = apk.GetEntry (entryName); | ||
// check the CRC values as the ModifiedDate is always 01/01/1980 in the aapt generated file. | ||
if (entry.CRC == e.CRC && entry.CompressedSize == e.CompressedSize) { | ||
Log.LogDebugMessage ($"Skipping {entryName} from {ApkInputPath} as its up to date."); | ||
continue; | ||
} | ||
|
||
// Delete the existing entry so we can replace it with the new one. | ||
apk.DeleteEntry (entryName); | ||
} | ||
|
||
var ms = new MemoryStream (); | ||
entry.Extract (ms); | ||
ms.Position = 0; | ||
Log.LogDebugMessage ($"Refreshing {entryName} from {ApkInputPath}"); | ||
apk.Archive.AddStream (ms, entryName, compressionMethod: entry.CompressionMethod); | ||
apk.AddEntry (ms, entryName, entry.CompressionMethod.ToCompressionLevel ()); | ||
} | ||
} | ||
} | ||
|
||
apk.FixupWindowsPathSeparators ((a, b) => Log.LogDebugMessage ($"Fixing up malformed entry `{a}` -> `{b}`")); | ||
apk.FixupWindowsPathSeparators (Log); | ||
|
||
// Add the files to the apk | ||
foreach (var file in FilesToAddToArchive) { | ||
|
@@ -135,6 +148,8 @@ public override bool RunTask () | |
return !Log.HasLoggedErrors; | ||
} | ||
|
||
apk_path = apk_path.Replace ('\\', '/'); | ||
|
||
// This is a temporary hack for adding files directly from inside a .jar/.aar | ||
// into the APK. Eventually another task should be writing them to disk and just | ||
// passing us a filename like everything else. | ||
|
@@ -145,7 +160,7 @@ public override bool RunTask () | |
// eg: "obj/myjar.jar#myfile.txt" | ||
var jar_file_path = disk_path.Substring (0, disk_path.Length - (jar_entry_name.Length + 1)); | ||
|
||
if (apk.Archive.Any (ze => ze.FullName == apk_path)) { | ||
if (apk.ContainsEntry (apk_path)) { | ||
Log.LogDebugMessage ("Failed to add jar entry {0} from {1}: the same file already exists in the apk", jar_entry_name, Path.GetFileName (jar_file_path)); | ||
continue; | ||
} | ||
|
@@ -165,7 +180,7 @@ public override bool RunTask () | |
} | ||
|
||
Log.LogDebugMessage ($"Adding {jar_entry_name} from {jar_file_path} as the archive file is out of date."); | ||
apk.AddEntryAndFlush (data, apk_path); | ||
apk.AddEntry (data, apk_path); | ||
} | ||
|
||
continue; | ||
|
@@ -180,63 +195,89 @@ public override bool RunTask () | |
if (string.Compare (Path.GetFileName (entry), "AndroidManifest.xml", StringComparison.OrdinalIgnoreCase) == 0) | ||
continue; | ||
|
||
Log.LogDebugMessage ($"Removing {entry} as it is not longer required."); | ||
apk.Archive.DeleteEntry (entry); | ||
Log.LogDebugMessage ($"Removing {entry} as it is no longer required."); | ||
apk.DeleteEntry (entry); | ||
} | ||
|
||
if (string.Compare (AndroidPackageFormat, "aab", true) == 0) | ||
if (is_aab) | ||
FixupArchive (apk); | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
bool AddFileToArchiveIfNewer (ZipArchiveEx apk, string file, string inArchivePath, ITaskItem item, List<string> existingEntries) | ||
// .NET Framework has a bug where it doesn't handle uncompressed files correctly. | ||
// It writes them as "compressed" (DEFLATE) but with a compression level of 0. This causes | ||
// issues with Android, which expect uncompressed files to be stored correctly. | ||
// We can work around this by using LibZipSharp, which doesn't have this bug. | ||
// This is only necessary if we're on .NET Framework (MSBuild in VSWin) and we have uncompressed files. | ||
bool ShouldFallbackToLibZipSharp () | ||
{ | ||
var compressionMethod = GetCompressionMethod (item); | ||
existingEntries.Remove (inArchivePath.Replace (Path.DirectorySeparatorChar, '/')); | ||
// Explicitly requested via MSBuild property. | ||
if (UseLibZipSharp) { | ||
Log.LogDebugMessage ("Falling back to LibZipSharp because '$(_AndroidUseLibZipSharp)' is 'true'."); | ||
return true; | ||
} | ||
|
||
// .NET 6+ handles uncompressed files correctly, so we don't need to fallback. | ||
if (RuntimeInformation.FrameworkDescription == ".NET") | ||
return false; | ||
|
||
// Nothing is going to get written uncompressed, so we don't need to fallback. | ||
if (uncompressedMethod != CompressionMethod.Store) | ||
return false; | ||
|
||
if (apk.SkipExistingFile (file, inArchivePath, compressionMethod)) { | ||
Log.LogDebugMessage ($"Skipping {file} as the archive file is up to date."); | ||
// No uncompressed file extensions were specified, so we don't need to fallback. | ||
if (UncompressedFileExtensionsSet.Count == 0) | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In each of these cases, can we put a |
||
|
||
// See if any of the files to be added need to be uncompressed. | ||
foreach (var file in FilesToAddToArchive) { | ||
var file_path = file.ItemSpec; | ||
|
||
// Handle files from inside a .jar/.aar | ||
if (file.GetMetadataOrDefault ("JavaArchiveEntry", null) is string jar_entry_name) | ||
file_path = jar_entry_name; | ||
|
||
if (UncompressedFileExtensionsSet.Contains (Path.GetExtension (file_path))) { | ||
Log.LogDebugMessage ($"Falling back to LibZipSharp because '{file_path}' needs to be stored uncompressed."); | ||
return true; | ||
} | ||
} | ||
|
||
Log.LogDebugMessage ($"Adding {file} as the archive file is out of date."); | ||
apk.AddFileAndFlush (file, inArchivePath, compressionMethod); | ||
return false; | ||
} | ||
|
||
bool AddFileToArchiveIfNewer (IZipArchive apk, string file, string inArchivePath, ITaskItem item, List<string> existingEntries) | ||
{ | ||
var compressionMethod = GetCompressionLevel (item); | ||
existingEntries.Remove (inArchivePath.Replace (Path.DirectorySeparatorChar, '/')); | ||
|
||
return true; | ||
return apk.AddFileIfChanged (Log, file, inArchivePath, compressionMethod); | ||
} | ||
|
||
/// <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) | ||
void FixupArchive (IZipArchive zip) | ||
{ | ||
if (!zip.Archive.ContainsEntry ("AndroidManifest.xml")) { | ||
if (!zip.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")); | ||
if (zip.ContainsEntry ("manifest/AndroidManifest.xml")) | ||
zip.DeleteEntry ("manifest/AndroidManifest.xml"); | ||
|
||
entry.Rename ("manifest/AndroidManifest.xml"); | ||
zip.MoveEntry ("AndroidManifest.xml", "manifest/AndroidManifest.xml"); | ||
} | ||
|
||
CompressionMethod GetCompressionMethod (ITaskItem item) | ||
System.IO.Compression.CompressionLevel GetCompressionLevel (ITaskItem item) | ||
{ | ||
var compression = item.GetMetadataOrDefault ("Compression", ""); | ||
|
||
if (compression.HasValue ()) { | ||
if (Enum.TryParse (compression, out CompressionMethod result)) | ||
return result; | ||
} | ||
|
||
return UncompressedFileExtensionsSet.Contains (Path.GetExtension (item.ItemSpec)) ? uncompressedMethod : CompressionMethod.Default; | ||
return (UncompressedFileExtensionsSet.Contains (Path.GetExtension (item.ItemSpec)) ? uncompressedMethod : CompressionMethod.Default).ToCompressionLevel (); | ||
} | ||
|
||
HashSet<string> ParseUncompressedFileExtensions () | ||
|
46 changes: 46 additions & 0 deletions
46
src/Xamarin.Android.Build.Tasks/Utilities/UtilityExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using Xamarin.Tools.Zip; | ||
|
||
namespace Xamarin.Android.Tasks; | ||
|
||
static class UtilityExtensions | ||
{ | ||
public static System.IO.Compression.CompressionLevel ToCompressionLevel (this CompressionMethod method) | ||
{ | ||
switch (method) { | ||
case CompressionMethod.Store: | ||
return System.IO.Compression.CompressionLevel.NoCompression; | ||
case CompressionMethod.Default: | ||
case CompressionMethod.Deflate: | ||
return System.IO.Compression.CompressionLevel.Optimal; | ||
default: | ||
throw new ArgumentOutOfRangeException (nameof (method), method, null); | ||
} | ||
} | ||
|
||
public static CompressionMethod ToCompressionMethod (this System.IO.Compression.CompressionLevel level) | ||
{ | ||
switch (level) { | ||
case System.IO.Compression.CompressionLevel.NoCompression: | ||
return CompressionMethod.Store; | ||
case System.IO.Compression.CompressionLevel.Optimal: | ||
return CompressionMethod.Deflate; | ||
default: | ||
throw new ArgumentOutOfRangeException (nameof (level), level, null); | ||
} | ||
} | ||
|
||
public static FileMode ToFileMode (this ZipArchiveMode mode) | ||
{ | ||
switch (mode) { | ||
case ZipArchiveMode.Create: | ||
return FileMode.Create; | ||
case ZipArchiveMode.Update: | ||
return FileMode.Open; | ||
default: | ||
throw new ArgumentOutOfRangeException (nameof (mode), mode, null); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone has
$(AndroidPackageFormats)=aab;apk
, does the right value come through here?I think this is the default value for
Release
mode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AndroidPackageFormat
gets calculated fromAndroidPackageFormats
.