Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xamarin.Jetbrains.Annotations namespace clash with JetBrains.Annotations causes CS0433 build failure #885

Open
Susko3 opened this issue May 6, 2024 · 1 comment

Comments

@Susko3
Copy link

Susko3 commented May 6, 2024

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:

<AndroidNamespaceReplacement Include='org.jetbrains' Replacement='JetBrains' />

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

  1. Create a new android library or application project
  2. 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>
  1. Try to use JetBrains.Annotations.NotNullAttribute
using JetBrains.Annotations;

namespace AndroidLib1;

public class Class1
{
    public void Test([NotNull] object obj)
    {
    }
}
  1. 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
@jpobst
Copy link
Contributor

jpobst commented May 6, 2024

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

Susko3 added a commit to Susko3/osu-framework that referenced this issue May 7, 2024
…ations`

This conflict was most noticeable when using `[NotNull]` in osu!
ppy/osu@9c22fa3

This doesn't fix the conflict in o!f.Android, but it does fix it for downstream projects.
If a fix is needed for o!f, PackageReference Aliases can be used.

More info: xamarin/AndroidX#885
Susko3 added a commit to Susko3/osu-framework that referenced this issue May 7, 2024
…ations`

This conflict was most noticeable when using `[NotNull]` in osu!
ppy/osu@9c22fa3

This doesn't fix the conflict in o!f.Android, but it does fix it for downstream projects.
If a fix is needed for o!f, PackageReference Aliases can be used.

More info: xamarin/AndroidX#885
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants