From 39e3519defdf78bd8242e518ef711fd252f03a73 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Wed, 1 Feb 2023 13:07:14 +0100 Subject: [PATCH 1/3] Maybe fix the `unable to open file as zip archive` error Context: https://gist.github.com/grendello/55b9a74c80c611dd48b726ee6f16d3c9 Sometimes in our tests we see an error from LibZipSharp which claims an file cannot be opened as a valid ZIP archive. The reason for this is ZIP format corruption happening sometimes in the `BuildApk` task. The way the process works is that we first run `aapt2` to produce an APK archive named `packaged_resources`, which we then copy to the destination APK (using standard `File.Copy`) and open it to append our content. The first item we always append is the `classes.dex` file which is also the **only** corrupted entry in the broken APK files. ZIP files contain two records describing each entry in the archive: 1. in the Central Directory (which is located at the end of the file) 2. local header in the ZIP data stream, offset of whose is contained in the above Central Directory record. After close examination, it appears that the broken APK files contain `1` above for `classes.dex` but not `2`. Instead of `2` we see a copy of `packaged_resource` package's Central Directory record. The corruption may be caused by invalid stream position after we open the `packaged_resources` copy and append `classes.dex` entry to it. Calling `Flush ()` after adding `classex.dex` **may** fix the problem. If it doesn't, we should consider copying data from `packaged_resources` to the final APK entry by entry by decompressing them from the original archive and adding them to the destination one. --- src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs index 28cca622f9b..b3998d336cb 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs @@ -183,6 +183,7 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut string apkName = dex.GetMetadata ("ApkName"); string dexPath = string.IsNullOrWhiteSpace (apkName) ? Path.GetFileName (dex.ItemSpec) : apkName; AddFileToArchiveIfNewer (apk, dex.ItemSpec, DalvikPath + dexPath, compressionMethod: dexCompressionMethod); + apk.Flush (); } if (EmbedAssemblies && !BundleAssemblies) { From 8214a43d7b9915d250cfd16900035fff61e649f1 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Mon, 27 Feb 2023 15:09:18 +0100 Subject: [PATCH 2/3] Bump monodroid and LibZipSharp --- .external | 2 +- Directory.Build.props | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.external b/.external index 1f60e2f5032..0deda634333 100644 --- a/.external +++ b/.external @@ -1,2 +1,2 @@ -xamarin/monodroid:main@50faac94c6a0c27864564829ac83f3988c82f8ef +xamarin/monodroid:main@602aca98245744882a129206b79b5a5e093dae44 mono/mono:2020-02@6dd9def57ce969ca04a0ecd9ef72c0a8f069112d diff --git a/Directory.Build.props b/Directory.Build.props index 431d9255db4..33a80a4adcf 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -40,7 +40,7 @@ - 2.0.7 + 2.1.0 1.0.0 0.11.4 13.0.1 From 94785658dba62927961cbe2c8704081b2eb1378a Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Mon, 27 Feb 2023 17:44:50 +0100 Subject: [PATCH 3/3] Bump xamarin-android-tools --- external/xamarin-android-tools | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/xamarin-android-tools b/external/xamarin-android-tools index 099fd95f545..dbe42bf1688 160000 --- a/external/xamarin-android-tools +++ b/external/xamarin-android-tools @@ -1 +1 @@ -Subproject commit 099fd95f5459d9df78af0ad28f46e3016dd7ca1d +Subproject commit dbe42bf1688e79e1ca474dff4a1f8060158cafd9