Android Ported to C#
Java is not the only way to build native apps on Android. In fact, it’s not even the best option: Xamarin.Android offers C# to Android developers as a high-performance, low-power alternative to Java.
Xamarin.Android allows developers to write code using C# while running on top of the Java-powered operating system with 100% API coverage, and then share that same code with iOS and Windows Phone. You can learn more about Xamarin.Android in the Xamarin developer center.
C# ECMA Standard
Unlike Sun with Java, Microsoft submitted C# and the .NET VM for standardization to ECMA, and ultimately saw those standards graduated all the way to ISO, with strong patent commitments, covered by Microsoft’s legally binding community promise.
Xamarin Beginnings – Making Android Even Better
In July 2012 when Xamarin was getting started, we got our team together in Boston to plan the evolution of Mono on iOS and Android. After a day of kayaking in the Charles River, we sat down to dinner and turned our attention to how we could improve the performance and battery life of applications on Android, and make our own Android offering even better.
The Xamarin team after a day of Kayaking, back when we were a small company
Idea for a 100 % C# Android Port
Over and over we came back to the basics: Dalvik is a young virtual machine, it is not as performant or tuned as Mono and suffers from many of Java’s performance limitations without the benefit of the high-end optimizations from Oracle’s HotSpot.
One crazy idea that the team had was to translate Android’s source code to C#. Android would benefit from C# performance features like structures, P/Invoke, real generics and our more mature runtime.
Although nothing happened back in July, this idea stuck in the back of our minds.
Fast forward a few months: Xamarin.Android is doing great and our customers can build amazing apps for Android in C#, and we are starting to think again about improving our own product’s performance on Android. What if we could swap out Java with faster C# and get rid of various Dalvik limitations in the process? Could we create an Android phone completely free of Java, and free of the limitations of the Dalvik VM?
We decided it was crazy enough to try. So we started a small skunkworks project with the goal of doing a machine translation of Android from Java to C#. We called this project XobotOS.
The XobotOS Research Project
The result of our efforts is that today we have most of Android’s layouts and controls entirely in C#. Here is a screenshot of XobotOS running on a Linux workstation, no Java involved:
Getting to this point required that the majority of the Android Java code be translated from Java to C#, so what you see above represents very significant progress. So how did we do it?
Java Translation via Sharpen
Android’s core codebase contains over a million lines of Java code, and we knew we wanted to be able to stay up to date with new releases of Android — in fact, we started with the Android 2.x source code back in 2011, and then upgraded XobotOS to Android 4.0 when Google open sourced Ice Cream Sandwich earlier this year. So for us, the only reasonable option was to do a machine translation of Java to C#, building and maintaining any necessary tools along the way.
The tool we used as a starting point is called Sharpen. Sharpen is famous for helping people such as Frank Krueger port a Java applet to an award-winning iPad app in two months.
We matured Sharpen a lot, and the result is a much-improved Java-to-C# translation tool for everyone. We are releasing this new version of Sharpen today along with the code for XobotOS and we hope that many more people will benefit from it and contribute to it.
So once you have Android running on Mono, the obvious question is — how does Mono perform compared to Dalvik?
When C# came around, Microsoft modified the language in a couple of significant ways that made it easier to optimize. Value types were introduced to allow small objects to have low overheads and virtual methods were made opt-in, instead of opt-out which made for simpler VMs. Later on, Java and C# diverged in the way that they implemented generics. Java went with a full-backwards compatibility approach, while C# baked the support into the runtime. The C# approach led to a simple-to-use, simple-to-understand generics setup as well as being much more performant and complete.
Since then, both the language and the execution environment have continued to evolve and improve. C# went from being a slightly better Java to be light-years ahead. From embracing dynamic programming, bring asynchronicity into the language, introduce iterators, functional programming constructs, embrace parallelism and got a great implementation of generics. Many of the these features came from the research done by Don Syme and his F# team that have kept a steady flow of new ideas getting injected into the language. Furthermore, Mono as a virtual machine has matured substantially in the last 10 years and is now considered to be on its 8th generation.
All of this adds up. You can see the massive difference in the performance of structs and generics in this benchmark we ran of a simple binary tree implementation in Java and C#:
Today we’re proud to announce that we’ve made XobotOS available on Github so that you can try it out yourself.
Our goal as a company is to provide the best platform for building mobile apps, and so XobotOS will not be a focus for us going forward. But it was a fun experiment to run, and as it turns out, a few technologies have come out of the effort that we’ll be able to include in future versions of our products:
Direct Graphics Access to Skia: Currently Xamarin.Android accesses the underlying graphics libraries through Java, with the code that we built for XobotOS, we will skip the middleman and use Mono’s P/Invoke to get straight to the native rendering code in Skia.
Java to C# tooling: Our new version of Sharpen is available as part of our XobotOS release.
Replacing Java code with C# code: We now have the tools necessary to replace some chunks of Java code with C# code where performance is critical and when C# can offer better solutions than Java has. Our plan is to take elements of the research project and integrate those into our products.
A project that we started because we thought it would be fun to do has turned out to yield some serious benefits for our products. It’s important for a startup to stay focused, but sometimes you have to try something crazy to make progress. And who knows, maybe Google will thank us some day :-).
Get started with Xamarin today to create beautiful Android, iOS, and Mac apps written in C#. Sharing the same code between all of these platforms including Windows and leverage your favorite .NET libraries, and still easily incorporate platform-specific libraries and frameworks when you want to.