Xamarin.Android on Linux

Thanks to the awesome guys at JetBrains and their Project Rider IDE, it’s now possible (unofficially) to develop Xamarin.Android apps on Linux.

This is a basic project created with Project Rider and running on the emulator:

Once I had a basic application up and running, I tried an application I’ve had in the store for a while (Temperament). It worked perfectly:

I’m running KDE NEON based upon Ubuntu 18.04, which you can download from here. For help on setting up KDE NEON, see here.

Note: Project Rider and Xamarin.Android is still a work in progress, so things change from time to time. I’ve updated this document as per my last successful install on Kde Neon and Ubuntu 19.04. There’s an Updates section at the end as a kind of history of the changes. If you get stuck, take a look at that section and some of the comments. These instructions are for Ubuntu 18.04 based distributions, for Ubuntu 19.04 there’s a few changes, please see here.

Install Essential Libraries

sudo apt-get install curl lzip lunzip plzip build-essential cmake automake autoconf libc6-i386 libncurses5:i386 libstdc++6:i386 lib32z1 libc6:i386 libbz2-1.0:i386 uudeview apt-transport-https

Setup Android Studio

Before installing Android Studio, starting with Ubuntu 18.04 you need to install qemu-kvm. Enter these commands:

sudo apt install qemu-kvm

sudo adduser $USER kvm

sudo reboot

Next install Android Studio by downloading from here, extracting the archive, then running bin/studio.sh from the newly created android-studio folder.

Once Android Studio is up and running, test you can create a simple Android project and deploy to a virtual device (I created a Nexus 5×28 x86_64 device).

Now install SDK 28 and the NDK bundle via Android Studio.

Install Mono and .Net Core

Next install Mono as per the instructions here, and .NET Core as per here.

Update your .bashrc (found in your home folder), it should look something like this:

bash

Note: I had installed Android SDK to the default folder: $HOME/Android/Sdk

Update: Creating a symlink (see the following commands) was not required on my latest install, I advise you skip it. You can always do it later if you have a problem starting an emulator.

The next thing we need to do is create a symlink to the Android Emulator:

cd ~/Android/Sdk/tools

ln -s ../emulator/emulator emulator-x86

Install Oracle JDK

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

sudo apt-get install oracle-java8-installer

Install Xamarin.Android

Download a stable build from here.

I used: xamarin.android-oss_v9.1.199.8_Linux-x86_64_master_878ba261-Debug.tar.bz2

Update: The official Xamarin for Linux builds have been failing for over a month. Use the last successful build, you can find artifacts for the last successful build here.

Extract it:

tar xjf ./xamarin.android-oss_v9.1.199.8_Linux-x86_64_master_878ba261-Debug.tar.bz2

Go to the unpacked dir:

cd ./xamarin.android-oss_v9.1.199.8_Linux-x86_64_master_878ba261-Debug

Make dirs:

sudo mkdir "/usr/lib/xamarin.android"
sudo mkdir "/usr/lib/mono/xbuild/Xamarin/"

Copy dir from extracted files:

sudo cp -a "bin/Debug/lib/xamarin.android/." "/usr/lib/xamarin.android/"

Delete dirs for next step:

rm -rf "/usr/lib/mono/xbuild/Xamarin/Android"
rm -rf "/usr/lib/mono/xbuild-frameworks/MonoAndroid"

Make Symlinks:

sudo ln -s "/usr/lib/xamarin.android/xbuild/Xamarin/Android/" "/usr/lib/mono/xbuild/Xamarin/Android"
sudo ln -s "/usr/lib/xamarin.android/xbuild-frameworks/MonoAndroid/" "/usr/lib/mono/xbuild-frameworks/MonoAndroid"

Replace System.Reflection.Metadata

As mentioned in the updates section below, you need to replace an assembly. Download this nuget package, unzip it, and then copy the file System.Reflection.Metadata.dll from the lib/netstandard2.0 folder over the file found in /usr/lib/mono/msbuild/15.0/bin/

Configure SDK and NDK paths in Rider settings

Update: In the latest version I just created a new Xamarin.Android project and the template prompted me to click a button to set up these paths.

Go to “File | Settings | Build, Execution, Deployment | Android” and set “Android SDK Location”, “Android NDK Location”, and “Java Development Kit Location” . For me, the locations are:

/home/david/Android/Sdk

/home/david/Android/Sdk/ndk-bundle

/usr/lib/jvm/java-8-oracle

Update your Project File

You can now create a new Xamarin.Android project.

Whether you are creating a new project, or using an existing one, you need to update your project file. Open the .csproj file in your favourite editor, find this section:

<PropertyGroup Condition=” ‘$(Configuration)|$(Platform)’ == ‘Debug|AnyCPU’ “>

And add this setting:

<AndroidSupportedAbis>arm64-v8a;armeabi-v7a;x86;x86_64</AndroidSupportedAbis>

section

If you are using an existing project and you have the following import directive at the end of the project file:

<Import Project=”$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets” />

Change it to this:

<Import Project=”$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets”/>

You should now be able to build and deploy your project to a virtual device on Linux.

Note: if you copy and paste, ensure the quotes are correct or you’ll get an error.

Strings.xml should be strings.xml

If you create a new project and get an error that a string resource can’t be found, rename the file Strings.xml in the file system to strings.xml.

strings

Conclusion

I’d like to say a big thank you to the JetBrains team for this blog, and to Andreas Go for his help with getting the project file right. Project Rider is shaping up to be an awesome IDE.

Update:

Recently people have been reporting build issues, thanks to Abdelrahman here is a fix:

I was having the same issue as @JENS ECKERVOGT, after some researching about the subject i found out they changed from the msbuild built-in System.Reflection.Metadata to the one that comes with corefx project because it’s much faster.

so i searched nuget for System.Reflection.Metadata (https://www.nuget.org/packages/System.Reflection.Metadata/) downloaded the package and opened it in archive manager copying the dll that’s in netstandard 2.0 to msbuild directory and also did the same with (https://www.nuget.org/packages/System.Collections.Immutable/) and every thing works fine now

Update 2:

I’ve just been through a complete re-install, all of the above instructions worked fine (I needed to use Abdelrahman’s fix mentioned above but only for System.Reflection.Metadata).  The location of the msbuild folder is:

/usr/lib/mono/msbuild/15.0/bin/

For this install, I used this Xamarin build:

xamarin.android-oss_v9.2.99.121_Linux-x86_64_master_2660230c-Debug.tar.bz2

I had two minor issues:

Firstly, the settings (see Configure SDK and NDK paths in Rider settings section) has changed from:

File | Settings | Build, Execution, Deployment | Xamarin

to

File | Settings | Build, Execution, Deployment | Android

And you will need to enter the jdk location too, I specified:

/usr/lib/jvm/java-8-oracle

The article has been updated accordingly.

Secondly, if you copy and paste the project file changes mentioned above (see the Update your Project File section) to an existing .csproj file, make sure the double quotes are correct after you paste the specified text, or your project won’t load.

This is a second application I just managed to run in Xamarin.Android on Linux following my latest install. I wrote this a few years ago and it is using a background service for playing an audio playlist. Everything is working as expected:

xamarin2

14 thoughts on “Xamarin.Android on Linux

  1. I Can’t get it to work.
    I followed all your Steps however I can’t build a new Project.

    AndroidManifest.xml(6): [APT0000] resource string/app_name (aka Template.Template:string/app_name) not found.
    AndroidManifest.xml(7): [APT0000] resource string/app_name (aka Template.Template:string/app_name) not found.
    Xamarin.Android.Common.targets(2434, 3): [APT0000] failed processing manifest.

    Like

    1. Hi Simon, I’m too busy at the moment to investigate your issues, but in the meantime, can you check the entry in the manifest matches your resource name, i.e. string/app_name should probably be strings/app_name?

      Like

      1. Hi Velocity 🙂
        I can’t really explain it but it suddenly started working for me…

        I first thought there was a problem with the xamarin build.
        As Consequence I did download the build again, reinstalled libzip4.
        While it still didn’t work I tried to build a HelloWorld sample without rider, just using the xabuild…

        After that rider suddenly recognizes everything to be fine and I can now Build and Run perfectly fine..

        Maybe I should add something..
        From the template rider creates a strings.xml in the ResourcesFolder, however it References a “missing” Strings.xml (CaseSensitivity!!), so you either have to check the Dependencies or simply rename this file.

        Liked by 1 person

      2. Hey @Simon and @CSVelocity,
        Yeah you are right,

        I have tried to fix but I have sometimes error why does Xabuild won’t build cause it has sometimes assemblies not found like this

        How do you have not problem but for me not fixed. How do you have success? PLEASE TELL ME! I am very disappointed. Because I really miss to develop with Xamarin.Android on Linux and Rider. – Because I don’t like Windows 10 because Windows 10 failed major update into default desktop ( it seems after installation of Windows 10 ) since September/October 2018. I stopped work with Windows 10. I am working on Ubuntu. Why do idiots not support for MonoDevelop. That is why I am very disappointed and I don’t understand how does MonoGame Team work Android project for MonoDevelop. I feel like they lie me. Since 2 or 3 weeks later I found Rider and I have big problem with Xamarin.Android and Rider because it seems like string.xml and sometimes assemblies not found. I have already compiled Xamarin.Android from Github and compiled on Ubuntu 18.04 and It works fine ok. But Rider and xabuild won’t compile because I have problem with not found assemblies. How do I find?
        in /usr/lib/mono/4.5 or /usr/lib/mono/xbuild.frameworks or I want find for current assemblies.

        Like

      3. Hey Jens,

        well I can only take a guess but it looks like you’re missing some mono packages.

        Have you tried reinstalling mono ??
        sudo apt-get install –reinstall mono-complete

        Like

  2. I am very disappointed – Because it looks shit because Rider shows only latest Android version I want use old version of Android 8.1 ( Oreo )

    Why does Rider not compile successful ? =(

    Because I have already tried downloading Xamarin Android from github and I compiled successful. copy to /usr/lib/mono than i open Rider and create Xamarin Android App and it seems like shit always error error error error error….

    I have tried 5 or 6 times = NO SUCCESS.

    How did you get success? PLEASE SHOW ME VIDEO WITH SUBTITLE I want understand.

    Thanks

    Like

    1. Hi Jens, sorry to hear it is not working for you. I documented the steps I took to do two successful installs. I’ve been super busy lately, but will do another install soon and if necessary I’ll update the blog accordingly. Regarding versions, I just use what Microsoft makes available via their Jenkins builds.

      Like

      1. Thanks for your feedback Simon, glad you got it working. Hopefully Rider will evolve to be something like Android Studio for Xamarin, it’s awesome coding C# in Linux 🙂

        Like

  3. @Jens

    Hi Jens, sorry to hear of your troubles again, must be very frustrating.

    Are you able to create an Android Studio project and run it in an emulator? Can you create a mono project in Project Rider and run it successfully? If not, perhaps you’ll need to try a fresh install of Ubuntu 18.04 and follow the steps in the post exactly? For example, I don’t compile Xamarin.Android from Github.

    If you do try a fresh install, you’ll need to install packages like buildessential, and any libraries Android Studio needs to work correctly on Ubuntu. Make sure Android Studio and Project Rider is working BEFORE proceeding with the Xamarin.Android installation (Create a simple Android project, then create a Mono console project and run them).

    I’m really busy at the moment, but when I get a chance I’ll do another post soon specifically for Ubuntu 18.04 – from initial install up to and including Xamarin.Android for project Rider. In the meantime, I hope this helps.

    Like

  4. I was having the same issue as @JENS ECKERVOGT, after some researching about the subject i found out they changed from the msbuild built-in System.Reflection.Metadata to the one that comes with corefx project because it’s much faster.

    so i searched nuget for System.Reflection.Metadata (https://www.nuget.org/packages/System.Reflection.Metadata/) downloaded the package and opened it in archive manager copying the dll that’s in netstandard 2.0 to msbuild directory and also did the same with (https://www.nuget.org/packages/System.Collections.Immutable/) and every thing works fine now 🙂

    Liked by 1 person

Leave a Reply to csvelocity Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s