Skip to content

Commit 8cc3e4f

Browse files
[One .NET] improve default MSBuild item groups (#5349)
Context: #5348 While implementing PR #5348, I noticed that we could add a wildcard to make Java bindings simpler: <TransformFile Include="Transforms\**\*.xml" /> This way you can just put `.xml` files into the `Transforms` folder so that they will be picked up automatically. We also need to include some additional wildcards: <AndroidLibrary Include="**\*.jar" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" /> <AndroidLibrary Include="**\*.aar" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" /> <AndroidNativeLibrary Include="**\*.so" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" /> To make sure we exclude folders like `bin` and `obj`, we can rely on `$(DefaultItemExcludes)` and `$(DefaultExcludesInProjectFolder)` [as the dotnet/sdk does][0]. I updated the `XASdkTests.DotNetBuildBinding()` test to use `Metadata.xml`, and it now relies on the new defaults. [0]: https://github.com/dotnet/sdk/blob/3e9e1d1e3082cab14593b54562c956b155881658/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.props#L37
1 parent 101c58f commit 8cc3e4f

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

Documentation/guides/OneDotNet.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,28 @@ project][binding] as a separate project type. Any of the MSBuild item
2929
groups or build actions that currently work in binding projects will
3030
be supported through a .NET 6 Android application or library.
3131

32-
For example, a binding library could look like:
32+
For example, a binding library would be identical to a class library:
3333

3434
```xml
3535
<Project Sdk="Microsoft.NET.Sdk">
3636
<PropertyGroup>
3737
<TargetFramework>net6.0-android</TargetFramework>
3838
</PropertyGroup>
39-
<ItemGroup>
40-
<TransformFile Include="Transforms\Metadata.xml" />
41-
<EmbeddedJar Include="Jars\foo.jar" />
42-
</ItemGroup>
4339
</Project>
4440
```
4541

42+
Along with the file structure:
43+
44+
Transforms/
45+
Metadata.xml
46+
foo.jar
47+
48+
`Transforms\*.xml` files are automatically included as a
49+
`@(TransformFile)` item, and `.jar` files are automatically included
50+
as a `@(AndroidLibrary)` item.
51+
4652
This will bind C# types for the Java types found in `foo.jar` using
47-
the metadata fixups from `Metadata.xml`.
53+
the metadata fixups from `Transforms\Metadata.xml`.
4854

4955
[binding]: https://docs.microsoft.com/xamarin/android/platform/binding-java-library/
5056

Documentation/guides/OneDotNetEmbeddedResources.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,25 +179,25 @@ Let's simplify this, we could support all of the above with a new
179179

180180
```xml
181181
<!-- Include and bind -->
182-
<AndroidLibrary Include="foo.aar" Bind="true" />
182+
<AndroidLibrary Include="foo.aar" />
183183
<!-- Just include, do not bind -->
184-
<AndroidLibrary Include="bar.aar" />
185-
<AndroidLibrary Include="baz.jar" />
184+
<AndroidLibrary Include="bar.aar" Bind="false" />
185+
<AndroidLibrary Include="baz.jar" Bind="false" />
186186
<!--
187187
Bind but do not include in NuGet package.
188188
%(Pack) is built into NuGet MSBuild targets.
189189
-->
190-
<AndroidLibrary Include="bar.aar" Bind="true" Pack="false" />
190+
<AndroidLibrary Include="bar.aar" Pack="false" />
191191
<!-- Native libraries need ABI directory -->
192192
<AndroidLibrary Include="armeabi-v7a\libfoo.so" />
193193
<AndroidLibrary Include="x86\libfoo.so" />
194194
```
195195

196196
The new `@(AndroidLibrary)` item group will simply translate to the
197197
old ones for backwards compatibility. The extension of the file can be
198-
used to determine what kind of library each item is. `%(Bind)` will be
199-
`false` by default, and `%(Pack)` will be `true` by default. `%(Pack)`
200-
will not do anything in application projects.
198+
used to determine what kind of library each item is. `%(Bind)` and
199+
`%(Pack)` will both be `true` by default. `%(Pack)` will not do
200+
anything in application projects.
201201

202202
The deprecated item groups will no longer embed, but pack into
203203
`.nupkg` files instead:

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ https://github.com/dotnet/designs/blob/4703666296f5e59964961464c25807c727282cae/
1616
-->
1717
<Project>
1818

19-
<!-- Default Resource file inclusion -->
20-
<!-- https://developer.android.com/guide/topics/resources/providing-resources -->
2119
<ItemGroup Condition=" '$(EnableDefaultAndroidItems)' == 'true' ">
20+
<!-- Default Resource file inclusion -->
21+
<!-- https://developer.android.com/guide/topics/resources/providing-resources -->
2222
<AndroidResource Include="$(MonoAndroidResourcePrefix)\*\*.xml" />
2323
<AndroidResource Include="$(MonoAndroidResourcePrefix)\*\*.axml" />
2424
<AndroidResource Include="$(MonoAndroidResourcePrefix)\*\*.png" />
@@ -28,11 +28,14 @@ https://github.com/dotnet/designs/blob/4703666296f5e59964961464c25807c727282cae/
2828
<AndroidResource Include="$(MonoAndroidResourcePrefix)\font\*.otf" />
2929
<AndroidResource Include="$(MonoAndroidResourcePrefix)\font\*.ttc" />
3030
<AndroidResource Include="$(MonoAndroidResourcePrefix)\raw\*" Exclude="$(MonoAndroidResourcePrefix)\raw\.*" />
31-
</ItemGroup>
32-
33-
<!-- Default Asset file inclusion -->
34-
<ItemGroup Condition=" '$(EnableDefaultAndroidItems)' == 'true' ">
31+
<!-- Default Asset file inclusion -->
3532
<AndroidAsset Include="$(MonoAndroidAssetsPrefix)\**\*" Exclude="$(MonoAndroidAssetsPrefix)\**\.*\**" />
33+
<!-- Default XPath transforms for bindings -->
34+
<TransformFile Include="Transforms\**\*.xml" />
35+
<!-- Default Java or native libraries -->
36+
<AndroidLibrary Include="**\*.jar" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
37+
<AndroidLibrary Include="**\*.aar" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
38+
<AndroidNativeLibrary Include="**\*.so" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
3639
</ItemGroup>
3740

3841
</Project>

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AndroidLibraries.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ projects.
1818
</ItemGroup>
1919
<ItemDefinitionGroup>
2020
<AndroidLibrary>
21-
<Bind>false</Bind>
21+
<Bind>true</Bind>
2222
<Pack>true</Pack>
2323
</AndroidLibrary>
2424
</ItemDefinitionGroup>

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,13 @@ public void DotNetLibraryAarChanges ()
259259
public void DotNetBuildBinding ()
260260
{
261261
var proj = new XASdkProject (outputType: "Library");
262-
proj.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("javaclasses.jar") {
263-
MetadataValues = "Bind=true",
262+
proj.Sources.Add (new AndroidItem.TransformFile ("Transforms\\Metadata.xml") {
263+
TextContent = () =>
264+
@"<metadata>
265+
<attr path=""/api/package[@name='com.xamarin.android.test.msbuildtest']"" name=""managedName"">MSBuildTest</attr>
266+
</metadata>",
267+
});
268+
proj.Sources.Add (new AndroidItem.AndroidLibrary ("javaclasses.jar") {
264269
BinaryContent = () => Convert.FromBase64String (InlineData.JavaClassesJarBase64)
265270
});
266271
// TODO: bring back when Xamarin.Android.Bindings.Documentation.targets is working
@@ -273,7 +278,7 @@ public void DotNetBuildBinding ()
273278
var assemblyPath = Path.Combine (FullProjectDirectory, proj.OutputPath, "UnnamedProject.dll");
274279
FileAssert.Exists (assemblyPath);
275280
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
276-
var typeName = "Com.Xamarin.Android.Test.Msbuildtest.JavaSourceJarTest";
281+
var typeName = "MSBuildTest.JavaSourceJarTest";
277282
var type = assembly.MainModule.GetType (typeName);
278283
Assert.IsNotNull (type, $"{assemblyPath} should contain {typeName}");
279284
}

0 commit comments

Comments
 (0)