Introducing dot42, a C# Compiler for Android’s Dalvik Runtime
The biggest downside of Mono for Android is that you are building on Mono, a completely separate runtime that what Android expects. And while there are certainly some advantages to having access to a full CLR, marshalling calls between it and Android’s Dalvik runtime can be expensive. So why not skip the IL code and emit Dex code directly?
Actually that is a slight exaggeration. The dot42 compiler doesn’t actually skip the IL. Rather, it reads the IL code and converts it into a new language called RL or Register Language. The main difference between IL and RL is that IL is stack-based (somewhat like Java’s byte code) while RL is register based. Once converted into RL, a set of optimizations are performed and the final product is compiled again. This last compilation gives us dex code, the language of Android’s Dalvik Runtime.
Through each step of the process the original .NET debugging information is maintained, though of course it must be converted from the Windows/.NET PDB format to dex’s debug information format. dot42 uses the Android Debug Bridge to connect the Visual Studio debugging facilities
Android Libraries are accessed by directly referencing JAR files. “After compilation, your calls are redirected to calls into the original Android libraries.” Some mapping between types is needed, but that is largely addressed by the compiler. For example, java.lang.Boolean is mapped to C#’s Nullable<bool>.
Because dot42 emits dex code, there are some C# features that you will not be able to use. The most notable of these is user defined value types, a.k.a. structs. Since they don’t exist in Dalvik, there is simply no way to map them.
When using dot42, you’ll often need to decide between the C# and Android versions of common data structures. For example, should you use .NET’s List<T> or Java’s ArrayList<T>? According to Ewout Prangsma, developers should favor the Java version if they are concerned about memory and download size. Even though their implementation of List<T> is basically a thin wrapper around ArrayList<T>, it is still extra code that needs to be packaged and shipped to the phone.
dot42 is a commercial product that starts at 499 Euro per developer with the usual bulk licensing options.