Repository navigation
[Xamarin.Android.Build.Tasks] Replace ExtractJarsFromAar with built-in Unzip - #13506
Open
simonrozsival wants to merge 1 commit into
Open
simonrozsival wants to merge 1 commit into
simonrozsival wants to merge 1 commit into
Conversation
…n Unzip _ExtractAar now stages the .jar and annotations.zip entries with <Unzip/>, then uses the retained CopyIfChanged task to rewrite only files whose content changed, and deletes files that are no longer in any archive. Incremental behavior matches the removed task. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
simonrozsival
marked this pull request as ready for review
November 11, 2026 14:33
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The final copy step omits descendant-link validation, allowing archive files to be written through symlinks or junctions outside the intended extraction directories.
5 open issues
Validate final destinations against symlink and junction traversal · New Normalize ZIP entry separators before extracting on Unix · New Disable SkipUnchangedFiles to preserve later-archive-wins behavior · New Preserve platform-aware path comparison when finding stale files · New Add regression coverage for staging, copying, and stale-file cleanup · New
What changed in this PR
Replaces the custom AAR extraction task with MSBuild’s built-in Unzip, reducing custom task maintenance in the Android binding build pipeline.
Changes:
- Adds staged extraction for bound and reference JARs and annotation archives.
- Uses timestamp refresh, content-aware copying, and stale-file deletion to preserve incremental behavior.
- Removes
ExtractJarsFromAar, its task registration, and its dedicated tests.
| File | Description |
|---|---|
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/ExtractJarsFromAarTests.cs |
Removes the custom extractor’s tests. |
src/Xamarin.Android.Build.Tasks/Tasks/ExtractJarsFromAar.cs |
Removes the custom extraction implementation. |
src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AndroidLibraries.targets |
Replaces the task invocation with staged extraction, copying, and cleanup. |
Review effort: Balanced
| </ItemGroup> | ||
| <!-- Unzip keeps the zip entry timestamps; touch the staged files so CopyIfChanged compares content and only rewrites changed files --> | ||
| <Touch Files="@(_ExtractAarFile)" /> | ||
| <CopyIfChanged SourceFiles="@(_ExtractAarFile)" DestinationFiles="@(_ExtractAarFile->'$(IntermediateOutputPath)%(RecursiveDir)%(Filename)%(Extension)')" /> |
| <Unzip | ||
| SourceFiles="@(_ExtractAarLibrary)" | ||
| DestinationFolder="$(_ExtractAarStagingDirectory)%(OutputPrefix)_jars\%(Filename)%(Extension)" | ||
| Include="classes.jar;libs/*.jar" |
Comment on lines
+122
to
138
| <Unzip | ||
| SourceFiles="@(_ExtractAarLibrary)" | ||
| DestinationFolder="$(_ExtractAarStagingDirectory)%(OutputPrefix)_jars\%(Filename)%(Extension)" | ||
| Include="*.jar" | ||
| Condition=" '%(Extension)' != '.aar' " | ||
| /> | ||
| <Unzip | ||
| SourceFiles="@(_ExtractAarLibrary)" | ||
| DestinationFolder="$(_ExtractAarStagingDirectory)%(OutputPrefix)_jars\%(Filename)%(Extension)" | ||
| Include="classes.jar;libs/*.jar" | ||
| Condition=" '%(Extension)' == '.aar' " | ||
| /> | ||
| <Unzip | ||
| SourceFiles="@(_ExtractAarLibrary)" | ||
| DestinationFolder="$(_ExtractAarStagingDirectory)%(OutputPrefix)_annotations\%(Filename)%(Extension)" | ||
| Include="annotations.zip;*/annotations.zip" | ||
| /> |
| <_ExtractAarFile Include="$(_ExtractAarStagingDirectory)**" /> | ||
| <_ExtractAarStaleFile | ||
| Include="$(IntermediateOutputPath)library_project_jars\**;$(IntermediateOutputPath)library_project_annotations\**;$(IntermediateOutputPath)reference_library_project_jars\**;$(IntermediateOutputPath)reference_library_project_annotations\**" | ||
| Exclude="@(_ExtractAarFile->'$(IntermediateOutputPath)%(RecursiveDir)%(Filename)%(Extension)')" |
Comment on lines
+146
to
+149
| <!-- Unzip keeps the zip entry timestamps; touch the staged files so CopyIfChanged compares content and only rewrites changed files --> | ||
| <Touch Files="@(_ExtractAarFile)" /> | ||
| <CopyIfChanged SourceFiles="@(_ExtractAarFile)" DestinationFiles="@(_ExtractAarFile->'$(IntermediateOutputPath)%(RecursiveDir)%(Filename)%(Extension)')" /> | ||
| <Delete Files="@(_ExtractAarStaleFile)" /> |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Context: #11212
Context: #13505
Context: https://github.com/dotnet/msbuild/blob/main/src/Tasks/Unzip.cs
The
_ExtractAartarget used the customExtractJarsFromAartask to extract*.jarandannotations.zipentries from@(LibraryProjectZip)and@(_AndroidReferenceLibraryProjectZip)into$(IntermediateOutputPath)library_project_jars,library_project_annotationsand theirreference_*counterparts. That is one more custom task to host, test and eventually make multithreaded, while MSBuild's<Unzip/>already supportsIncludepatterns.~~ Why not just
<Unzip SkipUnchangedFiles="true"/>? ~~The incrementality of the extracted files matters.
_ExportJarToXmluses@(InputJar)and@(ReferenceJar)(the extracted jars) asInputs.ExtractJarsFromAarrewrote a file only when its content changed, and it deleted files that no longer came from any archive.<Unzip/>sets every extracted file's mtime to the zip entry's timestamp. ItsSkipUnchangedFilescheck compares only entry timestamp and size. Gradle/AGP-produced AARs typically use fixed timestamps (1980-02-01), so:classes.jarwould be written with a 1980 mtime, and_ExportJarToXmlwould treat itself as up to date, leaving bindings stale;classes.jarof the same size would not be extracted at all.~~ Approach ~~
_ExtractAarnow:$(IntermediateOutputPath)extract_aar_staging\, using three batched<Unzip/>calls per library:.aarinputs:Include="*.jar".aarinputs:Include="classes.jar;libs/*.jar", which mirrorsFiles.ShouldSkipEntryInAar()Include="annotations.zip;*/annotations.zip"<Touch/>es the staged files.CopyIfChangedwould otherwise skip a file whose destination is newer and has the same length.CopyIfChangedtask. It compares content, so unchanged files keep their timestamps.<Delete/>s stale files: everything under the four output directories minus the expected set, computed with anExcludetransform over%(RecursiveDir).This keeps the previous incremental semantics, and the target's
Inputs/Outputsare unchanged. javac and D8 are unaffected: neither reads these directories (their jars come fromResolveLibraryProjectImports). The only consumers are class-parse and the generator'sAnnotationsZipFiles. The added cost is one temporary disk write of the matched entries, and only when_ExtractAarruns.<Unzip/>pattern notes:*also matches/, and patterns are separator-normalized only whenIncludecontains;. That is why every pattern containing a path separator is part of a;-list.~~ Behavior differences ~~
<Unzip/>'s own zip-slip check instead ofFiles.GetArchiveExtractionPath(). Because<Unzip/>filters before it validates, a traversal.jarentry in an.aarthat doesn't matchclasses.jar/libs/*is now ignored instead of failing the build. Nothing is written outside the destination in either case.classes.jarmatching is now case-insensitive.ExtractJarsFromAar, its<UsingTask/>andExtractJarsFromAarTestsare removed.Files.ShouldSkipEntryInAar()andFiles.GetArchiveExtractionPath()remain, becauseResolveLibraryProjectImportsuses them.~~ Testing ~~
Not built or tested locally, to save disk space in this batch; validation is left to CI. Relevant coverage is in
BindingBuildTest: theannotations.zipsupport test and thelint.jarexclusion check, plus the binding incremental build tests.