• 2 min read

Don't Put Android in Your Namespace in Xamarin Projects

One of the most common mistakes when creating a new project for Android is to put "Android" in the name. You want to because your app is going to run on Android, but it causes a series of pain immediately with namespace conflicts. When you create a new Xamarin.Forms project you may have noticed that the Assembly Name and Default Namespaces are different. If you name your Xamarin.Forms project "App37" you will see the following:

Android project Application settings with Assembly Name App37.Android but Default Namespace App37.Droid

This happens automatically because you are creating a full solution full of multiple projects. The Xamarin team was clever and in the template ensured that these were setup correct.

However, if you create a non-Xamarin.Forms project and are just creating a Android (non-Forms) app and put Android in the name (anywhere, not just at the end) it will cause some issues.

Configure your new project for an Android App (Xamarin) named MyApp.Android, with NO!!! scrawled over it in red

When you open up the project settings you will see both the settings are the same:

Project settings where Assembly Name and Default Namespace are both MyApp.Android, marked AHHH!

Now you go to compile and you have TONS of errors that different namespaces don't exist or have conflicts:

Error List with CS0234 and CS1503 errors saying types like Content, Support, and Views do not exist in MyApp.Android

The issue here is that your namespace now has a conflict with every namespace from the core Android libraries!

Activity code whose namespace MyApp.Android, circled in red, collides with the using Android statements above it

Now, if you are in this space you can solve it! Simply change your default namespace to "MyApp.Droid" in the project settings and also change your namespace in any files in your project to "MyApp.Droid" as well.

After that do a full clean and rebuild to ensure that the  Resources -> Resources.designer.cs file has been modified and updated with the new namespace.

James Montemagno
Author

James Montemagno

James Montemagno is a Principal Lead Program Manager for Developer Community at Microsoft. He has been a .NET developer since 2005, working in a wide range of industries including game development, printer software, and web services. Live, Love, Bike, and Code.

Milli the dog peeking up from the bottom of the page