James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code

Share


Tags


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:

Application settings with different assembly and namespace names

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.

Creating a new project with Android in name

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

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

6 Errors on load for Android conflicts

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

Your namespace conflicts with Android

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.

Copyright © James Montemagno 2020 All rights reserved. Privacy Policy

View Comments