Skip to content

Commit 52d6880

Browse files
[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 d593b72 commit 52d6880

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
@@ -380,6 +380,15 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease)
380380
new BuildItem ("EmbeddedResource", "Foo.es.resx") {
381381
TextContent = () => InlineData.ResxWithContents ("<data name=\"CancelButton\"><value>Cancelar</value></data>")
382382
},
383+
new AndroidItem.TransformFile ("Transforms.xml") {
384+
// Remove two methods that introduced warnings:
385+
// 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.
386+
// 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.
387+
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>",
388+
},
389+
new AndroidItem.AndroidLibrary ("material-menu-1.1.0.aar") {
390+
WebContent = "https://repo.jfrog.org/artifactory/libs-release-bintray/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar"
391+
},
383392
}
384393
};
385394
proj.MainActivity = proj.DefaultMainActivity.Replace (": Activity", ": AndroidX.AppCompat.App.AppCompatActivity");
@@ -425,8 +434,9 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease)
425434
FileAssert.Exists (assemblyPath);
426435
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
427436
var typeName = "Com.Xamarin.Android.Test.Msbuildtest.JavaSourceJarTest";
428-
var type = assembly.MainModule.GetType (typeName);
429-
Assert.IsNotNull (type, $"{assemblyPath} should contain {typeName}");
437+
Assert.IsNotNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should contain {typeName}");
438+
typeName = "Com.Balysv.Material.Drawable.Menu.MaterialMenuView";
439+
Assert.IsNotNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should contain {typeName}");
430440
}
431441

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

0 commit comments

Comments
 (0)