Building an Android app with .NET 6

Firstly, the news – Microsoft has made preview 1 of .NET 6 available. Make sure you checkout the long blog post that gives background on what’s coming in .NET 6 and what’s shipped in the preview. Setup Next, grab the bits and get ready to build an Android app using .NET 6. Make sure you … Continue reading “Building an Android app with .NET 6”

Firstly, the news – Microsoft has made preview 1 of .NET 6 available. Make sure you checkout the long blog post that gives background on what’s coming in .NET 6 and what’s shipped in the preview.

Setup

Next, grab the bits and get ready to build an Android app using .NET 6.

  • Make sure you have the latest Visual Studio Preview installed.
  • Go to the Download .NET 6.0 page and pick the appropriate SDK.
  • Go to the Net6-Mobile-Samples repro and grand the installers for the iOS and Android workloads
  • [optional] Enable Visual Studio via the feature flag – Open a command prompt as Administrator. Execute the following
cd "C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin\SdkResolvers\Microsoft.DotNet.MSBuildSdkResolver" 
echo > EnableWorkloadResolver.sentinel

Now, somewhere along the line my Android SDK installation got all messed up. Luckily the Android SDK Manager is able to detect this and will prompt to repair your installation if required. I’m not saying this will happen to everyone but it’s worth taking an opportunity to open the Android SDK Manager and confirm everything is installed correctly.

Open the Android SDK Manager from the Tools, Android menu.

Follow any prompts to update or repair your Android SDK.

New Project

For Android, there’s a new dotnet project template, making it easy to create a new Android application from the command prompt by typing “dotnet new Android -o <AppName>”.

Building

The next step is to navigate to the directory where the Android app was created and run dotnet build. Unfortunately this didn’t work for me, giving an error stating “error NU1101: Unable to find package Microsoft.Android.Runtime.android.21-arm64. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, nuget.org

To fix this we need to specify a NuGet.config with the following content.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <!-- ensure only the sources defined below are used -->
    <add key="dotnet6" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json" />
    <add key="xamarin" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/xamarin-impl/nuget/v3/index.json" />
    <add key="public"  value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
  </packageSources>
  <config>
    <add key="globalPackagesFolder" value="packages" />
  </config>
</configuration>

Unfortunately whilst this fixes the issues restoring packages, it still doesn’t fix the build. In fact you get pages and pages of errors, enough to make you think that you’ve done something wrong.

If you scroll back through the list of errors to the first error, it’s something like “packages\microsoft.android.runtime.android.21-arm64\11.0.200-ci.master.85\runtimes\android.21-arm64\native\libmono-android.debug.so : error XA4301: Cannot determine ABI of native library 'packages\microsoft.android.runtime.android.21-arm64\11.0.200-ci.master.85\runtimes\android.21-arm64\native\libmono-android.debug.so'. Move this file to a directory with a valid Android ABI name such as 'libs/armeabi-v7a/'. [C:\temp\MyFirstAndroidNetApp\MyFirstAndroidNetApp.csproj]“. All you need to do to fix this is to run “dotnet build” again. Don’t ask me why but the first time you run it, you’ll still see the API level 21 errors but run it again and you should see success.

Running

You can launch your app on the emulator or an attached device by calling “dotnet build -t:run“. However, if you don’t have a device attached or the emulator open you’ll see the following error “error XA0010: No available device.“.

You can either plug in a device, or you can use the Android Device Manager (from the Tools, Android menu in Visual Studio) to launch an emulator, or you can simply open the whole project in Visual Studio (assuming you enable the feature flag from earlier) and click on the play button.

This should launch the emulator, deploy and then run the app. Note that you may see the build error from earlier, in which case, just press the play button again.

Summary

Ok, so you might be asking what we’ve achieved at this point, considering we were able to build an Android app in Visual Studio using C# for a while now. There’s a couple of important things here. Firstly that this is the first time we’re building against the same .NET that you’d use elsewhere, so a truly unified framework.

Secondly, we’ve moved across to the new style, SDK based, csproj file format that supports multi-targeting. In theory this means you can have a single project that targets Android, iOS, Windows etc. Hold that thoughts as we’ll circle back to this in subsequent blog posts.

Leave a Reply

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