Wednesday, 23 May 2018

Android vs Xamarin droid

Xamarin produces native cross-platform solutions written in c#, with "real" native User interfaces, it can result in anywhere between 30% to 75% cross platform code, depending on how UI vs Business logic (BL) heavy you're application is; whereas for an enterprise application that leverages Xamarin forms as well as a shared project or a Portable Class Library (PCL) you can potentially reach 95% cross-platform code.

But how does it work? Xamarin provides c# wrappers for the underlying java utility libraries that include things like data structures, Networking, and so on, there are also c# wrappers for Android specific api's, things like data storage, device features such as: gps, camera, phone and so on. Xamarin apps also have access to the Mono.net library that contains most of the .net types.


Since Xamarin leverages the Java SDK we need to have the JDK installed to compile Xamarin android applications as well as the Android SDK.

If we where developing an Android application using Eclipse, what we'd do is start with java code, we'd then use a java compiler to get java bytecode, then that javabytcode would be complied into Android Dalvik bytcode using the android dex compiler. The dex code is then packaged with the application resources such as images, sounds, etc into an application package or (.apk) file which is just really a zip, Once the package file is installed onto the Android device the Android runtime translates the dex file into native code; each of these native files then run in their own process with there their own copy of ART on the device.


Now in xamarin we follow a very similar workflow, where we write our c# code, then we compile it into IL, which sort of looks like a variant of c. That IL code is JIT complied on the device using the MonoRuntime, which is packaged into our application package (.apk) file along with any resources and .dex files.

our andriod application then executes both the Mono runtime that JIT compiles our IL code and the Android runtime that executes the native dex bytecode, Both the Mono runtime  and ART interact with the underlying device.




keep in mind that in our xamarin application we can include Java.jar files that don't have wrappers, there are two ways to accomplish this:
  • Java Native Interface (JNI): Use Reflection
  • Java Bindings Library: 
Neither technique is simple but either can be used.