Skip to content

Commit 3a89e8d

Browse files
[Xamarin.Android.Build.Tasks] fix duplicate .aar files (#8196)
Context: dotnet/maui#16024 (comment) .NET MAUI's build currently fails with: Xamarin.Android.Aapt2.targets(123,3): error APT2144: invalid file path 'D:\a\_work\1\s\src\Core\src\obj\Debug\net8.0-android\lp\129.stamp'. What is very wrong about this, it is trying to `aapt2 compile` a `*.stamp` file: Executing compile -o /Users/builder/azdo/_work/1/s/src/Core/src/obj/Release/net8.0-android/lp/87/jl/res/../flat/ /Users/builder/azdo/_work/1/s/src/Core/src/obj/Release/net8.0-android/lp/87.stamp Normally this runs against `*.flat` or `*.flata` files. This problem was introduced in 26ffd5d: 1. Library A uses an AndroidX package, the AndroidX `.aar` file is added to `@(AndroidAarLibrary)`. The NuGet package does this in a `.targets` file. 2. With the change in 26ffd5d, the `.aar` is copied to Library A's build output. 3. Library B uses the same AndroidX package and references Library A. 4. Library B now has duplicate `.aar` files & has the weird build error! I could reproduce the issue in a test. There *may* be a second bug here, but we should update our logic to be: <!-- .aar files should be copied to $(OutputPath) in .NET 6--> <None Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' " ... /> <!-- @(LibraryProjectZip) items that are not in @(AndroidLibrary) --> <None Include="@(LibraryProjectZip)" Exclude="@(AndroidLibrary)" ... /> So we now only copy: * The new `@(AndroidLibrary)` item group with an `.aar` extension. *Not* `@(AndroidAarLibrary)`. * Any `@(LibraryProjectZip)` that are *not* in `@(AndroidLibrary)`. This supports the classic item group name, keeping our behavior before. Now the new test and the test updated in 26ffd5d both pass.
1 parent 5507af9 commit 3a89e8d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ This item group populates the Build Action drop-down in IDEs.
103103
<AndroidJavaLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' and '%(AndroidLibrary.Bind)' != 'true' " />
104104
<EmbeddedJar Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' and '%(AndroidLibrary.Bind)' == 'true' " />
105105
<!-- .aar files should be copied to $(OutputPath) in .NET 6-->
106-
<None Include="@(AndroidAarLibrary)" TfmSpecificPackageFile="%(AndroidAarLibrary.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
107-
<None Include="@(LibraryProjectZip)" TfmSpecificPackageFile="%(LibraryProjectZip.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
106+
<None Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' " TfmSpecificPackageFile="%(AndroidLibrary.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
107+
<!-- @(LibraryProjectZip) items that are not in @(AndroidLibrary) -->
108+
<None Include="@(LibraryProjectZip)" Exclude="@(AndroidLibrary)" TfmSpecificPackageFile="%(LibraryProjectZip.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
108109
</ItemGroup>
109110
<!-- Legacy binding projects -->
110111
<ItemGroup Condition=" '$(_AndroidIsBindingProject)' == 'true' and '$(UsingAndroidNETSdk)' != 'true' ">

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,5 +2577,38 @@ public void SimilarAndroidXAssemblyNames ([Values(true, false)] bool publishTrim
25772577
using var builder = CreateApkBuilder ();
25782578
Assert.IsTrue (builder.Build (proj), "Build should have succeeded.");
25792579
}
2580+
2581+
// Combination of class libraries that triggered the problem:
2582+
// error APT2144: invalid file path 'obj/Release/net8.0-android/lp/86.stamp'.
2583+
[Test]
2584+
public void ClassLibraryAarDependencies ()
2585+
{
2586+
var path = Path.Combine ("temp", TestName);
2587+
var material = new Package { Id = "Xamarin.Google.Android.Material", Version = "1.9.0.1" };
2588+
var libraryA = new XamarinAndroidLibraryProject {
2589+
ProjectName = "LibraryA",
2590+
Sources = {
2591+
new BuildItem.Source ("Bar.cs") {
2592+
TextContent = () => "public class Bar { }",
2593+
},
2594+
},
2595+
PackageReferences = { material },
2596+
};
2597+
using var builderA = CreateDllBuilder (Path.Combine (path, libraryA.ProjectName));
2598+
Assert.IsTrue (builderA.Build (libraryA), "Build should have succeeded.");
2599+
2600+
var libraryB = new XamarinAndroidLibraryProject {
2601+
ProjectName = "LibraryB",
2602+
Sources = {
2603+
new BuildItem.Source ("Foo.cs") {
2604+
TextContent = () => "public class Foo : Bar { }",
2605+
}
2606+
},
2607+
PackageReferences = { material },
2608+
};
2609+
libraryB.AddReference (libraryA);
2610+
using var builderB = CreateDllBuilder (Path.Combine (path, libraryB.ProjectName));
2611+
Assert.IsTrue (builderB.Build (libraryB), "Build should have succeeded.");
2612+
}
25802613
}
25812614
}

0 commit comments

Comments
 (0)