`Xamarin.Jetbrains.Annotations` namespace clash with `JetBrains.Annotations` causes CS0433 build failure
Susko3 opened this issue · comments
Android application type
Android for .NET (net6.0-android, etc.)
Affected platform version
MSBuild version 17.9.8+b34f75857 for .NET 8.0.204
Description
First noticed in ppy/osu@9c22fa3.
The java binding for org.jetbrains.annotations
(named Xamarin.Jetbrains.Annotations) uses the Jetbrains.Annotations
namespace. This directly clashes with the .NET JetBrains.Annotations which also uses the Jetbrains.Annotations
namespace.
This namespace clash is apparent when using a type that is defined in both the Java and in the .NET version of the library. For example, NotNullAttribute
.
Offending line that produces the clashing namespace:
AndroidX/Directory.Build.props
Line 74 in 3c8325e
As for fixing this issue, I suggest you change the namespace for the java binding. I don't think the types from the java library are meant to be used in application code, so the name of the namespace probably doesn't matter.
Steps to Reproduce
- Create a new android library or application project
- Add JetBrains.Annotations and Xamarin.Jetbrains.Annotations (or a package that depends on it, like Xamarin.AndroidX.Window) nuget package references
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/>
<PackageReference Include="Xamarin.Jetbrains.Annotations" Version="24.1.0.2"/>
</ItemGroup>
- Try to use
JetBrains.Annotations.NotNullAttribute
using JetBrains.Annotations;
namespace AndroidLib1;
public class Class1
{
public void Test([NotNull] object obj)
{
}
}
- Try to build the project, the build will fail with error
error CS0433: The type 'NotNullAttribute' exists in both 'JetBrains.Annotations, Version=4242.42.42.42, Culture=neutral, PublicKeyToken=1010a0d8d6380325'
and 'Xamarin.Jetbrains.Annotations, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
Did you find any workaround?
Add this to the project. Taken from https://stackoverflow.com/questions/9194495/type-exists-in-2-assemblies/65127159#65127159.
<Project Sdk="Microsoft.NET.Sdk">
<!-- Rest of project... -->
<Target Name="AddPackageAliases" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="%(Filename) == 'Xamarin.Jetbrains.Annotations'">
<Aliases>XamarinJetbrainsAnnotations</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
</Project>
Relevant log output
❯ dotnet build AndroidLib1
MSBuild version 17.9.8+b34f75857 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
S:\code\TestConsoleApp1\AndroidLib1\Class1.cs(7,23): error CS0433: The type 'NotNullAttribute' exists in both 'JetBrains.Annotations, Version=4242.42.42.42, Culture=neutral, PublicKeyToken=1010a0d8d6380325' and 'Xamarin.Jetbrai
ns.Annotations, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' [S:\code\TestConsoleApp1\AndroidLib1\AndroidLib1.csproj]
Build FAILED.
S:\code\TestConsoleApp1\AndroidLib1\Class1.cs(7,23): error CS0433: The type 'NotNullAttribute' exists in both 'JetBrains.Annotations, Version=4242.42.42.42, Culture=neutral, PublicKeyToken=1010a0d8d6380325' and 'Xamarin.Jetbrai
ns.Annotations, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' [S:\code\TestConsoleApp1\AndroidLib1\AndroidLib1.csproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:01.45
Unfortunately this is a historical mistake that would cause too much breakage if we fixed it now.
There is a lot of history/discussion/workarounds related to this issue here:
xamarin/XamarinComponents#1176