Wednesday, November 30, 2011

Android - Create apps in Visual Studio / C# with Mono

It's been in an invitation-only beta for a while, but now you can download Mono for Android and develop Android apps in C# and Visual Studio 2010.
From the web site:
What is Mono for 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.
Mon requires:
  • Java JDK
  • Android SDK
  • Visual Studio 2010 (Professional or better) or MonoDevelop
  • The Mono for Android SDK
You should already have the first two if you're developing for Android, so go to http://mono-android.net/ and download the evaluation version. The install process is very simple - just click to execute and let it run.
Once it's finished installing, fire up VS2010 and you should see a new project type: Mono for Android Application. Creating an empty project gives you a solution with a directory structure similar to what you'd get in Eclipse - Assets and Resources folders, and an activity file called Activity1.cs.
The Mono site has several tutorials that are almost identical to those on android.com, so let's take a look at the Gallery tutorial:
First, you'll notice that the resource file is the same on both systems.
Now, look at the OnCreate method. Here's the C# version:
protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    // Set our view from the "main" layout resource
    SetContentView (Resource.Layout.Main);
    Gallery gallery = (Gallery) FindViewById<Gallery>(Resource.Id.gallery);
    gallery.Adapter = new ImageAdapter (this);
    gallery.ItemClick += delegate (object sender, ItemEventArgs args) {
        Toast.MakeText (this, args.Position.ToString (), ToastLength.Short).Show ();
    };
}
And here's Java:
@Override
public void onCreate(Bundle savedInstanceState) {
    
super.onCreate(savedInstanceState);
     setContentView
(R.layout.main);

    
Gallery g = (Gallery) findViewById(R.id.gallery);
     g
.setAdapter(new ImageAdapter(this));

     g
.setOnItemClickListener(new OnItemClickListener() {
        
public void onItemClick(AdapterView parent, View v, int position, long id) {
             Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
        
}
     
});
}
Pretty much the same...
So, if you're a C# developer who really doesn't want to mess with learning Java, this might be the way to go. The drawback is cost... First, you'll need a Visual Studio Professional license (in the neighborhood of $700), although you could try using MonoDevelop (free). The real expense is in Mono itself. Even if you only want the Professional version (which is unable to do large-scale deployments to 100 or more devices) you're looking at $399. There's a $99 Student version, but it can't be used commercially and can't deploy to any market. Also, that license only entitles you to a year of updates of Mono.
Personally, as much as I'd like to recycle some existing C# code, I don't think this IDE would ever be worth $399 to me as a hobbyist developer.

No comments:

Post a Comment

thank you