Monkey business — write once, deploy on iOS, Android and Windows with Xamarin

Microsoft and Nokia missed the boat on tablets and smartphones. Microsoft’s Surface RT tablets were priced very high, and there were not many takers, with the result that Microsoft wrote off 900 million dollars as inventory adjustment. Nokia Lumia line with Windows Phone OS has been well received. Microsoft has bought Nokia’s struggling phone business. Though Windows Phone OS has registered the highest growth in the recent years, its market share is less than 4 percent.

Android has close to 80% of the smartphone market. But the consolation prize is that Microsoft makes about 2 billion dollars a year from Android licensing. Android applications are mostly written in Java and while there are differences — it is not compiled to Java byte code, not all apis and libraries are available — if you know Java you can start writing Android applications.

While Java was a hugely popular and dominant language even before Android came on the scene, this was not the case with Objective-c, the language used by Apple. Outside of Apple, there was essentially no adoption. But with rise of Apple from an all time low when the stock was trading at $4 to where it was valued at half a trillion dollars (some pundits said that it will be the first company to reach 1 trillion), you saw the huge rise in the popularity and usage of Objective-c, a language heavily influenced by Smalltalk and message passing. If you want to write an ipad or iphone app that can take you from rags to riches, you need to write it in Objective-c!

So, if you want to write an application that runs on phone, phablets and tablets, you either pick the Apple ecosystem, hire Objective-c programmers and deploy the app to the AppStore or you pick the Android ecosystem, hire Java programmers and deploy your app to the Google Play Store.

Many small companies/startups may not have the resources to hire both Objective-c and Java programmers and so will pick one platform and if they achieve some success and revenue they will target the other.

Due to the very low market share, Microsoft pays developers to port many of the popular apps to the Windows mobile platform.

What if you are a .NET developer? There are millions of developers with knowledge of C#. Many of them have started learning Objective-c or Java so they can develop applications for the iOS or Android platforms — and that is a shame because C# is a pretty fine, powerful, succinct language.

Every version of C# added some nice features, with minimal backward incompatibility. To name a few: properties, delegates (essentially typesafe function pointers) and events in 1.0; generics, anonymous methods, nullable types in 2.0; automatically implemented properties, lambda expressions, anonymous types, limited type inference, LINQ in 3.0; dynamic typing [^1] in 4.0; the powerful async feature in 5.0.

Async allows you to write easy to understand sequential code where you may call methods that access webservices or perform database operations and while these happen on the same thread (could be UI thread), the thread is not blocked while waiting for these operations to complete. Compiler magic handles all of this so that you write clean code with no callbacks or asynchronous event handling. You can single step thru this code in the Visual Studio debugger!

So, basically as a C# programmer, you cannot leverage the considerable expertise you have in the language and have to acquire new language skills that can take years. Or can you? What if you can develop fully native applications for Android and iOS using C#? Apps that you can sell on the Apple Store and Google Play store? Native Android, iOS and Windows Phone applications that share about 80% or more of the code (all of the business logic) so that you can simultaneously develop for all 3 platforms? Well, you can! Enter Xamarin, which got the Jolt Award for best mobile development tool.

You can use the familiar and powerful Visual Studio (not Express) with full Intellisense or use the standalone Xamarin Studio, an IDE that runs on Windows and MacOS. Xamarin Studio, though relatively new, is quite polished and is steadily improving. You get full debugging support. I have used both IDEs and am quite impressed. You can directly deploy iOS apps to Testflight from Xamarin Studio. Visual Studio has a concept of a Solution with multiple Projects. So, you can have a single solution with several Projects namely Windows WPF, Android, iOS, Windows Phone 8, Windows Store. It is nice to be able to test your business logic by setting the "Startup Project" to WPF, build and run on the desktop as the turnaround time is virtually instantaneous, add features, fix bugs, then set the Startup Project to Android, build and run the app on the superfast GENYMOTION emulator and then test it on the Windows Phone and so on. For iOS apps, though you can use Visual Studio, you need a Mac (either next to you or have remote access to a Mac build server) to compile and run the code. If your primary computer is a Mac, then you can use the Xamarin Studio to develop iOS and Android apps and you would need a Windows machine (real or virtual) to develop Windows apps.

You can make rapid progress in developing your application by using off-the-shelf components (some free, some paid) available in the Xamarin component store — authentication SDK, charting library, database encryption, signature pad, Dropbox sync, Facebook sdk, Google maps, Azure mobile services, Reactive Extensions etc. The MVVM (Model-View-ViewModel) framework is quite powerful as it separates the presentation logic from the actual presentation, and this looser coupling greatly enhances testability. You don’t have to click on a button or some UI element to test. MvvmCross makes this powerful framework available on iOS and Android also. Checkout the amazing MvvmCross video tutorials for more information.

It is important to note that with Xamarin there is no UI abstraction layer. The iphone/ipad application will use the native User interface controls provided by iOS except that it is called from C#. For e.g. buttons on your ipad app are created using iOS’s UIButton class. Similarly the Android app will use the native Android UI classes.

So, only the UI code has to be written for each platform, which is the right thing to do. Xamarin makes this possible by projecting the entire native iOS SDK and Android SDK into C# classes.

When Apple pushes out an update to the API, Xamarin makes it available in a day.

Apple doesn’t allow code generation at runtime. The iOS app is AOT (Ahead of time) compiled to ARM machine code.

It all started with Mono, an open source implementation of the .NET framework, which allowed C# programs to be run on Linux. It had a full ahead of time compiler that can be used to create static applications. Mono is Spanish for monkey. Before that there was Ximian (a play on simian) which provided the Ximian desktop suite for Linux. Continuing with the monkey theme, the founders Miguel de Icaza and Nat Friedman decided to name their new company (and their product) Xamarin (from the tamarin monkey). The irony is that these two toiled for years to advance the state of the art in the Linux desktop, spent 10 years on Mono, and while Linux is everywhere except the desktop, they have found success in leveraging Mono to allow developers to create native applications on Android, iOS and the Mac! As Nat Friedman says, Xamarin is an overnight success that took 10 years to develop! And of course you can create apps for Windows desktop, Windows Phone and Windows Store as they use the .NET framework.

Some example of Apps that were developed with Xamarin:

Infinite Flight is a Flight Simulator for Windows Phone 7, iOS and Android developed in that order by a team of — get this — 2 dedicated developers. Once they had the sim working on Windows, they ported it to iOS. Using Xamarin, they could share the C# code and so the porting was quite fast with minimal code changes. On Windows Phone, Microsoft’s XNA was used for video game development. As XNA is not available for iOS, they used MonoGame which is a port of XNA. MonoGame didn’t have 3D support and these developers added support for it and contributed it back. The iOS app was well received and then they ported the App to Android. Needless to say, if any App requires high-performance it is a flight simulator and InfiniteFlight runs quite nicely on the iPad.

RDIO provides internet radio service with their mobile app. Initially they had three different codebases for iOS, Android and Windows Phone and it was becoming difficult to manage feature updates and bug fixes. They then switched to Xamarin and soon launched a major update simultaneously on iOS and Android as they were able to share 50000 lines of code.

Using Xamarin and Monogame, Hitcents produced an award winning game called "Draw a Stickman" on Windows 8 and iOS (Android coming soon) with about 95% shared code.

Checkout the xamarin app showcase for more applications. You can check it out by downloading the free trial version of Xamarin.

Obviously, all of this has got Microsoft interested. Microsoft has freely licensed the Portable Class Library (PCL) to Xamarin. Microsoft announced "We are making portable Microsoft .NET NuGet libraries available under a new license that enables use on all platforms. This includes HttpClient, Immutable Collections, SignalR, ODataLib and several others." This is really good news as more of the code can be shared on all the platforms.

There are rumors swirling that Microsoft may be planning to acquire Xamarin. And that would be good news for C# lovers. From time to market, bug fixing and feature enhancement perspective, it is better to do it all in one language, C#, rather than in Objective C, Java and C#.

[^1]: Note that C# is a statically typed language and that is a good thing. Though C# 4.0 added support for dynamic types (basically a static type called dynamic!), there is no need to use it except for interoperability and for using COM api.

Leave a Reply

Your email address will not be published. Required fields are marked *