Repository navigation
Fix BuildArchive updates for existing jar entries #11552
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
Merged
simonrozsival
merged 11 commits into
main
from
copilot/fix-buildarchive-incremental-build-bug
Aug 18, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
13eb4b3
Initial plan
Copilot 31bde61
Fix BuildArchive jar entry retention
Copilot c19e3aa
Update stale BuildArchive jar entries
Copilot ff69351
Cover up-to-date BuildArchive jar entries
Copilot 6ee59ed
Address BuildArchive review feedback
Copilot f70a57f
Address BuildArchive validation feedback
Copilot e4aad98
Address review: null-check jar.ReadEntry result; create secondJar on …
Copilot 7de0873
Address review: consistent log path format, re-add stale entry to cle…
Copilot 1ebf996
Address review: [] collection expressions in tests, CRC comment, Cont…
Copilot 1e9d788
Test consecutive BuildArchive updates
jonathanpeppers 313b211
Merge branch 'main' into copilot/fix-buildarchive-incremental-build-bug
simonrozsival 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
189 changes: 189 additions & 0 deletions
189
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/BuildArchiveTests.cs
This file contains hidden or 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,189 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using Microsoft.Android.Build.Tasks; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using NUnit.Framework; | ||
| using Xamarin.Android.Tasks; | ||
| using Xamarin.Tools.Zip; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests; | ||
|
|
||
| [TestFixture] | ||
| public class BuildArchiveTests | ||
| { | ||
| string? tempDirectory; | ||
|
|
||
| string TempDirectory => tempDirectory ?? throw new InvalidOperationException ("Setup has not run."); | ||
|
|
||
| [SetUp] | ||
| public void Setup () | ||
| { | ||
| tempDirectory = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ()); | ||
| Directory.CreateDirectory (tempDirectory); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown () | ||
| { | ||
| if (!tempDirectory.IsNullOrEmpty () && Directory.Exists (tempDirectory)) | ||
| Directory.Delete (tempDirectory, recursive: true); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConsecutiveUnchangedBuildsKeepJavaArchiveEntries () | ||
| { | ||
| var apk = Path.Combine (TempDirectory, "app.apk"); | ||
| var jar = Path.Combine (TempDirectory, "classes.jar"); | ||
|
|
||
| CreateArchive (apk, ("AndroidManifest.xml", "manifest"), ("commonMain/default/manifest", "existing"), ("stale.txt", "stale")); | ||
| CreateArchive (jar, ("commonMain/default/manifest", "current")); | ||
|
|
||
| var item = new TaskItem ($"{jar}#commonMain/default/manifest"); | ||
| item.SetMetadata ("ArchivePath", "commonMain/default/manifest"); | ||
| item.SetMetadata ("JavaArchiveEntry", "commonMain/default/manifest"); | ||
| string? previousSnapshot = null; | ||
|
|
||
| for (var build = 1; build <= 3; build++) { | ||
| var task = new BuildArchive { | ||
| BuildEngine = new MockBuildEngine (TestContext.Out), | ||
| ApkOutputPath = apk, | ||
| FilesToAddToArchive = [item], | ||
| }; | ||
|
|
||
| Assert.IsTrue (task.RunTask (), $"build {build} should have succeeded"); | ||
|
|
||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
| var snapshot = GetArchiveSnapshot (apk); | ||
| if (previousSnapshot is not null) | ||
| Assert.AreEqual (previousSnapshot, snapshot, $"build {build} should match the previous unchanged build"); | ||
| previousSnapshot = snapshot; | ||
|
|
||
| using (var archive = ZipArchive.Open (apk, FileMode.Open)) { | ||
| archive.AssertEntryContents (apk, "commonMain/default/manifest", "current"); | ||
| archive.AssertDoesNotContainEntry (apk, "stale.txt"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
| [Test] | ||
| public void ExistingJavaArchiveEntriesAreSkippedWhenUpToDate () | ||
| { | ||
| var apk = Path.Combine (TempDirectory, "app.apk"); | ||
| var jar = Path.Combine (TempDirectory, "classes.jar"); | ||
|
|
||
| CreateArchive (apk, ("commonMain/default/manifest", "current")); | ||
| CreateArchive (jar, ("commonMain/default/manifest", "current")); | ||
|
|
||
| var item = new TaskItem ($"{jar}#commonMain/default/manifest"); | ||
| item.SetMetadata ("ArchivePath", "commonMain/default/manifest"); | ||
| item.SetMetadata ("JavaArchiveEntry", "commonMain/default/manifest"); | ||
| var messages = new List<BuildMessageEventArgs> (); | ||
|
|
||
| var task = new BuildArchive { | ||
| BuildEngine = new MockBuildEngine (TestContext.Out, messages: messages), | ||
| ApkOutputPath = apk, | ||
| FilesToAddToArchive = [item], | ||
| }; | ||
|
|
||
| Assert.IsTrue (task.RunTask (), "task should have succeeded"); | ||
|
|
||
| Assert.That (messages, Has.Some.Property (nameof (BuildMessageEventArgs.Message)).EqualTo ($"Skipping commonMain/default/manifest from {jar} as it is up to date.")); | ||
|
|
||
| using (var archive = ZipArchive.Open (apk, FileMode.Open)) { | ||
| archive.AssertEntryContents (apk, "commonMain/default/manifest", "current"); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void DuplicateJavaArchiveEntriesKeepFirstCurrentBuildItem () | ||
| { | ||
| var apk = Path.Combine (TempDirectory, "app.apk"); | ||
| var firstJar = Path.Combine (TempDirectory, "first.jar"); | ||
| var secondJar = Path.Combine (TempDirectory, "second.jar"); | ||
|
|
||
| CreateArchive (apk, ("stale.txt", "stale")); | ||
| CreateArchive (firstJar, ("commonMain/default/manifest", "first")); | ||
| CreateArchive (secondJar, ("commonMain/default/manifest", "second")); | ||
|
|
||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
| var firstItem = new TaskItem ($"{firstJar}#commonMain/default/manifest"); | ||
| firstItem.SetMetadata ("ArchivePath", "commonMain/default/manifest"); | ||
| firstItem.SetMetadata ("JavaArchiveEntry", "commonMain/default/manifest"); | ||
| var secondItem = new TaskItem ($"{secondJar}#commonMain/default/manifest"); | ||
| secondItem.SetMetadata ("ArchivePath", "commonMain/default/manifest"); | ||
| secondItem.SetMetadata ("JavaArchiveEntry", "commonMain/default/manifest"); | ||
| var messages = new List<BuildMessageEventArgs> (); | ||
|
|
||
| var task = new BuildArchive { | ||
| BuildEngine = new MockBuildEngine (TestContext.Out, messages: messages), | ||
| ApkOutputPath = apk, | ||
| FilesToAddToArchive = [firstItem, secondItem], | ||
| }; | ||
|
|
||
| Assert.IsTrue (task.RunTask (), "task should have succeeded"); | ||
|
|
||
| Assert.That (messages, Has.Some.Property (nameof (BuildMessageEventArgs.Message)).EqualTo ("Failed to add jar entry commonMain/default/manifest from second.jar: the same file already exists in the apk")); | ||
|
|
||
| using (var archive = ZipArchive.Open (apk, FileMode.Open)) { | ||
| archive.AssertEntryContents (apk, "commonMain/default/manifest", "first"); | ||
| archive.AssertDoesNotContainEntry (apk, "stale.txt"); | ||
| } | ||
| } | ||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
|
|
||
| [Test] | ||
| public void MissingJarEntryIsSkippedAndExistingOutputEntryIsRemoved () | ||
| { | ||
| var apk = Path.Combine (TempDirectory, "app.apk"); | ||
| var jar = Path.Combine (TempDirectory, "classes.jar"); | ||
|
|
||
| CreateArchive (apk, ("commonMain/default/manifest", "existing")); | ||
| CreateArchive (jar, ("other-entry.txt", "contents")); | ||
|
|
||
| var item = new TaskItem ($"{jar}#commonMain/default/manifest"); | ||
| item.SetMetadata ("ArchivePath", "commonMain/default/manifest"); | ||
| item.SetMetadata ("JavaArchiveEntry", "commonMain/default/manifest"); | ||
| var messages = new List<BuildMessageEventArgs> (); | ||
|
|
||
| var task = new BuildArchive { | ||
| BuildEngine = new MockBuildEngine (TestContext.Out, messages: messages), | ||
| ApkOutputPath = apk, | ||
| FilesToAddToArchive = [item], | ||
| }; | ||
|
|
||
| Assert.IsTrue (task.RunTask (), "task should have succeeded"); | ||
|
|
||
| Assert.That (messages, Has.Some.Property (nameof (BuildMessageEventArgs.Message)).EqualTo ($"Failed to add jar entry commonMain/default/manifest from {jar}: entry not found in jar.")); | ||
|
|
||
| // The entry should be removed. If the APK itself no longer exists, all entries were cleared (also satisfies the assertion). | ||
| if (File.Exists (apk)) { | ||
| using (var archive = ZipArchive.Open (apk, FileMode.Open)) { | ||
| archive.AssertDoesNotContainEntry (apk, "commonMain/default/manifest"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void CreateArchive (string path, params (string name, string contents) [] entries) | ||
| { | ||
| using (var stream = File.Create (path)) | ||
| using (var archive = ZipArchive.Create (stream)) { | ||
| foreach (var entry in entries) { | ||
| archive.AddEntry (entry.name, entry.contents, encoding: Encoding.UTF8); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static string GetArchiveSnapshot (string path) | ||
| { | ||
| using var archive = ZipArchive.Open (path, FileMode.Open); | ||
| return string.Join ("\n", archive | ||
| .OrderBy (entry => entry.FullName, StringComparer.Ordinal) | ||
| .Select (entry => { | ||
| using var stream = new MemoryStream (); | ||
| entry.Extract (stream); | ||
| return $"{entry.FullName}:{Convert.ToBase64String (stream.ToArray ())}"; | ||
| })); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.