Skip to content

Commit 001675f

Browse files
[One .NET] ignore @(Content) from @(ProjectReference) (#5308)
Fixes: #5299 Context: dotnet/sdk#14619 When setting up `Mono.Android.NET-Tests`, we had to work around an error with duplicate `@(Content)` files: error NETSDK1148: Found multiple publish output files with the same relative path: external\Java.Interop\bin\Release\java-interop.jar, \external\Java.Interop\bin\Release-netcoreapp3.1\java-interop.jar. Android doesn't support the `@(Content)` build action *at all*, and we emit a `XA0101` warning if it is used. We even have a test for this. However, it appears that any `@(Content)` from `@(ProjectReference)` items are currently brought over in .NET 6, as they get added to the `@(ResolvedFileToPublish)` item group by the dotnet/sdk. Luckily there appears to be a [`$(_GetChildProjectCopyToPublishDirectoryItems)`][0] MSBuild property we can set to skip this behavior. We can set this by default, as we don't need to compute `@(Content)` items in referenced projects. The library project will emit `XA0101` and that is enough. I also updated a test that would have triggered `NETSDK1148` which now works. I could also see a perf improvement in the `Mono.Android.NET-Tests` project on Windows: * Before: GetCopyToPublishDirectoryItems 21ms * After: GetCopyToPublishDirectoryItems 0ms [0]: https://github.com/dotnet/sdk/blob/955c0fc7b06e2fa34bacd076ed39f61e4fb61716/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets#L783-L794
1 parent 55faee0 commit 001675f

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
<!-- mono-symbolicate is not supported -->
2323
<MonoSymbolArchive>false</MonoSymbolArchive>
2424
<AndroidCodegenTarget Condition=" '$(AndroidCodegenTarget)' == '' ">XAJavaInterop1</AndroidCodegenTarget>
25+
<!--
26+
Disable @(Content) from referenced projects
27+
See: https://github.com/dotnet/sdk/blob/955c0fc7b06e2fa34bacd076ed39f61e4fb61716/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets#L16
28+
-->
29+
<_GetChildProjectCopyToPublishDirectoryItems>false</_GetChildProjectCopyToPublishDirectoryItems>
2530
</PropertyGroup>
2631

2732
<!-- User-facing configuration-specific defaults -->

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,21 +1484,55 @@ public void BuildBasicApplicationCheckConfigFiles ()
14841484
}
14851485

14861486
[Test]
1487-
public void BuildApplicationCheckItEmitsAWarningWithContentItems ()
1487+
public void CheckContentBuildAction ()
14881488
{
1489-
var proj = new XamarinAndroidApplicationProject ();
1490-
using (var b = CreateApkBuilder ("temp/BuildApplicationCheckItEmitsAWarningWithContentItems")) {
1491-
b.ThrowOnBuildFailure = false;
1489+
var metadata = "CopyToOutputDirectory=PreserveNewest";
1490+
var path = Path.Combine ("temp", TestName);
1491+
1492+
var lib = new XamarinAndroidLibraryProject {
1493+
ProjectName = "Library1",
1494+
Sources = {
1495+
new BuildItem.Source ("Bar.cs") {
1496+
TextContent = () => "public class Bar { }"
1497+
},
1498+
},
1499+
OtherBuildItems = {
1500+
new BuildItem.Content ("TestContent.txt") {
1501+
TextContent = () => "Test Content from Library",
1502+
MetadataValues = metadata,
1503+
}
1504+
}
1505+
};
1506+
1507+
var proj = new XamarinAndroidApplicationProject {
1508+
ProjectName = "App",
1509+
Sources = {
1510+
new BuildItem.Source ("Foo.cs") {
1511+
TextContent = () => "public class Foo : Bar { }"
1512+
},
1513+
},
1514+
References = {
1515+
new BuildItem ("ProjectReference", "..\\Library1\\Library1.csproj"),
1516+
}
1517+
};
1518+
using (var libBuilder = CreateDllBuilder (Path.Combine (path, lib.ProjectName)))
1519+
using (var appBuilder = CreateApkBuilder (Path.Combine (path, proj.ProjectName))) {
1520+
Assert.IsTrue (libBuilder.Build (lib), "library should have built successfully");
1521+
StringAssertEx.Contains ("TestContent.txt : warning XA0101: @(Content) build action is not supported", libBuilder.LastBuildOutput,
1522+
"Build Output did not contain 'TestContent.txt : warning XA0101'.");
1523+
14921524
proj.AndroidResources.Add (new BuildItem.Content ("TestContent.txt") {
1493-
TextContent = () => "Test Content"
1525+
TextContent = () => "Test Content",
1526+
MetadataValues = metadata,
14941527
});
14951528
proj.AndroidResources.Add (new BuildItem.Content ("TestContent1.txt") {
1496-
TextContent = () => "Test Content 1"
1529+
TextContent = () => "Test Content 1",
1530+
MetadataValues = metadata,
14971531
});
1498-
Assert.IsTrue (b.Build (proj), "Build should have built successfully");
1499-
StringAssertEx.Contains ("TestContent.txt : warning XA0101: @(Content) build action is not supported", b.LastBuildOutput,
1532+
Assert.IsTrue (appBuilder.Build (proj), "app should have built successfully");
1533+
StringAssertEx.Contains ("TestContent.txt : warning XA0101: @(Content) build action is not supported", appBuilder.LastBuildOutput,
15001534
"Build Output did not contain 'TestContent.txt : warning XA0101'.");
1501-
StringAssertEx.Contains ("TestContent1.txt : warning XA0101: @(Content) build action is not supported", b.LastBuildOutput,
1535+
StringAssertEx.Contains ("TestContent1.txt : warning XA0101: @(Content) build action is not supported", appBuilder.LastBuildOutput,
15021536
"Build Output did not contain 'TestContent1.txt : warning XA0101'.");
15031537
}
15041538
}

tests/Mono.Android-Tests/Directory.Build.targets

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@
3333
DependsOnTargets="_CreateJavaInteropDllConfigs">
3434
</Target>
3535

36-
<!-- When we build the multitargeted Java.Interop project as a dependency of this test project we hit the following error:
37-
error NETSDK1148: Found multiple publish output files with the same relative path: external\Java.Interop\bin\Release\java-interop.jar, \external\Java.Interop\bin\Release-netcoreapp3.1\java-interop.jar.
38-
We do not need to include any of this content in the output directory, so remove it here:
39-
-->
40-
<Target Name="RemoveJavaInteropJarContent"
41-
Condition=" '$(TargetFramework)' == 'net6.0-android' "
42-
BeforeTargets="ComputeResolvedFilesToPublishList" >
43-
<ItemGroup>
44-
<ResolvedFileToPublish Remove="$(JavaInteropSourceDirectory)\bin\**\java-interop.jar" />
45-
</ItemGroup>
46-
</Target>
47-
4836
<Target Name="GenerateNuGetConfig" >
4937
<PropertyGroup>
5038
<LocalNupkgDirectory Condition=" '$(LocalNupkgDirectory)' == '' ">$(XamarinAndroidSourcePath)bin\Build$(Configuration)\nupkgs\</LocalNupkgDirectory>

0 commit comments

Comments
 (0)