6

I want to develop applications for Android in C#. On Windows I tried Xamarin Studio and I was very happy with it. Is there a way to use Xamarin on Ubuntu?

I noticed there are Xamarin.Android binaries for Linux available on the internet, but those are only for Xamarin.Android 9.22, I would love to use Xamarin.Android 10. Can this be done?

Lastly, I know there is no Xamarin Studio for Linux. What IDE can I use instead of it? How do I set it up for building Android apps and deploying them?

1 Answer 1

9

Short answer

Yes, you can develop with Xamarin.Android on Ubuntu! However, since Linux, sadly, isn't oficially supported by Xamarin, it's not as straightforward as it would be on Windows. In short, first of all you have to install Java, Mono and Android Studio, then you have to compile Xamarin.Android yourself and finally you have to setup your favorite IDE to work with Xamarin.

.

Prerequisites

Installing Android Studio

(Note: In theory you could just download the Android SDK and NDK, I haven't tried it though.)

Installation of Android Studio on Ubuntu has been covered thoroughly in a different SO answer, but I'll go over it quickly: first you need to install Java 8 JDK. While it's officially recommended to use Oracle Java, OpenJDK will do just fine:

sudo apt install openjdk-8-jdk

You'll also need to set the JAVA_HOME variable to match the installation directory of your Java 8 JDK. To do so, add this line to your ~/.bashrc:

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

Then you have to download Android Studio and unzip it somewhere you like, for example your home folder, or /opt/androidstudio.

Once you have it, you can start it up and open SDK Manager in the top right corner:

SDK Manager

Now download the SDK versions you want to target. Be sure to also install NDK, though, as that's what we will need to deploy Xamarin applications. Then you can try building an sample app and deploying it to your phone (or to a running emulator) just to make sure everything works all right.

You also need to set the AndroidSdkDirectory environment variable, so that Xamarin knows where to look for the installed SDKs. You can do this by adding this line to your .bashrc, .zshrc or whatever shell you use:

export AndroidSdkDirectory=~/Android/Sdk

You can also create aliases for useful commands:

alias sdkmanager=~/Android/Sdk/tools/bin/sdkmanager
alias avdmanager=~/Android/Sdk/tools/bin/avdmanager
alias adb=~/Android/Sdk/platform-tools/adb
alias android-emulator=~/Android/Sdk/emulator/emulator

Installing Mono

As of October 2019, Ubuntu still doesn't have Mono 5 in its repositories. Therefore we'll have to add the official Mono repositories (it's a good idea anyway):

sudo apt install gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update

Next step we install the Mono development packages. Maybe you won't need everything, but I was lazy and went with mono-complete. You'll also need nuget, so let's install that too:

sudo apt install mono-complete nuget

You can check whether the correct version was installed by trying the msbuild command. The old versions of Mono didn't have it, so if the command was found, you're probably ready to go.

.

Compiling Xamarin.Android

Dependencies

There are some dependencies needed to successfully compile Xamarin.Android. Most of those packages will be already installed on your machine, so you can try skipping this step and only installing those that are missing. However, it takes some significant time to retry the build, so you'll save it by installing them right away:

sudo apt install autoconf autotools-dev automake cmake build-essential curl gcc g++ g++-mingw-w64 gcc-mingw-w64 git libncurses5-dev libtool libz-mingw-w64-dev libzip-dev linux-libc-dev make ninja-build p7zip-full sqlite3 vim-common zlib1g-dev linux-libc-dev:i386 zlib1g-dev:i386 libx32tinfo-dev

Cloning

Fetch the latest Xamarin.Android source code from GitHub:

git clone https://github.com/xamarin/xamarin-android.git
cd xamarin-android

You probably don't want to build from master, so choose a stable release in the GitHub repo and check it out with git checkout [chosen commit hash]. Make sure to also reset the submodules to the correct version.

System detection

If you're using a distribution derived from Ubuntu, you'll have to add an extra line to the system detection code to make it work:

nano build-tools/xaprepare/xaprepare/OperatingSystems/Linux.cs

and under the line

{"Ubuntu",    (ctx) => new LinuxUbuntu (ctx)},

you have to add a line for your system, eg. for Elementary OS it's:

{"elementary",(ctx) => new LinuxUbuntu (ctx)},

If you're on pure Ubuntu, you can skip this step.

Build

First you have to build all the dependencies:

make prepare

And then you can build Xamarin.Android itself. If you only want to target the latest Android, run:

make

(The targeted version is the latest Android version supported by your application. So if you want to write apps for Android versions from 7 to 10, you want to target v10.0. If you want to be able to target any version of Android, you'll have to run make jenkins instead.)

Both of those steps require a significant amount of patience and Internet connection.

Installation

Finally you can install Xamarin.Android to your system:

sudo make install prefix=/usr

.

Using Xamarin

Set up a project

To test that our build actually works, we want to build and deploy an actual Xamarin project. Ideal for this purpose are the demo projects in xamarin/monodroid repository. So let's clone it:

git clone https://github.com/xamarin/monodroid-samples.git
cd monodroid-samples/Button

Add supported ABIs

For some reason, we need to add the list of supported ABIs to the project file. Open it in an editor:

nano DroidButton.csproj

And to the first PropertyGroup add this line:

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

If you didn't build with make jenkins, you'll also want to change the TargetFrameworkVersion to v10.0.

Build

msbuild

Deploy

Either start up a virtual device in AVD Manager (accessible eg. through Android Studio), or connect a phone in debug mode and then run:

msbuild /t:Install

This should install the app to your phone. If you want to build and deploy with one command, you can use /t:Build,Install. If you want to pass extra parameters to ADB (for example to identify the device where to deploy), use /p:AdbTarget=[adb options], for example: msbuild /t:Install /p:AdbTarget=-s1080c487.

.

Summary

So we've got Xamarin set up! This was a long journey, wasn't it? Full of surprises, letdowns and often pure misery. Was it worth it? You are to decide, but for me it definitely was. Now, take a break (only a short one though!) and then start building your dream application!

15
  • 1
    I've built Xamarin 10.0.0.43 with make jenkins and uploaded it here. Hopefully it can save you some time.
    – m93a
    Oct 3, 2019 at 11:00
  • I keep getting this error when I run make unable to find package java.lang in classpath or bootclasspath not sure what I'm doing wrong
    – Eman
    Oct 30, 2019 at 3:32
  • 1
    Aaah, I see it now. I've got this line in my .zshrc: export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
    – m93a
    Oct 31, 2019 at 9:13
  • 2
    I got it working, according to the xamarin guys I should rely on the jdk & android sdk provided by them during the prepare process, that was the trick for me
    – Eman
    Nov 2, 2019 at 1:09
  • 1
    Okay so I found the file xamarin-android/build-tools/xaprepare/xaprepare/ConfigAndData/Dependencies/Linux.DebianCommon.cs which has names of all the packages that make searches for. There, I changed libncurses-dev to libncurses5-dev. Everything works now. Ubuntu scares me sometimes! Nov 24, 2019 at 15:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .