5

I am creating a fresh Mono for android application using http://xamarin.com/monoforandroid enter image description here

I selected Mono for Android Application using Visual C# option.

My Android target version is : enter image description here

I went to references of the project and tried adding my pre existing dlls (made in .NET 4 Framework) and I got this error:

enter image description here

The strange stuff is there is no option to change the .NET Framework from project properties. Can you tell me what I am missing?

Rocky Singh
  • 15,128
  • 29
  • 99
  • 146

2 Answers2

5

The problem here is that you're trying to reference a .NET 4 DLL from a project that isn't .NET 4. Mono for Android uses its own profile for .NET, which is very similar to that of Silverlight (but not exactly the same). While it's possible that adding a DLL compiled for a different profile will work, it's very risky as you will probably run into problems at runtime where your app will crash, due to something being missing from the Mono for Android profile.

The best answer right now is to create a Mono for Android class library, link in the appropriate files from the library you want to use, and reference that. This will give you compile-time checking that you're not using anything unsupported by the Mono for Android profile, and help keep you more sane in the long run. I have an old blog post up here that talks about how to link files across projects.

That said, in this case you're in luck because someone else has already done that last part for you! Check out this fork of Json.NET which provides versions for MonoTouch and Mono for Android.

Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
  • Even if I try to use source files (of my preexisting dlls) how could I do since those files uses the .NET 4 Framework features. How can I use those source files in that Mono for Android class library project? – Rocky Singh May 11 '12 at 13:30
  • Let's say I have a class public class Class1 { public static dynamic A { get; set; } }. How can I use dynamic keyword here without .NET 4 version? – Rocky Singh May 11 '12 at 13:32
  • Dynamic support was added in the current alpha version of Mono for Android: http://docs.xamarin.com/android/Releases/Mono_For_Android_4/Mono_for_Android_4.1.0#Dynamic_support – Greg Shackles May 11 '12 at 13:37
  • And to answer your first question there, my point is that you can't simply rely on every .NET feature being available in Mono for Android (though it often will be) since it's a different platform. Pulling the source files into a MfA class library allows you to make sure that your code is supported by that profile. – Greg Shackles May 11 '12 at 13:43
  • But that is for Android 4+. I was asking for Android 2.3 version supporting .NET framework 4 version. There is large majority of users using Android 2 version. We can't shift to Android 4 simply. I was wishing that product to support existing .NET 4 API assemblies (which I will use to call API methods or remote database calls) to work with new android applications – Rocky Singh May 11 '12 at 13:45
  • No, that's for Mono for Android 4.1.x (the alpha series for what will be 4.2), which has no real relation to Android 4 :) – Greg Shackles May 11 '12 at 13:47
  • Hmm.. So you mean to say I still have to copy the source code of all the existing dlls to Android class project and then I have to rebuild it and make assemblies and then use them in android application projects. Don't you think this is very tedious way of using the existing dlls? – Rocky Singh May 11 '12 at 14:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11155/discussion-between-greg-shackles-and-rocky-singh) – Greg Shackles May 11 '12 at 15:37
  • As Greg said back in 2011, create a new MfA project with links to the existing files. This is discussed in [Cross-Platform Development With Mono for Android](http://visualstudiomagazine.com/articles/2012/05/30/cross-platform-mono-for-android.aspx). Creating links is described [here](http://support.microsoft.com/kb/306234). – Andy Johnson Sep 19 '12 at 10:11
2

The strange stuff is there is no option to change the .NET Framework from project properties. Can you tell me what I am missing?

It's not particularly strange - that dialog box was written by Microsoft, with its own project types in mind. It sounds like Mono for Android doesn't have the same options available.

I strongly suspect you'll just have to use a version of JSON.NET targeting .NET 3.5.

(Having said that, Mono for Android claims to support "dynamic language features" which sounds like it should be targeting .NET 4. Odd. I suspect the fix is the same though.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Jon, Can you give me some pointers on how to change target framework for mono for android project? Since I have many dlls targeted to .NET 4 – Rocky Singh May 11 '12 at 12:53
  • @RockySingh: I suspect if there's no option, then it can't be done - at a guess. You may find that a bleeding-edge version of Mono for Android supports it, of course. – Jon Skeet May 11 '12 at 12:57
  • You could can place source file in a shared position. Then create an Android DLL and drag source to the project. MonoDevelop will ask you how to manage the file, choose to link the file to the project. In these way you could reuse sources with different build configurations (e.g. another one could be for Silverlight or MonoTouch). – gsscoder May 23 '12 at 09:48