Skip to main content

Developing with Avalonia for Android

Setting up your developer environment

Follow these steps to install the tools you will need, using the CLI:

  • Check that you have installed a compatible version of the .NET SDK. The lowest version that works with Avalonia UI is 6.0.2.00.
info

You can see the versions of the .NET SDK here.

  • You may need to uninstall an old version of the Android Workload. To do this, type the following command:
dotnet workload uninstall android
  • Install the Android Workload. To do this, type the following command:
dotnet workload install android
info

You may need to run the above commands with sudo.

Install the Android SDK

There are several ways to install the Android SDK. Choose the one that matches your development environment.

If you have Visual Studio or Visual Studio for Mac then follow the guide found here.

If you use JetBrains Rider then follow the guide here.

Alternatively you can install the Android command line tools from here.

This toolset has a command line based SDK manager that can be used to install the SDK. On successfully installing the Android SDK, add the path to the sdk to your PATH environment variable, directly in bash or in your profile's .bashrc file on Linux.

export ANDROID_HOME=/path/to/sdk
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

You can also directly specify the Android SDK location in the dotnet commands when you build, run or deploy the dotnet Android project, by setting the AndroidSdkDirectory variable in the command:

dotnet build ... /p:AndroidSdkDirectory=/path/to/sdk

Ensure you've installed the JDK 11 or above using your platform's package manager. This is already done if set up using Visual Studio or JetBrains Rider as stated above.

There is also a tool in development called MAUI Check that can install all the required SDKs and tools for you automatically:

dotnet tool install -g Redth.Net.Maui.Check
maui-check

With the above Android development environment setup, you will be able to build Android applications, and run them in a simulator on your platform.

Running your app on an Android device

To deploy and run the Application on a real Android device, make sure of the following:

  1. Have the Android version on the device match the supported or target versions in AndroidManifest.xml (if no versions are specified, assume that target version is latest, supported by Xamarin/MAUI),
  2. Connect the device to development machine and enable USB Debugging in developer settings,
  3. If default connection mode is "battery charging only", switch to MTP or another mode (otherwise ADB won't connect to the device),
  4. Have matching Android SDK installed to the target SDK version in AndroidManifest.xml,

Deployment is done via running the dotnet run command (if using console), or via the following setup (if using Visual Studio Code):

tasks.json:

{
"version": "2.0.0",
"tasks": [
{
"label": "build-android",
"command": "dotnet",
"type": "process",
"args": [
"build",
"--no-restore",
"${workspaceFolder}/<ProjectName>.csproj",
"-p:TargetFramework=net6.0-android",
"-p:Configuration=Debug"
],
"problemMatcher": "$msCompile"
}
]
}

where <ProjectName> is your Android-specific Avalonia project name.

Running your app on an Android simulator

Assuming you have created a project called HelloWorld. Enter the directory HelloWorld.Android from the command line.

To build the project for Android run the following command.

dotnet build

To run the project in a simulator, run the following command.

dotnet run
Discussion

Have questions or feedback? Join the conversation below.