Skip to main content

Android meets .NET !!!

It is always fun for me to program in C# (besides C++). If so, how would I feel if I was able to program for Android in C#? You may be wondering what in the world I am talking about. Android development environment is all Java and open source stuff. How could this Microsoft thing fit onto it?

Well, it seems that some clever guys had huddled up and ported Mono for Android, developed .NET libraries for the Android SDK, and also supplemented it with a 'Mono for Android' project template in Visual Studio; and called it mono for android. Thus we end up writing C# code happily for Android.

After a bunch of installations, fire up your Visual Studio (2010) and you should be seeing a new project template 'Mono for Android' under C#.


Create a new 'Mono for Android' project, which by default comes with an activity - C# code.

Modified the orginal code to try starting a new activity from the default one...

The project layout for most of the part is similar to the native Android project. The layout resources files are named *.axml, and the strings.xml is really clean with named string constants with the ugly behind the scenes of ids and stuff in the Resource.designer.cs. The way to code is straight forward and needs no special explanation. A good to see thing for me was .NET coding style.

A similar set of addins and plugins have been developed for MonoDevelop too; although I did not try that.

It is really a wondrous thing to know, program and relish such an attempt. However, there are sad curves in the story:-
  • Mono for Android is available only for Windows (and Mac). It is not available for Linux.
  • The installation of the application from Visual Studio and application response for the few clicks is pig slow. It is not Visual Studio's fault. It seems Mono takes quite a bit of time for JITting. After each part of the code is JITed, the application response for that part is fairly ok, but not as good as Android's native\custom VM Dalvik.
  • The trial version does not allow deploying on the device. You can play around and deploy only on the emulator. If you want to use it on the device, it costs. Yes, it costs somewhere between $400-2500, depending on the licesing and support. Pretty expensive!
Try it out (the trial one or the full version if you are rich), at least for fun!

Comments

Rajagopal said…
Good article.

I think Mono For Android is a better option for learning, but if we are to go with real time project and later deploying on a device, Java is the better option, though we may not enjoy the luxuries as in VS and C#.

We have a project running here, which is planned to move to the Android platform in near future. Some folks here argue to go with Mono, but I feel down the road we might realize support will be limited. Mono claims they support Silverlight in Linux using Moonlight, but it has many pitfalls.
I did a study on porting our current .Net application to Android. Following is the summary.

.Net Support using Mono Android

Mono for Android enables developers to use Microsoft Visual Studio to create C# and .NET based applications that run on Android phones and tablets.


Developers can use their existing skills and reuse code and libraries that have been built with .NET, while taking advantage of native Android APIs.
Mono does not support Windows Form/XAML/Silverlight or Visual Basic.
To reuse existing .NET code with Mono for Android, you must recompile your libraries with Mono for Android base assemblies.
Mono for Android will support the C# language, garbage collection, multi-threading and many features of the core of .NET 3.5 including C# 3.0 and LINQ
The following assemblies are part of Mono for Android 1.0:
· mscorlib.dll

· System.dll

· System.Core.dll

· System.Json.dll

· System.Runtime.Serialization.dll

· System.ServiceModel.dll

· System.ServiceModel.Web.dll

· System.Web.Services.dll

· System.Xml.dll

· System.Xml.Linq.dll

In addition, the following Mono assemblies are part of Mono for Android 1.0:
· Mono.Security.dll

· Mono.Data.Sqlite.dll

· Mono.Data.Tds.dll

· Mono.Android.dll

And the following third party assemblies are part of Mono for Android 1.0:
· OpenTK.dll



Database Support:
SQLite is supported as Database which is serverless. XML is also supported.

Installation of MS Access & SQL Server is not possible. So MDF & MDB files are not accessible. However connecting Remote SQL Server database is possible using Web services.

Communications:
It supports wireless communications using GSM mobile-phone technology, 3G, Edge, 802.11 Wi-Fi networks.
Unknown said…
One should expect only the development platform and the runtime to be available in Android or any others. WinForms is native to Windows. Porting to Linux makes sense while porting that to Android does not since the underlying technique of window management is completely different.

I am not sure if we can take the road of Mono for Android. There may not be issues support wise but the performance of the application seems not to be proven.

Not all portings work well. For instance, there is Firefox and Opera for Android. Both suck. They may not be ported but rewritten. Not sure!

If someone has already treaded that road and made success, it would be safe for us to choose. Or if we are to choose, it should not be a critical project on which the company bets.

But it is really exciting to know about such attempts. And who knows it may turn out to be a big success one day.
Hi guys..I like your blog information.This is one of the well defined post. Thanks for your support.
garryfernandis said…
I think Nowadays rather than using Java applications Android applications are in great demand in market.
So Its great article to read here and with the help of screenshots it makes us better understand.build an iphone app
darichkid said…
Here is a database compatible with MonoDroid:
http://www.kellermansoftware.com/p-43-ninja-net-database-pro.aspx
Programmer can use their current skills and recycling program code and collections that have been built with .NET, while using local Android operating system APIs.

Popular posts from this blog

Implementing COM OutOfProc Servers in C# .NET !!!

Had to implement our COM OOP Server project in .NET, and I found this solution from the internet after a great deal of search, but unfortunately the whole idea was ruled out, and we wrapped it as a .NET assembly. This is worth knowing. Step 1: Implement IClassFactory in a class in .NET. Use the following definition for IClassFactory. namespace COM { static class Guids { public const string IClassFactory = "00000001-0000-0000-C000-000000000046"; public const string IUnknown = "00000000-0000-0000-C000-000000000046"; } /// /// IClassFactory declaration /// [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid(COM.Guids.IClassFactory)] internal interface IClassFactory { [PreserveSig] int CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject); [PreserveSig] int LockServer(bool fLock); } } Step 2: [DllImport("ole32.dll")] private static extern int CoR

Extension Methods - A Polished C++ Feature !!!

Extension Method is an excellent feature in C# 3.0. It is a mechanism by which new methods can be exposed from an existing type (interface or class) without directly adding the method to the type. Why do we need extension methods anyway ? Ok, that is the big story of lamba and LINQ. But from a conceptual standpoint, the extension methods establish a mechanism to extend the public interface of a type. The compiler is smart enough to make the method a part of the public interface of the type. Yeah, that is what it does, and the intellisense is very cool in making us believe that. It is cleaner and easier (for the library developers and for us programmers even) to add extra functionality (methods) not provided in the type. That is the intent. And we know that was exercised extravagantly in LINQ. The IEnumerable was extended with a whole lot set of methods to aid the LINQ design. Remember the Where, Select etc methods on IEnumerable. An example code snippet is worth a thousand

sizeof vs Marshal.SizeOf !!!

There are two facilities in C# to determine the size of a type - sizeof operator and Marshal.SizeOf method. Let me discuss what they offer and how they differ. Pardon me if I happen to ramble a bit. Before we settle the difference between sizeof and Marshal.SizeOf , let us discuss why would we want to compute the size of a variable or type. Other than academic, one typical reason to know the size of a type (in a production code) would be allocate memory for an array of items; typically done while using malloc . Unlike in C++ (or unmanaged world), computing the size of a type definitely has no such use in C# (managed world). Within the managed application, size does not matter; since there are types provided by the CLR for creating\managing fixed size and variable size (typed) arrays. And as per MSDN, the size cannot be computed accurately. Does that mean we don't need to compute the size of a type at all when working in the CLR world? Obviously no, else I would