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?
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:
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
: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:
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:You can also create aliases for useful commands:
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):
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 neednuget
, so let's install that too: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:
Cloning
Fetch the latest Xamarin.Android source code from GitHub:
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:
and under the line
you have to add a line for your system, eg. for Elementary OS it's:
If you're on pure Ubuntu, you can skip this step.
Build
First you have to build all the dependencies:
And then you can build Xamarin.Android itself. If you only want to target the latest Android, run:
(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 runmake 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:
.
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:
Add supported ABIs
For some reason, we need to add the list of supported ABIs to the project file. Open it in an editor:
And to the first
PropertyGroup
add this line:If you didn't build with
make jenkins
, you'll also want to change theTargetFrameworkVersion
tov10.0
.Build
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:
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!