Skip to content

Warning BG8403 Type Name Matches Namespace Name

Jonathan Pobst edited this page Jul 12, 2022 · 1 revision

This warning was added in .NET 7 / Xamarin.Android 13.1 (Visual Studio 2022 17.4).

In Java, it is often common for a package to contain a type whose name matches the package name:

package android.graphics.drawable;

public class Drawable { ... }

This is rarely an issue in Java because package and type names differ in case, and Java is a case-sensitive language.

However, part of the Android Binding process makes the generated C# code look more like a standard C# API. This includes converting Java package names to C# Pascal cased namespace names. Thus, this code becomes:

namespace Android.Graphics.Drawable;

public class Drawable { ... }

Although this is legal C#, it can lead to unexpected errors for a user if their code is not explicit enough about whether they are referencing the namespace or the type.

This is explained in the Framework Design Guidelines:

❌ DO NOT use the same name for a namespace and a type in that namespace.

For example, do not use Debug as a namespace name and then also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.

The BG8403 warning alerts Java bindings creators to this scenario, in case they wish to prevent this issue.

warning BG8403: Type `Android.Graphics.Drawable.Drawable` has a type name which matches the enclosing namespace name.

There are two ways to fix this:

  • Change the namespace name
  • Change the type name

Change Namespace Name

The Framework Design Guidelines suggest making namespace names plural when appropriate, such as System.Collections instead of System.Collection.

A namespace replacement can be used to rename the entire namespace:

<ItemGroup>
  <AndroidNamespaceReplacement Include='Android.Graphics.Drawable' Replacement='Android.Graphics.Drawables' />
</ItemGroup>

Change Type Name

Sometimes it is not desirable to rename the namespace, such as when it would break compatibility with an already published API. Another option is to use metadata to rename the type itself:

<attr path="/api/package[@name='android.graphics.drawable']/class[@name='drawable']" name="managedName">DrawableType</attr>

Suppression

This warning can be suppressed if it is not relevant or desirable for the project.

<PropertyGroup>
  <NoWarn>$(NoWarn);BG8403</NoWarn>
</PropertyGroup>