Skip to content

Commit 26ffd5d

Browse files
[Xamarin.Android.Build.Tasks] fix .aar files flowing from project references (#8193)
Fixes: #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.
1 parent 58d1306 commit 26ffd5d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ 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)" />
106107
<None Include="@(LibraryProjectZip)" TfmSpecificPackageFile="%(LibraryProjectZip.Pack)" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
107108
</ItemGroup>
108109
<!-- Legacy binding projects -->

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public Foo ()
132132
new AndroidItem.AndroidLibrary ("sub\\directory\\bar.aar") {
133133
WebContent = "https://repo1.maven.org/maven2/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar",
134134
},
135+
new AndroidItem.AndroidLibrary ("sub\\directory\\baz.aar") {
136+
WebContent = "https://repo1.maven.org/maven2/com/soundcloud/android/android-crop/1.0.1/android-crop-1.0.1.aar",
137+
MetadataValues = "Bind=false",
138+
},
135139
new AndroidItem.AndroidJavaSource ("JavaSourceTestExtension.java") {
136140
Encoding = Encoding.ASCII,
137141
TextContent = () => ResourceData.JavaSourceTestExtension,
@@ -150,6 +154,10 @@ public Foo ()
150154
libB.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("sub\\directory\\arm64-v8a\\libfoo.so") {
151155
BinaryContent = () => Array.Empty<byte> (),
152156
});
157+
libB.OtherBuildItems.Add (new AndroidItem.AndroidLibrary (default (Func<string>)) {
158+
Update = () => "sub\\directory\\baz.aar",
159+
MetadataValues = "Bind=false",
160+
});
153161
libB.OtherBuildItems.Add (new AndroidItem.AndroidNativeLibrary (default (Func<string>)) {
154162
Update = () => "libfoo.so",
155163
MetadataValues = "Link=x86\\libfoo.so",
@@ -172,6 +180,7 @@ public Foo ()
172180
aarPath = Path.Combine (libBOutputPath, $"{libB.ProjectName}.aar");
173181
FileAssert.Exists (aarPath);
174182
FileAssert.Exists (Path.Combine (libBOutputPath, "bar.aar"));
183+
FileAssert.Exists (Path.Combine (libBOutputPath, "baz.aar"));
175184
using (var aar = ZipHelper.OpenZip (aarPath)) {
176185
aar.AssertContainsEntry (aarPath, "assets/foo/foo.txt");
177186
aar.AssertContainsEntry (aarPath, "res/layout/mylayout.xml");
@@ -235,6 +244,10 @@ public Foo ()
235244
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
236245
className = "Lcom/xamarin/android/test/msbuildtest/JavaSourceTestExtension;";
237246
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
247+
className = "Lcom/balysv/material/drawable/menu/MaterialMenu;"; // from material-menu-1.1.0.aar
248+
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
249+
className = "Lcom/soundcloud/android/crop/Crop;"; // from android-crop-1.0.1.aar
250+
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
238251

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

0 commit comments

Comments
 (0)