From 23fee30e48c6dd2fdfbb92af274213574f2ca75f Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Tue, 1 Dec 2020 14:25:37 -0600 Subject: [PATCH] [One .NET] improve default MSBuild item groups When implementing #5348, I noticed that we could add a wildcard to make Java bindings simpler: This way you can just put `.xml` files in the `Transforms` folder so they will be picked up automatically. We also need to include some additional wildcards: To make sure we exclude folders like `bin` and `obj`, we can rely on `$(DefaultItemExcludes)` and `$(DefaultExcludesInProjectFolder)` as the dotnet/sdk does: https://github.com/dotnet/sdk/blob/3e9e1d1e3082cab14593b54562c956b155881658/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.props#L37 I updated a test that uses a `Metadata.xml`, and it now relies on the new defaults. --- Documentation/guides/OneDotNet.md | 18 ++++++++++++------ .../guides/OneDotNetEmbeddedResources.md | 14 +++++++------- .../Microsoft.Android.Sdk/Sdk/AutoImport.props | 15 +++++++++------ ...rosoft.Android.Sdk.AndroidLibraries.targets | 2 +- .../Xamarin.Android.Build.Tests/XASdkTests.cs | 11 ++++++++--- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/Documentation/guides/OneDotNet.md b/Documentation/guides/OneDotNet.md index d9e7e8b6c8e..27e1d728500 100644 --- a/Documentation/guides/OneDotNet.md +++ b/Documentation/guides/OneDotNet.md @@ -29,22 +29,28 @@ project][binding] as a separate project type. Any of the MSBuild item groups or build actions that currently work in binding projects will be supported through a .NET 6 Android application or library. -For example, a binding library could look like: +For example, a binding library would be identical to a class library: ```xml net6.0-android - - - - ``` +Along with the file structure: + + Transforms/ + Metadata.xml + foo.jar + +`Transforms\*.xml` files are automatically included as a +`@(TransformFile)` item, and `.jar` files are automatically included +as a `@(AndroidLibrary)` item. + This will bind C# types for the Java types found in `foo.jar` using -the metadata fixups from `Metadata.xml`. +the metadata fixups from `Transforms\Metadata.xml`. [binding]: https://docs.microsoft.com/xamarin/android/platform/binding-java-library/ diff --git a/Documentation/guides/OneDotNetEmbeddedResources.md b/Documentation/guides/OneDotNetEmbeddedResources.md index eaf94750f9a..8d9a2343863 100644 --- a/Documentation/guides/OneDotNetEmbeddedResources.md +++ b/Documentation/guides/OneDotNetEmbeddedResources.md @@ -179,15 +179,15 @@ Let's simplify this, we could support all of the above with a new ```xml - + - - + + - + @@ -195,9 +195,9 @@ Let's simplify this, we could support all of the above with a new The new `@(AndroidLibrary)` item group will simply translate to the old ones for backwards compatibility. The extension of the file can be -used to determine what kind of library each item is. `%(Bind)` will be -`false` by default, and `%(Pack)` will be `true` by default. `%(Pack)` -will not do anything in application projects. +used to determine what kind of library each item is. `%(Bind)` and +`%(Pack)` will both be `true` by default. `%(Pack)` will not do +anything in application projects. The deprecated item groups will no longer embed, but pack into `.nupkg` files instead: diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props index 42ccfa9744b..0a417097111 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props @@ -16,9 +16,9 @@ https://github.com/dotnet/designs/blob/4703666296f5e59964961464c25807c727282cae/ --> - - + + @@ -28,11 +28,14 @@ https://github.com/dotnet/designs/blob/4703666296f5e59964961464c25807c727282cae/ - - - - + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AndroidLibraries.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AndroidLibraries.targets index e8fdcd05fb7..7b72962351b 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AndroidLibraries.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AndroidLibraries.targets @@ -18,7 +18,7 @@ projects. - false + true true 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 bec46cd6cf1..dfbe56820a0 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 @@ -259,8 +259,13 @@ public void DotNetLibraryAarChanges () public void DotNetBuildBinding () { var proj = new XASdkProject (outputType: "Library"); - proj.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("javaclasses.jar") { - MetadataValues = "Bind=true", + proj.Sources.Add (new AndroidItem.TransformFile ("Transforms\\Metadata.xml") { + TextContent = () => +@" + MSBuildTest +", + }); + proj.Sources.Add (new AndroidItem.AndroidLibrary ("javaclasses.jar") { BinaryContent = () => Convert.FromBase64String (InlineData.JavaClassesJarBase64) }); // TODO: bring back when Xamarin.Android.Bindings.Documentation.targets is working @@ -273,7 +278,7 @@ public void DotNetBuildBinding () var assemblyPath = Path.Combine (FullProjectDirectory, proj.OutputPath, "UnnamedProject.dll"); FileAssert.Exists (assemblyPath); using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) { - var typeName = "Com.Xamarin.Android.Test.Msbuildtest.JavaSourceJarTest"; + var typeName = "MSBuildTest.JavaSourceJarTest"; var type = assembly.MainModule.GetType (typeName); Assert.IsNotNull (type, $"{assemblyPath} should contain {typeName}"); }