Mono/Dalvik Interop

I've been working away at getting Mono into a usable state on Android, and finally have something to show for it. I (mostly) completed the interop layer that allows code invocation of Mono from Dalvik, and vice versa. As a proof of concept test, I have my test verify reentrancy (i.e., you can invoke Mono method from Dalvik, which in turn invokes a Dalvik method from Mono).

Java code that calls into Mono:

String ret = (String)MonoBridge.invoke("MonoDalvikBridge.DalvikBridge, MonoDalvikBridge", String.class, "Echo", "hello");
Log.i("MonoActivity", ret);

And here's the C# method that was called. In turn, it calls back into Java:

public static string Echo(string echo)
string ret = Invoke<string>("com.koushikdutta.monodalvikbridge.MonoBridge", "echo", echo);
return echo;

And finally, this is the Java method that was called from C#:

public static String echo(String str)
return str;

As shown, passing and returning "primitive" types between both runtimes works seamlessly. Passing complex types and calling methods on those objects works as well, by way of a runtime wrapper is created for it (MonoObject/DalvikObject).

The basic syntax for invoking a method from one runtime into another is:

Invoke(className | runtimeObject, methodName, returnType, params object[] arguments)

That said, I'm pretty excited for when Mono supports the upcoming C# "dynamic" keyword. That will make interop (at least from Mono to Dalvik) quite trivial.

There's still a bit of clean up and convenience functions I need to implement, but the majority of this is complete. The code is all open source, for those that are curious.


Anonymous said...

Hello Koush!Let me congratulate you for this blog.It is very interesting for an Android newbie as me.The Mono/Dalvik interop looks great.I have downloaded the android mono project from svn and i would like to ask to you what should I do to test the example you've put into this blog (Java/C# code).Basically the steps for compile,building,etc...
Thank you!!

Huw said...


The google code page has access restrictions. Is this intentional?