Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] fix .aar files flowing from project r…
Browse files Browse the repository at this point in the history
…eferences (dotnet#8193)

Fixes: dotnet#8190

In a customer sample, they have an `.aar` file they need the Java code
from, but do not want a C# binding for it:

    <AndroidLibrary Update="FooNonBinding-release.aar" Bind="false" />

`Bind="false"` looks to have the side effect where:

1. It does not get copied to the output directory.

2. The Java types don't make it to the final app.

3. Crash at runtime:

    java.lang.ClassNotFoundException: Didn't find class "com.example.foononbinding.FooSample" on path

A workaround is to add a line such as:

    <None Include="FooNonBinding-release.aar" CopyToOutputDirectory="PreserveNewest" />

I could reproduce this issue by updating our existing
`DotNetBuildLibrary` test. I could assert the file exists in the output
directory, as well as actually using `dexdump` to verify Java classes
make it to the app. They did not!

The solution here being that we are missing a line such as:

    <!-- .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)" />

Now the `DotNetBuildLibrary` test passes.
  • Loading branch information
jonathanpeppers committed Jul 18, 2023
1 parent 58d1306 commit 26ffd5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ 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)" />
</ItemGroup>
<!-- Legacy binding projects -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public Foo ()
new AndroidItem.AndroidLibrary ("sub\\directory\\bar.aar") {
WebContent = "https://repo1.maven.org/maven2/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar",
},
new AndroidItem.AndroidLibrary ("sub\\directory\\baz.aar") {
WebContent = "https://repo1.maven.org/maven2/com/soundcloud/android/android-crop/1.0.1/android-crop-1.0.1.aar",
MetadataValues = "Bind=false",
},
new AndroidItem.AndroidJavaSource ("JavaSourceTestExtension.java") {
Encoding = Encoding.ASCII,
TextContent = () => ResourceData.JavaSourceTestExtension,
Expand All @@ -150,6 +154,10 @@ public Foo ()
libB.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("sub\\directory\\arm64-v8a\\libfoo.so") {
BinaryContent = () => Array.Empty<byte> (),
});
libB.OtherBuildItems.Add (new AndroidItem.AndroidLibrary (default (Func<string>)) {
Update = () => "sub\\directory\\baz.aar",
MetadataValues = "Bind=false",
});
libB.OtherBuildItems.Add (new AndroidItem.AndroidNativeLibrary (default (Func<string>)) {
Update = () => "libfoo.so",
MetadataValues = "Link=x86\\libfoo.so",
Expand All @@ -172,6 +180,7 @@ public Foo ()
aarPath = Path.Combine (libBOutputPath, $"{libB.ProjectName}.aar");
FileAssert.Exists (aarPath);
FileAssert.Exists (Path.Combine (libBOutputPath, "bar.aar"));
FileAssert.Exists (Path.Combine (libBOutputPath, "baz.aar"));
using (var aar = ZipHelper.OpenZip (aarPath)) {
aar.AssertContainsEntry (aarPath, "assets/foo/foo.txt");
aar.AssertContainsEntry (aarPath, "res/layout/mylayout.xml");
Expand Down Expand Up @@ -235,6 +244,10 @@ public Foo ()
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
className = "Lcom/xamarin/android/test/msbuildtest/JavaSourceTestExtension;";
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
className = "Lcom/balysv/material/drawable/menu/MaterialMenu;"; // from material-menu-1.1.0.aar
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
className = "Lcom/soundcloud/android/crop/Crop;"; // from android-crop-1.0.1.aar
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");

// Check environment variable
var environmentFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediate, "x86", required: true);
Expand Down

0 comments on commit 26ffd5d

Please sign in to comment.