Xamarin, founded in 2011 by Miguel de Icaza and now owned by Microsoft, is an open source platform that allows developers to create native iOS, Android, and Windows UWP applications using the .NET framework and C#. With Xamarin, developers have a powerful tool that can be used to deliver cross platform applications from a single codebase. As with all tools, failure or success is based on knowledge of how best to use the tool and this applies to Xamarin. Xamarin is very powerful and requires some fundamental knowledge to maximize the value of its design goals.
Xamarin utilizes a number of items to create cross platform applications.
-
C# Language - Allows developers to use a modern language with advanced features
-
Mono .NET framework - A cross platform version of the .NET framework
-
Visual Studio for Windows and Mac - An advanced IDE that developers use to create, build, and deploy software
-
Compiler - Produces an executable for each of the target platforms
For developers currently utilizing the C# and the .NET framework, these items should be familiar.
To access platform-specific features, Xamarin exposes SDK’s via a familiar C# syntax. Here is a breakdown by platform:
iOS
|
Android
|
Windows
|
Xamarin.iOS exposes the CocoaTouch SDK as namespaces that can be referenced from C#
|
Xamarin.Android exposes the Android SDK as namespaces that can be referenced from C#
|
Windows UWP applications can only be built using Visual Studio for Windows selecting the corresponding project type. There are namespaces available that can be referenced from C#
|
Building cross platform applications with Xamarin follow the familiar process of code, compile, and deploy. During the compilation step for Xamarin, the C# code is converted into native applications for each target platform but the output is quite different. For iOS, the C# code is ahead-of-time (AOT) compiled to ARM assembly language. The .NET framework is included minus any unused classes. For Android, the C# code is compiled to Intermediate Language (IL) and packaged with the Mono runtime with Just In Time (JIT) compilation. As with the iOS scenario, the .NET framework is included minus any unused classes. The Android application runs side-by-side with Java/ART (Android runtime) and interacts with native types using JNI. (Java Native Interface) For Windows UWP, the C# code is compiled to Intermediate Language (IL) and executed by the built-in runtime. Windows UWP applications do not use Xamarin directly. Despite these differences, Xamarin provides a seamless experience for writing C# code that can be reused for all target platforms.
Visual Studio for Windows or Visual Studio for Mac can be used for Xamarin development. Which IDE you choose will determine which platforms you can target with your application. Here is a quick breakdown by IDE:
Visual Studio for Windows
|
Visual Studio for Mac
|
|
-
Xamarin.iOS
-
Xamarin.Android
|
If your plan is to target iOS, Android, and UWP, Visual Studio for Windows is your choice for IDE but you must have access to a Mac running macOS for build and licensing. Here is a link for more information on system requirements for Xamarin.
In order to deliver cross platform applications with Xamarin that inherently support code reuse, the application architecture is a key component to success. Following object oriented programming principles such as encapsulation, separation of concerns/responsibilities, and polymorphism can contribute positively to code reuse. Structuring the application in layers that focus on specific concerns (i.e. data layer, data access layer, business logic layer, user interface layer) and utilizing common design patterns such as Model-View-ViewModel (MVVM) and Model-View-Controller (MVC) also support code reuse. These concepts/patterns when used appropriately minimizes duplication and complexity, effectively organizes, and positions the code to be leveraged for multiple platforms.
Sharing code is also a key component to successfully using Xamarin to deliver cross platform applications. There are several options for sharing code between target platforms. Shared Projects, Portable Class Libraries (PCL), and .NET Standard Libraries are the options and each have unique features which are summarized below:
Shared Projects
|
Portable Class Libraries(PCL)
|
.NET Standard Libraries
|
Allows code to exist in a common location that can be shared between target platforms
|
Allows code to exist in a common location that can be shared between target platforms
|
Allows code to exist in a common location that can be shared between target platforms
|
Shared Projects can contain platform specific code/functionality
Compiler directives are used to include/exclude platform-specific functionality for each platform target
|
PCL’s cannot contain any platform-specific code
PCL’s have a profile that describes which features are supported (the broader the profile the smaller the number of available features)
|
.NET Standard Libraries cannot contain any platform-specific code
.NET Standard Libraries have a larger number of available features than PCL’s
.NET Standard Libraries have a uniform API for all .NET platforms supported by the version of the library
|
During the build process, the code in the shared project is included in each of the platform target assemblies (there is no output assembly for the shared project)
|
PCL’s are referenced by other projects and there is an output assembly after the build
|
.NET Standard Libraries are referenced by other projects and there is an output assembly after the build
|
More Shared Project information
|
More PCL information
|
More .NET Standard information
|
Microsoft released .NET Standard 2.0 in late 2017. In this release, the number of API’s supported increased by over 20,000 from the previous version (1.6) of the .NET Standard. This increase allows developers to place even more code into reusable libraries. The .NET Standard 2.0 is supported by the following .NET implementations:
As you can see by the list, if your goal is maximum code reuse and your platform targets are supported by the .NET Standard 2.0, a .NET Standard 2.0 Library is where you should place all of your reusable code. For Xamarin, having code reuse central to your software architecture design goal allows you to deliver your mobile application faster to multiple platforms.
When exploring Xamarin, make sure you spend adequate time learning and understanding these fundamental design and project organizational concepts. Understanding and using them are necessary to take full advantage of Xamarin as a tool to effectively and efficiently deliver cross platform applications.
Keep Right’in Code!
Richard