Converting Xamarin.Android template to .NET 6

A couple of months ago I made a post about how you could convert your Xamarin Native templates to .NET 6. Since everything was still very much up in the air, some of those bits have changed and the official templates for .NET 6 Android and iOS applications are now available in the latest version of Visual Studio 2022. The templates are called “Android Application” and “iOS Application”, respectively. See the screenshots below of the templates in the File -> New Project dialog.

The new Android template.
The new iOS template.

So if you have a native Xamarin.Android or Xamarin.iOS project that you want to upgrade, you could create a new .NET 6 project and copy your files over, or you could upgrade your existing project with the new format. There are multiple guides out there on how you would do this with .NET MAUI, but not much on how you would do it with native templates. I would suggest creating a new project and copying over the files, but if you want to upgrade your existing project, here are some tips on how to go about it.

This isn’t a complete guide on how you can upgrade. A lot of the upgrade steps from Xamarin.Forms to .NET MAUI are applicable here so I suggest you check out this GitHub page for extra info.

Upgrade csproj

If you use the new template to create a File -> New Android Application Project, the csproj file will look something like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-android</TargetFramework>
    <SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
    <OutputType>Exe</OutputType>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <ApplicationId>com.companyname.AndroidApp3</ApplicationId>
    <ApplicationVersion>1</ApplicationVersion>
    <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
  </PropertyGroup>
</Project>

Notice that the ImplicitUsings are now in place, so you don’t need any explicit references to things like AndroidX or the Essentials package.

If you copy and paste this content into your existing Xamarin.Android project and try to build it, you might get some error messages that look like this:

resource style/Theme.MaterialComponents.Light.DarkActionBar (aka com.companyname.app3:style/Theme.MaterialComponents.Light.DarkActionBar) not found.

Check your Resources\values folder and delete styles.xml and try building again. If you have a themes.xml file as well that’s giving error messages, try deleting that too.

It runs?

Most likely you’ll have references to other projects and NuGet packages installed that you’ll need to bring back in. You’ll have to make sure that your NuGet packages target .NET 6. Otherwise you’ll have to find replacements or wait for the maintainers of the packages to update to .NET 6. As for the project references, you can add those back in in the same manner as you would do for SDK-style projects.

Final notes

As mentioned, this is not a full guide on how you can fully upgrade your Xamarin.Android project. These are just tips to help you on your path and things that I stumbled upon that I figured might help others. The .NET Upgrade Assistant, which is great for bigger and more extensive projects, seem to support upgrading from Xamarin.Forms to .NET MAUI, but it didn’t work when I tested it with the native templates. Maybe we’ll see this happening when .NET 7 ships in November.

I created some sample code on GitHub where I created a File -> New Xamarin.Android Project and converted it to a .NET 6 application using these steps. Check out the commit history to see the steps I mentioned here.

6 thoughts on “Converting Xamarin.Android template to .NET 6”

  1. I have the laster Visual Studio 2022, but can’t see those template (Android Application and iOS application), just the old xamarin.androidand xamarin.ios…

    I have .NET 6.0 installed… What am I missing ?

    1. Huh, that’s weird. I also have Visual Studio 2022 Preview installed, maybe that has something to do with it

  2. To see the project templates in Visual Studio, you should update Visual Studio and run the following commands:

    sudo dotnet workload install android
    sudo dotnet workload install ios

    If you restart Visual Studio, you will see the “iOS application” and “Android application” templates.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.