Skip to content

Commit dd00d48

Browse files
[xabt] Fix @(AndroidLibrary) with %(Pack)=false (#10365)
Fixes: #8504 The `<CreateAar/>` MSBuild task was not respecting the `%(Pack)=false` metadata on `@(AndroidLibrary)` items, causing jar files to be included in AAR packages even when explicitly marked for exclusion. This caused issues for developers who wanted to: - Reference binding projects without packaging the underlying jar files - Create modular architectures where jars are packaged separately - Avoid duplicate jar inclusion in complex dependency scenarios ## Solution Modified the jar processing loop in `CreateAar.cs` to: - Check the `Pack` metadata on each jar file item - Skip jars with `Pack='false'` using `continue;` - Add debug logging for transparency when jars are skipped - Preserve default behavior (include jars when Pack is unspecified or 'true') ## Testing Added comprehensive test `AndroidLibraryPackFalseExcludesJarFromAar` that: - Creates a binding project with both `Pack='false'` and default Pack jars - Verifies the AAR is generated successfully - Examines actual AAR contents to confirm Pack='false' jars are excluded - Confirms default behavior jars are still included Co-authored-by: Jonathan Peppers <[email protected]>
1 parent 19844f8 commit dd00d48

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public override bool RunTask ()
100100
}
101101
if (JarFiles != null) {
102102
foreach (var jar in JarFiles) {
103+
var pack = jar.GetMetadata ("Pack");
104+
if (string.Equals (pack, "false", StringComparison.OrdinalIgnoreCase)) {
105+
Log.LogDebugMessage ($"Skipping jar '{jar.ItemSpec}' because Pack='false'");
106+
continue;
107+
}
103108
var archivePath = $"libs/{GetHashedFileName (jar)}.jar";
104109
aar.AddStream (File.OpenRead (jar.ItemSpec), archivePath);
105110
existingEntries.Remove (archivePath);

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BindingBuildTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Xamarin.ProjectTools;
1010
using Microsoft.Android.Build.Tasks;
1111
using Microsoft.Build.Framework;
12+
using Xamarin.Tools.Zip;
1213

1314
namespace Xamarin.Android.Build.Tests
1415
{
@@ -437,6 +438,43 @@ public void OnUpdate (Java.Lang.Object p0)
437438
}
438439
}
439440

441+
[Test]
442+
public void AndroidLibraryPackFalseExcludesJarFromAar ()
443+
{
444+
var binding = new XamarinAndroidBindingProject () {
445+
IsRelease = true,
446+
};
447+
// Add a jar with Pack='false' - should not be included in AAR
448+
binding.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("Jars\\test-pack-false.jar") {
449+
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
450+
MetadataValues = "Pack=false;Bind=false",
451+
});
452+
// Add a jar with Pack='true' (default) - should be included in AAR
453+
binding.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("Jars\\test-pack-true.jar") {
454+
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
455+
MetadataValues = "Bind=false",
456+
});
457+
458+
using (var bindingBuilder = CreateDllBuilder ()) {
459+
Assert.IsTrue (bindingBuilder.Build (binding), "binding build should have succeeded");
460+
461+
// Check that the AAR file was created
462+
var aarPath = Path.Combine (Root, bindingBuilder.ProjectDirectory, binding.OutputPath, "UnnamedProject.aar");
463+
FileAssert.Exists (aarPath);
464+
465+
// Extract and examine AAR contents
466+
using (var aar = ZipArchive.Open (aarPath, FileMode.Open)) {
467+
// test-pack-false.jar should NOT be in the AAR because Pack='false'
468+
var packFalseEntry = aar.Where (e => e.FullName.Contains ("test-pack-false")).FirstOrDefault ();
469+
Assert.IsNull (packFalseEntry, "Jar with Pack='false' should not be included in AAR");
470+
471+
// test-pack-true.jar should be in the AAR (default Pack='true')
472+
var packTrueEntry = aar.Where (e => e.FullName.Contains ("test-pack-true") || e.FullName.StartsWith ("libs/")).FirstOrDefault ();
473+
Assert.IsNotNull (packTrueEntry, "Jar with Pack='true' (default) should be included in AAR");
474+
}
475+
}
476+
}
477+
440478
[Test]
441479
public void RemoveEventHandlerResolution ()
442480
{

0 commit comments

Comments
 (0)