Skip to content

Commit d653245

Browse files
authored
[Xamarin.Android.Build.Tasks] AndroidPackageFormat=aab incr build (#5760)
Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1288593 This is a weird bug which I cannot reproduce locally. However, reading the code this code path is possible: When trying to do incremental builds when using `$(AndroidPackageFormat)`=aab, we get the following error: [BT : 1.4.0] error : Module 'base' is missing mandatory file 'manifest/AndroidManifest.xml'. The problem is that `base.zip` doesn't have *any* `AndroidManifest.xml` in it! But why? What appears to happen is: 1. During "initial build", `packaged_resources` is created, and contains `AndroidManifest.xml`. `base.zip` is created based on the contents of `packaged_resources`, renaming `AndroidManifest.xml` to `manifest/AndroidManifest.xml`, so that `base.zip` has the expected folder structure for `aab` files. (This "copy and rename" is done by `<FixupArchive/>`.) Both `packaged_resources` and `base.zip` have the same timestamp. 2. On a subsequent incremental build, neither `packaged_resources` nor `base.zip` is changed (because they didn't need to, e.g. no Android Resources were added), and the `_BuildApkEmbed` target is executed. 3. The `_BuildApkEmbed` target runs the `<BuildApk/>` task, which "cleans up removed files" within `base.zip`. Because there is no corresponding file "on disk" for `manifest/AndroidManifest.xml` within `base.zip`, the `manifest/AndroidManifest.xml` file is *removed* from `base.zip`. 4. `_BuildApkEmbed` later calls `<BuildAppBundle/>`, which calls `bundletool.jar`, and fails with the above error message: [BT : 1.4.0] error : Module 'base' is missing mandatory file 'manifest/AndroidManifest.xml'. The fix is to update the `<BuildApk/>` task to *never remove* files named `AndroidManifest.xml`. It is a protected file and should always exist.
1 parent 0192669 commit d653245

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut
158158
}
159159
Log.LogDebugMessage ($"Deregistering item {entryName}");
160160
existingEntries.Remove (entryName);
161-
if (lastWriteInput <= lastWriteOutput)
161+
if (lastWriteInput <= lastWriteOutput) {
162+
Log.LogDebugMessage ($"Skipping to next item. {lastWriteInput} <= {lastWriteOutput}.");
162163
continue;
164+
}
163165
if (apk.Archive.ContainsEntry (entryName)) {
164166
ZipEntry e = apk.Archive.ReadEntry (entryName);
165167
// check the CRC values as the ModifiedDate is always 01/01/1980 in the aapt generated file.
@@ -266,6 +268,9 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut
266268
}
267269
// Clean up Removed files.
268270
foreach (var entry in existingEntries) {
271+
// never remove an AndroidManifest. It may be renamed when using aab.
272+
if (string.Compare (Path.GetFileName (entry), "AndroidManifest.xml", StringComparison.OrdinalIgnoreCase) == 0)
273+
continue;
269274
Log.LogDebugMessage ($"Removing {entry} as it is not longer required.");
270275
apk.Archive.DeleteEntry (entry);
271276
}

src/Xamarin.Android.Build.Tasks/Tasks/BuildBaseAppBundle.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.IO;
55
using Xamarin.Tools.Zip;
6+
using Microsoft.Android.Build.Tasks;
67

78
namespace Xamarin.Android.Tasks
89
{
@@ -32,9 +33,12 @@ public class BuildBaseAppBundle : BuildApk
3233
/// </summary>
3334
protected override void FixupArchive (ZipArchiveEx zip)
3435
{
35-
if (!zip.Archive.ContainsEntry ("AndroidManifest.xml"))
36+
if (!zip.Archive.ContainsEntry ("AndroidManifest.xml")) {
37+
Log.LogDebugMessage ($"No AndroidManifest.xml. Skipping Fixup");
3638
return;
39+
}
3740
var entry = zip.Archive.ReadEntry ("AndroidManifest.xml");
41+
Log.LogDebugMessage ($"Fixing up AndroidManifest.xml to be manifest/AndroidManifest.xml.");
3842
using (var stream = new MemoryStream ()) {
3943
entry.Extract (stream);
4044
stream.Position = 0;

0 commit comments

Comments
 (0)