From 26ffd5d763b36dde528bc324967f4925e9101733 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Tue, 18 Jul 2023 04:19:56 -0500 Subject: [PATCH] [Xamarin.Android.Build.Tasks] fix `.aar` files flowing from project references (#8193) Fixes: https://github.com/xamarin/xamarin-android/issues/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: `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: 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: ++ Now the `DotNetBuildLibrary` test passes. --- .../Android/Xamarin.Android.AvailableItems.targets | 1 + .../Tests/Xamarin.Android.Build.Tests/XASdkTests.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets index f5dac4d577b..019363103b2 100644 --- a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets +++ b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets @@ -103,6 +103,7 @@ This item group populates the Build Action drop-down in IDEs. + diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs index 50cb39b5694..e3f46c3a741 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs @@ -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, @@ -150,6 +154,10 @@ public Foo () libB.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("sub\\directory\\arm64-v8a\\libfoo.so") { BinaryContent = () => Array.Empty (), }); + libB.OtherBuildItems.Add (new AndroidItem.AndroidLibrary (default (Func)) { + Update = () => "sub\\directory\\baz.aar", + MetadataValues = "Bind=false", + }); libB.OtherBuildItems.Add (new AndroidItem.AndroidNativeLibrary (default (Func)) { Update = () => "libfoo.so", MetadataValues = "Link=x86\\libfoo.so", @@ -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"); @@ -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);