The .NET 6 team has done another step in the direction of uniforming the processes and tools of writing code for different platforms. Now it is possible to install the stack to compile applications for Android and work with it only via dotnet cli.

Why

Developing cross-platform mobile apps can be done with a large variety of frameworks and tools, but, for native applications (or close-to-native performance), the selection is limited. Xamarin is one of the frameworks that is considered native, and it has evolved a lot over time to stay updated with the latest improvements and features of the mobile OSes.

Up until recently, Xamarin could only be developed with ease in the Visual Studio IDE. The downside is that someone might consider it limiting when it comes to license. On the other hand, VS Code is more widely used by a large group of developers.

The latest version of .NET 6 brought Xamarin inside the dotnet tooling in the form of workload and it’s now possible to get a fully functional development environment, that requires only the dotnet SDK.

What changes

Now it is possible to target Android in a C# project file using the standard SDK project. Here is an example file:

1
2
3
4
5
6
7
8
9
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-android</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.AndroidX.Arch.Core.Runtime" Version="2.1.0.12" />
</ItemGroup>
</Project>

VS Code setup

  1. Have VS Code ready
  2. Install dotnet SDK
  3. Add dotnet workload for Android
    1
    dotnet workload install android-aot
  4. Open the sample project
  5. Install the extensions
  6. Start the emulator from the task

Running

  1. Start the build task
  2. Select the target Run
  3. Wait until the emulator is loaded with the app and it’s showing a white screen
  4. Attach the debugger to start the application

And more

Another area with huge improvements is binding libraries. Just change <OutputType>Library</OutputType> and add the jar or aar. Add Metadata.xml if needed. That’s it.
For more information, have a look here.

References