Native libraries in MonoDroid library projects

Currently, MonoDroid (Mono for Android) doesn’t support native libraries (or any other Android resource type) in library projects (bug report). Fortunately, they support normal .NET assembly resources. This led me to write a workaround for this missing feature.

The Class

Here’s the class that implements to loader.



  • 2012-07-18: Updates documentation and added some logging
  • 2012-07-11: Initial revision

How to use

To use this class, you first need to create a directory structure for the native libraries, like this:

+- NativeLibs
   +- armeabi-v7a
   +- armeabi
   +- other ABIs you want to support

Now place the native libraries in the apropriate directories, for example:

+- NativeLibs
   +- armeabi-v7a
   |  +-
   +- armeabi

Then select “Embedded Resource” as Build Action in the file’s properties (Hotkey: F4).

Selecting "Embedded Resource" as "Build Action"

Now use one of the Load() methods to load the library. For example, if you have a type called MyType in the project’s default namespace (specified in the project settings), you can use this call to load a library called

MonoDroidLibraryLoader.Load("sqlite", typeof(MyType));

Note: If you renamed the directory “NativeLibs” from above, you can specify it as third parameter for Load().

Accessing Java Classes from Mono for Android via JNI

Mono for Android let’s you write Android applications in a .NET language (for example, C#). It comes with wrappers for almost the entire Android API so building “standard” apps is easy enough. However, sometimes you may need to work with third party Java classes that aren’t part of Android itself. These classes usually come as a .jar file. Fortunately, Mono for Android provides a way to access those Java classes. Unfortunately, this way is a “little” bit tricky and documentation on this is (currently) quite limited.

This article will show some basic example of how to call a method of a Java class from Mono for Android. We will use Visual Studio 2010 in this article, as it is (in my opinion) currently superior to Mono Develop.

Update: Mono for Android v4.2 and higher provides an automatic way for doing the things described in this article. This is a lot easier than the manual way and thus the preferred way.

Read more →

Bug of the Day: Mono for Android

Today’s bug is again sponsored by Mono for Android:

Random “The type or namespace name ‘XXX’ could not be found” errors when building

Mono for Android randomly produces “The type or namespace name ‘XXX’ could not be found” errors when building a solution. These errors don’t really exist and disappear after a rebuild.

Bug of the Day: Fast Deployment

Today, I’ve been hunting down a bug in Mono for Android (4.4.55). This bug flooded the Android log with this entry (thousands of times):

02-04 17:09:32.010: W/(4300): Bad call to mono_mutex_lock result 11
02-04 17:09:32.010: A/(4300): * Assertion at /Users/builder/data/lanes/monodroid-mac-monodroid-4.4-series/6418373f/source/mono/mono/metadata/loader.c:2181, condition `ret == 0' not met

This flood of log messages made the whole deployment process very slow (54 seconds to deploy a fresh project).

As workaround I found that disabling Use Fast Deployment in the project settings “solved” the problem for me.

I’m still waiting for an answer from Xamarin on this issue. Update: This problem is tracked here.