xamarin / AndroidX

AndroidX bindings for .NET for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`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:

<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

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