Skip to content

Commit 12b28f2

Browse files
jonathanpeppersjonpryor
authored andcommitted
[One .NET] enable C# bindings for @(AndroidLibrary) (#6056)
Fixes: dotnet/maui#1475 A .NET 6 application project was failing to include C# bindings for `.aar` files, but `.jar` files were working. The workaround was to include: <LibraryProjectZip Include="myjavalibrary.aar" /> We had a test that checked this behavior for `.jar` files, but not `.aar` files. After reproducing the behavior in a test, I found the `_CategorizeAndroidLibraries` MSBuild target did not automatically setup the `@(LibraryProjectZip)` item group in .NET 6 application projects. `@(AndroidAarLibrary)` *was* being setup, which explained why the Java code made it to `classes.dex`, but C# bindings were missing. Updated the `_CategorizeAndroidLibraries` MSBuild target for this case.
1 parent 855e0ba commit 12b28f2

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ This item group populates the Build Action drop-down in IDEs.
8383
<ItemGroup Condition=" '$(AndroidApplication)' != 'true' ">
8484
<EmbeddedNativeLibrary Include="@(AndroidNativeLibrary)" />
8585
</ItemGroup>
86+
<!-- Any .NET 6 project -->
87+
<ItemGroup Condition=" '$(UsingAndroidNETSdk)' == 'true' ">
88+
<LibraryProjectZip Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' and '%(AndroidLibrary.Bind)' == 'true' " />
89+
</ItemGroup>
8690
<!-- .NET 6 class libraries-->
8791
<ItemGroup Condition=" '$(AndroidApplication)' != 'true' and '$(UsingAndroidNETSdk)' == 'true' ">
8892
<AndroidAarLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' and '%(AndroidLibrary.Bind)' != 'true' " />
89-
<LibraryProjectZip Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' and '%(AndroidLibrary.Bind)' == 'true' " />
9093
<AndroidJavaLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' and '%(AndroidLibrary.Bind)' != 'true' " />
9194
<EmbeddedJar Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' and '%(AndroidLibrary.Bind)' == 'true' " />
9295
<!-- .aar files should be copied to $(OutputPath) in .NET 6-->

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease)
376376
new BuildItem ("EmbeddedResource", "Foo.es.resx") {
377377
TextContent = () => InlineData.ResxWithContents ("<data name=\"CancelButton\"><value>Cancelar</value></data>")
378378
},
379+
new AndroidItem.TransformFile ("Transforms.xml") {
380+
// Remove two methods that introduced warnings:
381+
// Com.Balysv.Material.Drawable.Menu.MaterialMenuView.cs(214,30): warning CS0114: 'MaterialMenuView.OnRestoreInstanceState(IParcelable)' hides inherited member 'View.OnRestoreInstanceState(IParcelable?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
382+
// Com.Balysv.Material.Drawable.Menu.MaterialMenuView.cs(244,56): warning CS0114: 'MaterialMenuView.OnSaveInstanceState()' hides inherited member 'View.OnSaveInstanceState()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
383+
TextContent = () => "<metadata><remove-node path=\"/api/package[@name='com.balysv.material.drawable.menu']/class[@name='MaterialMenuView']/method[@name='onRestoreInstanceState']\" /><remove-node path=\"/api/package[@name='com.balysv.material.drawable.menu']/class[@name='MaterialMenuView']/method[@name='onSaveInstanceState']\" /></metadata>",
384+
},
385+
new AndroidItem.AndroidLibrary ("material-menu-1.1.0.aar") {
386+
WebContent = "https://repo.jfrog.org/artifactory/libs-release-bintray/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar"
387+
},
379388
}
380389
};
381390
proj.MainActivity = proj.DefaultMainActivity.Replace (": Activity", ": AndroidX.AppCompat.App.AppCompatActivity");
@@ -420,8 +429,9 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease)
420429
FileAssert.Exists (assemblyPath);
421430
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
422431
var typeName = "Com.Xamarin.Android.Test.Msbuildtest.JavaSourceJarTest";
423-
var type = assembly.MainModule.GetType (typeName);
424-
Assert.IsNotNull (type, $"{assemblyPath} should contain {typeName}");
432+
Assert.IsNotNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should contain {typeName}");
433+
typeName = "Com.Balysv.Material.Drawable.Menu.MaterialMenuView";
434+
Assert.IsNotNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should contain {typeName}");
425435
}
426436

427437
var rids = runtimeIdentifiers.Split (';');

0 commit comments

Comments
 (0)