- How to develop for Android with Xamarin?
- 1 Answer 1
- Short answer
- Prerequisites
- Installing Android Studio
- Installing Mono
- Compiling Xamarin.Android
- Dependencies
- Cloning
- System detection
- Build
- Installation
- Using Xamarin
- Set up a project
- Add supported ABIs
- Build
- Deploy
- Summary
- Install and setup Xamarin.Android
- Windows Installation
- Mac Installation
- Android SDK Setup
- Android Emulator Setup
- Android Device Setup
- Microsoft Mobile OpenJDK Preview
How to develop for Android with Xamarin?
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
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:
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
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 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:
And to the first PropertyGroup add this line:
If you didn’t build with make jenkins , you’ll also want to change the TargetFrameworkVersion to v10.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!
Install and setup Xamarin.Android
The topics in this section explain how to install and configure Xamarin.Android to work with Visual Studio on Windows and macOS, how to use the Android SDK Manager to download and install Android SDK tools and components that are required for building and testing your app, how to configure the Android emulator for debugging, and how to connect a physical Android device to your development computer for debugging and final testing your app.
Windows Installation
This guide walks you through the installation steps and configuration details required to install Xamarin.Android on Windows. By the end of this article, you will have a working Xamarin.Android installation integrated into Visual Studio, and you’ll be ready to start building your first Xamarin.Android application.
Mac Installation
This article walks you through the installation steps and configuration details required to install Xamarin.Android on a Mac. By the end of this article, you will have a working Xamarin.Android installation integrated into Visual Studio for Mac, and you’ll be ready to start building your first Xamarin.Android application.
Android SDK Setup
Visual Studio includes an Android SDK Manager that replaces Google’s standalone Android SDK Manager. This article explains how to use the SDK Manager to download Android SDK tools, platforms, and other components that you need for developing Xamarin.Android apps.
Android Emulator Setup
These articles explain how to setup the Android Emulator for testing and debugging Xamarin.Android applications.
Android Device Setup
This article explains how to setup a physical Android device and connect it to a development computer so that the device may be used to run and debug Xamarin.Android applications.
Microsoft Mobile OpenJDK Preview
This guide describes the steps for switching to the preview release of Microsoft’s distribution of the OpenJDK. This distribution of the OpenJDK is intended for mobile development.