Repository navigation
[release/11.0.1xx-rc1] Fix incremental APK archives and NuGet cache writes #12463
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
jonathanpeppers
merged 2 commits into
release/11.0.1xx-rc1
from
jonathanpeppers-rc1-bug-backports
Aug 21, 2026
Merged
Changes from all commits
Commits
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 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
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; | ||
| 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"); | ||
|
|
||
| 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"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [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")); | ||
|
|
||
| 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"); | ||
| } | ||
| } | ||
|
|
||
| [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 ())}"; | ||
| })); | ||
| } | ||
| } |
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
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.