Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Xamarin Android - Build Native Android App

 Print 
  Author : Shailendra Chauhan
Posted On : 20 Feb 2017
Total Views : 3,538   
 

Xamarin.Android is a part of Xamarin family to build native Android app with C# and Xamarin. Xamarin.Android provides the same UI controls as you have in Android with Java.

In this article you will learn about the Android architecture, Xamarin.Android architecture and tools to develop Xamarin.Android apps. Before going to start Xamarin.Android, let's understand Android architecture and development tools with Java.

Android Development

Android is an open source operating system based on Linux kernel. Android was originally developed by Android Inc. and subsequently purchased by Google. Android provides a rich application framework that allows you to build innovative apps and games for mobile devices in a Java language environment.

The native Android apps development can be done using Android Studio or Eclipse using Java as a programming language.

Android Platform Architecture

Android operating system is a Linux-based stack of software components which can be divided mainly into five sections as shown below in the following diagram.

Linux Kernel

The Linux kernel provides a multitasking execution environment which allows multiple processes to execute concurrently. The Android Runtime (ART) relies on the Linux kernel for underlying functionalities such as threading and low-level memory management etc.

Hardware Abstraction Layer (HAL)

The hardware abstraction layer (HAL) allows the Android application/framework to communicate with the hardware specific device drivers with the help of higher-level Java API. The Android HAL contains multiple library modules for a specific type of hardware component, such as the camera or bluetooth module. So, when you make a call to access device hardware using Java API, the Android system loads the library module for that hardware component.

Android Runtime

Each android app based on Android version 5.0 (API level 21) or higher, runs in its own process and with its own instance of the Android Runtime (ART). ART hase been written to run multiple vms (virtual machines) on low-memory devices.

In order to execute app within a ART, the app code must be transformed from standard Java class files to the Dalvik executable (.dex) format, that's optimized for minimal memory (Consume 50% less memory) footprint than standard Java bytecode. Build toolchains, such as Jack, compile Java sources into DEX bytecode, which can run on the Android platform.

Note

  1. Prior to Android version 5.0 (API level 21), Android app runs under Dalvik runtime. If your app runs well on ART, then it should work on Dalvik as well, but the reverse may not be true.

  2. Dalvik is a JIT'ing VM which compiles app code to native code on-the-fly. The downside to JIT is that the JIT compiler runs while you are using your app whic add latency and memory cost.

  3. AOT is like JIT where app code is compiled to native code once at the app installation time. What is stored on your phone and run is effectively native, not bytecode.

ART has two main features compared to Dalvik:

  1. Ahead-of-Time (AOT) compilation, which improves speed and reduces memory footprint

  2. Optimized garbage collection (GC)

Native C/C++ Libraries

C/C++ libraries provdes a wider range of functions including 2D and 3D graphics drawing, Secure Sockets Layer (SSL) communication, SQLite database management, media management like audio and video playback, display subsystem and graphic layer management and an implementation of the standard C system library (libc).

The Android app can access these libraries through the Java based Android core library APIs. Also, you can access these native C/C++ libraries using the Android Native Development Kit (NDK) within Java code using the Java Native Interface (JNI).

Java API Framework

The Java API Framework is a set of rich and extensible APIs to access entire feature-set of the Android OS using Java language. These APIs are helpful to interact Native C/C++ libraries, build rich UI and for various task management like Activity management, notification management, resource management etc.

System Apps and Third Party Apps

Android OS comes with a set of system apps for emails, phone calls, messages, calendars, internet browsing, contacts, and much more. Apart from system apps, you can also install third-party for emails, chats, messages like outlook, facebook, twitter etc.

Xamarin.Android Architecture

Xamarin.Android apps run within the Mono execution environment. The Mono runtime is written in the C language and runs side-by-side with the Android Runtime (ART) virtual machine. Both runtime environments run on top of the Linux kernel and expose various APIs to access the underlying android system.

In Xamarin.Android apps you can access the various low-level features of Android operating system either by calling .NET APIs or using the classes exposed in the Android namespaces which provides a bridge to the Java APIs that are exposed by the Android Runtime.

Managed Callable Wrappers (MCW)

The Android OS facilities like Audio, Graphics, OpenGL and Telephony are not available directly to native apps. They are only exposed through the Android Runtime Java APIs residing in one of the Java.* namespaces or the Android.* namespaces.

So, to access these APIs in Xamarin.Android, a managed callable wrappers is required which acts as a JNI (Java Native Interface) bridge to invoke Android code and provide support for overriding virtual methods and implementing Java interfaces.

Android Callable Wrappers (ACW)

Android callable wrappers are a JNI bridge which are used to invoke managed code when the Android runtime needs. When an Android Callable Wrapper (ACW) is created from Java, the ACW constructor will cause the corresponding C# constructor to be invoked.

Xamarin.Android Development

Xamarin.Android allows you to build native Android app using C#. A Xamarin Android project is based on the standard Android project structure as you creates with Android Studio or eclipse. Xamarin.Android development you can do using IDEs - Xamarin Studio or Visual Studio for Mac and Visual Studio for Windows.

What do you think?

I hope, now you have understanding about Xamarin.Android to build native Android mobile apps. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS
14 OCT
Angular2 and Angular4 (Online)
03:00 PM-05:00 PM IST (+5.30GMT)
12 OCT
ASP.NET Core (Online)
09:00 PM - 11:00 PM IST(+5:30 GMT)
10 OCT
Microsoft Azure Infrastructure Solutions (Online)
08:00 AM-09:30 AM IST / 09:30 PM -11:00 PM CST
30 SEP
Angular2 and Angular4 (Classroom)
08:30 AM-11:30 AM IST
20 SEP
MEAN Stack (Online)
07:00 AM-09:00 AM IST
20 SEP
ASP.NET MVC with Angular4 (Online)
9:00PM- 11:00PM IST(+5:30GMT)
16 SEP
Angular2 and Angular4 (Online)
08:00 AM-10:00 AM IST(+5.30 GMT)
22 AUG
ASP.NET Core with Angular4 (Online)
07:00 AM - 9:00 AM IST(+5:30 GMT)
LIKE US ON FACEBOOK
 
+