Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] fix duplicate .aar files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonathanpeppers committed Jul 18, 2023
1 parent 26ffd5d commit 359f66d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ This item group populates the Build Action drop-down in IDEs.
<AndroidJavaLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' and '%(AndroidLibrary.Bind)' != 'true' " />
<EmbeddedJar Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' and '%(AndroidLibrary.Bind)' == 'true' " />
<!-- .aar files should be copied to $(OutputPath) in .NET 6-->
<None Include="@(AndroidAarLibrary)" TfmSpecificPackageFile="%(AndroidAarLibrary.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
<None Include="@(LibraryProjectZip)" TfmSpecificPackageFile="%(LibraryProjectZip.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
<None Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' " TfmSpecificPackageFile="%(AndroidLibrary.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
<!-- @(LibraryProjectZip) items that are not in @(AndroidLibrary) -->
<None Include="@(LibraryProjectZip)" Exclude="@(AndroidLibrary)" TfmSpecificPackageFile="%(LibraryProjectZip.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
</ItemGroup>
<!-- Legacy binding projects -->
<ItemGroup Condition=" '$(_AndroidIsBindingProject)' == 'true' and '$(UsingAndroidNETSdk)' != 'true' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2577,5 +2577,38 @@ public void SimilarAndroidXAssemblyNames ([Values(true, false)] bool publishTrim
using var builder = CreateApkBuilder ();
Assert.IsTrue (builder.Build (proj), "Build should have succeeded.");
}

// Combination of class libraries that triggered the problem:
// error APT2144: invalid file path 'obj/Release/net8.0-android/lp/86.stamp'.
[Test]
public void ClassLibraryAarDependencies ()
{
var path = Path.Combine ("temp", TestName);
var material = new Package { Id = "Xamarin.Google.Android.Material", Version = "1.9.0.1" };
var libraryA = new XamarinAndroidLibraryProject {
ProjectName = "LibraryA",
Sources = {
new BuildItem.Source ("Bar.cs") {
TextContent = () => "public class Bar { }",
},
},
PackageReferences = { material },
};
using var builderA = CreateDllBuilder (Path.Combine (path, libraryA.ProjectName));
Assert.IsTrue (builderA.Build (libraryA), "Build should have succeeded.");

var libraryB = new XamarinAndroidLibraryProject {
ProjectName = "LibraryB",
Sources = {
new BuildItem.Source ("Foo.cs") {
TextContent = () => "public class Foo : Bar { }",
}
},
PackageReferences = { material },
};
libraryB.AddReference (libraryA);
using var builderB = CreateDllBuilder (Path.Combine (path, libraryB.ProjectName));
Assert.IsTrue (builderB.Build (libraryB), "Build should have succeeded.");
}
}
}

0 comments on commit 359f66d

Please sign in to comment.