KBeanie

How does Xamarin work?

Cross-platform development frameworks for mobile are still interesting topics of discussions among mobile developers. Xamarin is one of the popular cross-platform frameworks from Microsoft. With Xamarin, you can develop mobile applications for iOS, Android and Windows platform with a single codebase written in C#. You can also develop desktop applications for MacOS and Windows. And it’s not hybrid. Xamarin based apps are compiled native applications that have near-native performance, even for gaming applications.

Sounds promising, isn’t it? Well, before we arrive at any conclusions, let’s understand how Xamarin really works.

How Xamarin works?

Xamarin allows developers to develop native applications for Android, iOS and Windows Phone platforms, with a single codebase, i.e. C# and a single IDE, i.e. Visual Studio or Xamarin Studio. Thus, a single developer can actually develop native mobile applications without knowing Java, Kotlin, Objective-C or Swift. What that means is, all the C# code has to be converted to make it working on these three separate platforms.

Magic? Well, yes. Xamarin takes care of translating or compiling all your C# code to its corresponding platform-specific code.

Xamarin promises this:

Anything you can do in Objective-C, Swift and Java, you can do in C#.

  • Xamarin provides native UI interface.
  • Xamarin provides native API access.
  • Xamarin provides native performance.

The next obvious question: How exactly Xamarin achieves this?

Xamarin and iOS

For iOS, Xamarin provides a fully compiled (AOT – Ahead Of Time) binary that directly runs on your device to provide native performance. Xamarin.iOS exposes a C#/CIL binding to the Cocoa Touch API and also provides access to ECMA CIL APIs and various other .NET APIs.

Xamarin.iOS runs within the Mono execution environment and uses full AOT compilation to compile C# code to assembly language. This runs side-by-side with the Objective-C runtime. Both runtime environments run on top of a UNIX-like kernel, specifically XNU, and expose various APIs to the user code allowing developers to access the underlying native or managed system.

Xamarin.iOS instead uses an Ahead of Time (AOT) compiler to compile the managed code. This produces a native iOS binary, optionally optimized with LLVM for devices, that can be deployed on Apple’s ARM-based processor.

Xamarin and Android

For Android, Xamarin leverages the JIT(Just In Time) compilation to create an optimized executable. Xamarin.Android applications run within the Mono execution environment. This execution environment runs side-by-side with the Android Run Time(ART) virtual machine. Both runtime environments run on top of the Linux kernel and expose APIs to the code that allows access to the underlying system. Xamarin.Android applications also contain the Android Callable Wrappers(ACW) to allow Android to call into managed code. Managed Callable Wrappers(MCW) are used whenever managed code needs to call into Android APIs.

Approaches for development

There are 2 ways or approaches to develop using the Xamarin framework.

Shared business logic

You can write all your business logic with C#. You can then create your UI specific to the platforms to take advantage of the unique features that the platforms expose using native controls and design patterns. You would use Xamarin.iOS and Xamarin.Android for this approach. Still, everything is written in C#.

About 70% of the code you write for an application is the business logic. The business logic of the application can be written just once and is shared between all the platforms, thus saving you a lot of effort and time.

Xamarin.iOS and Xamarin.Android has 100% converage(all the features available through Swift on iOS and Java on Android) and some added useful .NET APIs as well.

Shared business logic and shared UI

In this approach, you would use Xamarin.Forms to also define the UI of your mobile application in addition to the business logic. The UI definitions are used to create corresponding native UI controls in each platform.

Leave a Reply

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