Microsoft's DLR and Mono bring Python and Ruby to Android

device

With my recent work on porting Mono to Android, I began needing a way to create Java classes and invoke their various methods from C#. I had read briefly about the Dynamic Language Runtime and upcoming C# support for dynamic types; so that naturally led to me considering implementing a JNI-P/Invoke interop layer that allows access to Java/Dalvik code through C# and vice versa. However, I wasn't sure if the DLR would even run on Mono, but I was pleasantly surprised. Support for the DLR was added to Mono around a year and a half ago!

After copying the necessary DLLs over to the phone, I was able to run the simple ipy.exe console application that comes with the DLR source code. The neat thing I discovered while perusing through the documentation is that IronRuby and IronPython are actually compiled into CIL byte code and thus eventually native code! Anyhow, as you can see, IronPython is working dandily on the phone. [1]

The Dynamic Language Runtime is probably the coolest new toy in the .NET arsenal since lambda expressions. Couple the DLR with the new dynamic keyword in C# 4.0, and you can invoke dynamic languages from C# as easily as you can access .NET classes from IronPython or IronRuby![0]

Now to get back to implementing Java interop...

 

[0] The dynamic keyword basically provides compile time parsing of an expression tree, but the expression isn't actually validated until runtime.

[1] Though Mono + DLR is clocking in at a hefty 13mb in files. But, applications will soon be installable onto the SD card so the storage constraints will be a moot point.

10 comments:

Jason D. Clinton said...

Excellent, excellent work. Looking forward to porting some Gnome Games to Android and this could make porting the game logic much easier. I hope we get a way to depend on your mono work so that every app. developer doesn't ship the same 13MB of runtime files.

Koush said...

Currently any application can access the mono executable and libraries (it is work read/execute).

I'm had planned on consolidating the entire runtime and the interop layer into a single package that other applications can use as a dependency.

Koush said...

I meant to say "world" read/execute.

Alfred said...

Does IKVM (http://www.ikvm.net/) help with your quest to interop between mono and davlik on Android?

IKVM can compile java bytecode into CLR bytecode.

Not sure if this helps - depends on your approach for interop.

Koush said...

I actually looked into it; it's a very cool project! Unfortunately, IKVM runs Java byte code, not Dalvik byte code.

And though there is a way to convert from Java to Dalvik, there is no way to convert from Dalvik to Java. And even if there were, it would be quite roundabout and inneffecient to do Dalvik->Java->CIL->Assembly. :)

Koush said...

But I am nearly done with the interop layer. Java can invoke methods on C# objects now...

Alfred said...

That's pretty cool.

With C# > Java interop, is it feasible to write a C# application that can use the Android java libraries, specifically the GUI ones?

Koush said...

Yes, it is.

However, with my current approach, inheriting from Java classes in C# would be klunky. For example, to create your own Activity class in C#, you'd need to to create a Java class that inherits from Activity and proxies all the overrideable methods to C# via the interop layer. Then on the C# end, you'd need a corresponding Activity proxy endpoint that a developer could inherit from...

This can all be automated through reflection and code generation though, so it may not be that bad. I'll look into this more when the interop is done.

Unknown said...

I looked for more recent posts on your blog tagged with "mono" and didn't find many... Did you ever get further with this? I'm fascinated by the idea that you could write an app in any CLR-targeting language and still make use of the Android app/UI stack.

Mageos said...

Do you have any plans to port Moonlight to Android?