XAMARIN vs. HMAD
Objectives of this chapter:
Xamarin is an interesting platform for mobile application development. The motive behind Xamarin is the same as that of hybrid mobile applications! Just as you define HTML5-based hybrid applications and after packaging deploy them on various platforms, Xamarin does a similar thing.
The basic difference is that with Xamarin, you code in C#.
Introduction to Managed Applications
Do you know the .NET or Mono .NET Framework? Have you ever coded in C#? Do you know the basics of XAML? Do you want to develop mobile applications that target multiple platforms?
If the answer to all these questions is yes, Xamarin is the platform for you! The Xamarin platform leverages the expertise of the C# language for business logic and XAML for the UI.
What is XAML? Extensible Application Markup Language (XAML) was introduced by Microsoft along with the Windows Presentation Foundation (WPF) framework around 10 years back. It is based on a container-content control relationship: each control can act as a container and can contain another control as content. Thus it makes 2D and 3D vector-based (with a z axis) UIs possible.
Using WPF, you can define a Windows OS–based desktop application with easy complex UI creation! Figures 12-1, 12-2, and 12-3 show the XAML UI, code, and output, respectively, for a simple WPF program.
Figure 12-1. XAML UI
Figure 12-2. XAML code-behind in C#
Figure 12-3. WPF code output
Similar XAML, with minute changes, can be used for developing Silverlight and Windows Phone applications. Silverlight is a Flash-like framework. Applications created using Silverlight work as in-browser or browser-based" applications. Making these applications work requires a plug-in or player installation as an extension on top of the browser.
How Does Xamarin Work?
Just like Java for Android and Objective C for iPhone, C# on .NET has been a development language and platform for Windows Phone development since the beginning. What is different with Xamarin is that it makes XAML and C# code work on iPhone and Android. In this section, you’ll learn how the XAML- and C#-based code work on different platforms. Let’s take a look at them one by one.
Using Xamarin for Windows Phone
Since C# and XAML are natively preferred by Windows Phone, you really don’t require Xamarin tools for Windows Phone applications. However, using Xamarin Studio for development makes it easy to write reusable code that can later be used for iPhone and Android development.
For Windows Phone, C# code is compiled into Microsoft Intermediate Language (MSIL). Later it is executed by the built-in runtime on Windows Phone.
It’s interesting to see how code works on Android and iPhone. The Microsoft .NET Framework provides many helper libraries. Such libraries are normally called Framework Class Libraries (FCLs). These libraries include helper classes to deal with the database, filesystem, network, security, and so on. References to such helper libraries are required when you start coding desktop or web-based applications using .NET. While working with Silverlight applications, certain limitations exist. For example, references to the database helper library are not allowed in Silverlight for security reasons.
To help programmers know which libraries are allowed and which are not allowed in a specific type of application, an editor such as Microsoft Visual Studio works on the concept of a framework profile. While a normal application is under development, Visual Studio allows programmers to work under the Full .NET Framework profile, which in turn allows programmers to refer to various helper libraries. While a Silverlight application is under development, Visual Studio allows programmers to work under the Silverlight profile, which in turn allows programmers to refer to specific helper libraries. In the same way, Xamarin Studio also works on the profile concept.
Xamarin Studio uses a Xamarin Mobile profile, which specific .NET libraries are allowed to reference, irrespective of the targeted vendor (such as Android or iPhone).
Xamarin offers two profile products: Xamarin.Android and Xamarin.iOS. Both packages are built on top of the Mono .NET Framework, an open source version of the .NET Framework.
Two libraries are offered by Xamarin in order to code against Android and iPhone. These libraries include Mono.Android.dll for Android and MonoTouch.dll for iPhone. Full lists of libraries for Android and iPhone are available at the following sites:
The Xamarin platform also helps programmers compile code and deliver mobile-platform-specific output such as APK for Android and APP for iPhone.
Figure 12-4 depicts the Xamarin architecture.
Figure 12-4. Xamarin working on Android
Xamarin-based Android applications execute on the Mono Runtime, which in turn executes parallel to the Android Runtime (ART). Both are based on the Linux kernel. Functionalities provided by the Android Runtime can be referred to in the Xamarin program by using .NET through an Android Callable Wrapper (ACW). ACW ultimately acts like a bridge between Java native APIs for Android and .NET.
If you want to override the native Java implementation using .NET code, you can use the Managed Callable Wrapper (MCW) given by the Xamarin .NET APIs.
Xamarin has an ahead-of-time (AOT) compiler that compiles Xamarin-based iOS applications into assembly code. Because the UI is a major concern for iPhone applications, Xamarin offers the Cocoa Touch SDK from Apple through wrappers in C#. Wrapper APIs make it easy for .NET developers to refer to Cocoa APIs without understanding their complexity.
To facilitate the development, testing, and publishing of iPhone applications, Xamarin also offers Xamarin Studio on iOS or Mac as well.
In all the platforms, Xamarin applications can leverage the existing runtime without worrying about handling memory allocation or garbage collection, as the Mono Framework–based runtime handles it implicitly.
How much does Xamarin cost? At the time of writing this book, the Xamarin cost for an individual is $25 per month. For businesses and enterprises, the cost varies from $999 to $1,899 per year.
Xamarin has special features, including the following:
When compared with hybrid application development using HTML5, Xamarin also has limitations. Table 12-1 compares HMAD and Xamarin development.
Table 12-1. Hybrid Applications: Xamarin-Based Development vs. HTML5-Based Development
Hybrid Mobile Application Development Using HTML5 |
Xamarin Development | |
---|---|---|
Language |
JavaScript/JS libraries such as jQuery. |
C#. |
UI challenges |
UI development uses HTML5. |
UI uses XAML only. |
Code reusability |
When used with a framework or platform such as AppBuilder, the UI and code are almost reusable. |
UI code is not sharable because of the norms on iPhone. |
App size |
Small compared to Xamarin. |
Larger, because of the libraries being packaged into the application. |
Availability of resources |
More resources in terms of man-hours are available from a web background. |
Fewer resources are available on the market. |
Learning curve |
For a developer familiar with the Web, the learning curve is less. |
XAML learning is little complex compared to HTML! |
Miscellaneous |
You can use various platforms and editors for code, including PhoneGap, AppBuilder, and Ionic. |
Only Xamarin/Visual Studio are available. |
Resource cleaning and memory cleaning is the job of the programmer and needs to be achieved through code only. |
Resource cleaning or memory cleaning is a job of the platform runtime. | |
Cost differs widely from packaging platform to platform. |
Cost is on the higher side when it comes to full Xamarin Studio. |
Summary
So what is good for you, as a developer: Xamarin or HTML5-based HMAD? Again you get the consultant’s answer: it depends! Now that you know about Xamarin, how it works, what it costs, and the editors available, you could easily find Xamarin attractive. To make your decision, you have to consider the following:
Finally, to conclude: no platform is bad, and each has its own benefits.
18.188.218.226