Understanding Xamarin Android - Build Native Android App
Xamarin.Android is a part of Xamarin family to build a 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 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.
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 components, 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.
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 has been written to run multiple vms (virtual machines) on low-memory devices.
In order to execute app within an 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.
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.
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.
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:
Ahead-of-Time (AOT) compilation, which improves speed and reduces memory footprint
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 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 the Android operating system either by calling .NET APIs or using the classes exposed in the Android namespaces which provide 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 wrapper 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 is 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 allows you to build a native Android app using C#. A Xamarin Android project is based on the standard Android project structure as you create 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 an understanding of 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. Lets us know if you would like to know more about Xamarin training program.