Xamarin! C# meets Android & iOS
I’ve been longing to write about this topic for quite a while now, so todays blog post will be my first blog post about Xamarin! Many of you probably have already heard of Xamarin, some of you are probably using it already – and those who do, know the struggle, but also the glory of it! 🙂
So, what’s Xamarin?
Simply put, Xamarin is a framework that enables developers to write native Android / iOS Apps in pure C# code and sharing code across both platforms. C# developers can use their beloved Visual Studio IDE and write efficient code that compiles ahead-of-time to native ARM assembly code on iOS or to IL code which interacts with native types via JNI and is compiled just-in-time (when the app launches) to native assembly code on Android. So this means no hybrid apps, no web-apps, but real applications with the same performance and set of API features of Android / iOS – which is nice.
Xamarin for everyone!
Microsoft abandoned their Windows 10 Android Bridge project and instead bought Xamarin in February 2016. Before this point, Xamarin would cost roughly 1000$ per year which was presumed a high price by many developers – especially considering the known issues that Xamarin brought with it. Thus, many people would not even contemplate Xamarin for their cross platform development. Since Xamarin is now officially part of Microsoft, Xamarin (and also the Visual Studio 2015 Community edition) is free for students, open-source software development and small teams, so every C# enthusiast can start developing with Xamarin right away! 🙂
What has improved
The first-hand experience of developing with Xamarin quite differs from what you might expect and there are a lot of obstacles to overcome when developing for either Android or iOS. Every time there is an issue, an update would follow shortly, which would dig up some new issues. I’ve also experienced updates which even broke my Visual Studio projects, because the installer somehow didn’t update Xamarin properly and then rolled the setup back… which just completely uninstalled my previous version.
But still, a lot has been fixed/improved. Let me give you a few examples: Last year I had to write an additional resource loader to copy some assets at runtime on Android because they weren’t copied to the desired folder by default. This has been fixed in one of the updates. I’ve also had issues with Xamarins iOS binding tool called Objective Sharpie (version 2.1) and since it was an internal tool from Xamarin developers, which they made public, there wasn’t really any support. I still managed to generate the iOS bindings then. I’ve been using Objective Sharpie 3.0 for generating bindings for our Anyline SDK 3.5 and haven’t had any problems using it, so I can assume that it has improved. Also, the communication/pairing mechanism between Windows and Mac for compiling apps has been made easier and more stable. In the past, a seperate tool called Xamarin Build Host was necessary for generating an authentication code for the Windows PC and pairing them. Today, pairing PC and Mac in Xamarin is much simpler thanks to the Xamarin Mac Agent.
The “not-so-fun” part
Let me point out that developing with Xamarin isn’t just riding rainbows and unicorns. In reality, you will probably bump into quite a few obstacles on your way to create a stable and bug-free mobile app.
Building will strain your patience
A huge problem is the often extremely long compile / build time: The bigger the app is, the longer it will take to build. Especially on iOS, compiling might take several minutes because of the ahead-of-time compilation! If you change just a small piece of code and want to debug, you better go grab a coffee and hope the app started when you come back. Also better make sure that you have a stable and fast network connection when building your iOS app from a Windows PC.
Considering using Visual Studio for Xamarin.iOS, the somewhat hilarious part is: After building for debug, Visual Studio indicates that the app is about to start on your device / simulator and you’re waiting to switch to the debug view. But that might never happen and there’s no response or feedback in your IDE. Sometimes, it does work, but your breakpoints fail to bind or the app just crashes because you tried to get to your breakpoint. Which means: Clean your build, maybe clean your iOS cache, delete your bin / obj folders and do this aaaaall over again. Including that coffee. 😉
Mastering Memory Management
C# code is managed code. You’ve got your Garbage Collector (GC) that literally cleans up after you. This might give you the false impression that when coding in Xamarin with C# you don’t have to know your stuff about memory management in contrast to a native C++ developer. If you’re going to develop Android or iOS apps, you will be confronted with memory issues, whether you like it or not. You’re responsible for your resources and you need to know and act on when you don’t need them anymore. Otherwise, the GC will not free these memory chunks and instead try to allocate more and more space.
Mobile devices simply don’t have the amount of memory that desktop devices have – mobile apps have to be responsive and follow an asynchronous code structure, which means that you can’t just pressure the GC by allocating memory excessively. Whenever the GC has to allocate new heap space, it has to stop all running threads – including the UI thread, which may lead to halting animations and unlovely rendering. Even worse, your app could run out of memory eventually, leading to crashes or unexpected behaviour.
I recommend taking a look at this short, but pretty well-written article on Xamarin memory pitfalls, even though it’s targeted on Xamarin.iOS, these rules and practices also apply for Xamarin.Android. You should also take a look at this advanced topic on garbage collection in Xamarin.
That being said..
..Xamarin is an adventurous framework, and you can achieve great things with it – I really recommend Xamarin as choice of native mobile development. But as I mentioned in this article, it comes with some drawbacks and you’ll have to tackle the issues it involves. (But on the other hand, where wouldn’t you? 😉 ) Xamarin is definitely worth a try before jumping to conclusions.
QUESTIONS? LET US KNOW!