[Mono-dev] Tweaks needed to get Mono compiling in the Android build environment

Koushik K. Dutta koush at koushikdutta.com
Sun Jan 18 03:13:33 EST 2009

Hi again all, I have gotten Mono cross compiling within the Android build environment successfully. The good part about this is that it is now properly using Android's linker and libc (Bionic); ie, I am no longer using glibc or ld-linux.so.3. So that reduced the memory footprint, increased the speed (Bionic uses Thumb code), and also allows interop with Android's shared libraries (which didn't work before due to a different linker script being used that caused incompatibility).

I've attached a patch of what is needed to change in Mono code to get this working. Brief summary:

1.       gcconfig.h: Android's Mono is not using glibc anymore, so the GC needs to search for the data start segment when PLATFORM_ANDROID is defined.

2.       io.c: Although Android has statfs, there are some missing functions and defines that prevent GetDiskSpaceFreeEx from compiling. I used the PLATFORM_ANDROID define again to make it fall back to the simpler implementation.

3.       attach.c: The HAVE_GETPWUID_R define should be checked and the proper function used (getpwuid in Android's case).

4.       socket-io.c: Android has an incomplete ipv6 implementation, so I needed to undef AF_INET6 to prevent compilation errors.

5.       mini-arm.c: Android's toolchain does not implement __clear_cache, so I needed to implement it properly for Mono to function. I have opened a bug on the Android team about this problem: http://code.google.com/p/android/issues/detail?id=1803. However, they tend to be fairly unresponsive...

6.       mono-mmap.c: The HAVE_SHM_OPEN define should be checked as well.

Creating a build for Android does not use the usual configure/make process anymore. Instead, Mono must be an external project under your local Android source tree, and cross compiled in that environment. To view the rest of the changes (mostly tweaks and filling holes in libc) and build instructions, you can go to http://code.google.com/p/androidmono/.

I'm contributing the both the internal and external changes to Mono under the MIT/X11 license. (Please disregard my previous patch, as this change supersedes that)


Koushik Dutta

From: Rodrigo Kumpera [mailto:kumpera at gmail.com]
Sent: Wednesday, January 14, 2009 4:18 AM
To: Koushik K. Dutta
Subject: Re: [Mono-dev] Tweaks needed in security.c for running Mono on Android - patch included

I loved the idea of bringing mono to Android, so keep everyone posted about your changes.
We'll make sure they are integrated back into the main tree once you figure out the way to go.
On Tue, Jan 13, 2009 at 11:53 PM, Koushik K. Dutta <koush at koushikdutta.com<mailto:koush at koushikdutta.com>> wrote:

Hi Rodrigo. Thanks for your response. After working on Mono/Android a bit further, I decided I need to think through the problem more. Linux and Android use significantly different linkers which is causing other various issues. I may end having to link versus Bionic after all, and implementing whatever dependencies are missing. So for now, this change may be unnecessary...

Koushik Dutta


From: Rodrigo Kumpera [mailto:kumpera at gmail.com<mailto:kumpera at gmail.com>]
Sent: Monday, January 12, 2009 10:24 AM
To: Koushik K. Dutta
Cc: mono-devel-list at lists.ximian.com<mailto:mono-devel-list at lists.ximian.com>
Subject: Re: [Mono-dev] Tweaks needed in security.c for running Mono on Android - patch included

Hi Koushik,

To have your patch accepted into the mono runtime you need to state that your contribution is under the MIT/X11 license
otherwise we can't commit it.

Besides this, most of the code in System.Security is windows centered and makes no sense under Android. Maybe the best
solution is just remove it as using it would make no sense at all.


2009/1/11 Koushik K. Dutta <koush at koushikdutta.com<mailto:koush at koushikdutta.com>>

Hi all, I'm working on getting Mono running under Android. So far, I've been able to get things running with next to no code changes (by way of configure, compiler and linker options).

I did run into one issue though that required a code change. Mono has a dependency on libc.so. On Android, Google implemented a custom lightweight version of libc.so, which they call "Bionic". I have tried building Mono against Bionic, but at the moment, it seems basically impossible because Bionic does not have Unicode support. (I will dig into other solutions for this further, but it's low priority at the moment... I want to get things running :))

So, as a result, Mono on Android needs to be packaged with the standard ARM/Linux version of libc.  However, there are significant differences between the Linux file system and Android file system. So when Mono's uses libc's getpwuid and getpwuid_r, it fails, and causes further problems in the runtime. Linux's getpwuid works by examining various files in /etc. But, Android does not have a /etc. Android has a uid/gid repository that is very different from typical Linux distributions.

For reference, here is Bionic's implementation of getpwuid (there is no getpwuid_r):

struct passwd* getpwuid(uid_t uid)


    stubs_state_t*  state = __stubs_state();

    struct passwd*  pw;

    if (state == NULL)

        return NULL;

    pw = &state->passwd;

    if ( android_id_to_passwd(pw, uid) != NULL )

        return pw;

    if (uid < AID_APP) {

        errno = ENOENT;

        return NULL;


    snprintf( state->app_name_buffer, sizeof state->app_name_buffer,

              "app_%d", uid - AID_APP );

    pw->pw_name  = state->app_name_buffer;

    pw->pw_dir   = "/data";

    pw->pw_shell = "/system/bin/sh";

    pw->pw_uid   = uid;

    pw->pw_gid   = uid;

    return pw;


getpwuid is used in three locations Mono. The two troublesome ones are in mono/metadata/security.c in the functions "GetTokenName" and "IsMemberOf". (The third is in pwd.c for Mono's Posix syscall wrapper, but that is not causing the problem. I'll fix that later.)

I have attached a patch file that I have successfully built and tested against Android.  It is also regression safe, as all the new code is inside a #ifdef PLATFORM_ANDROID block. The workaround is simple, and there are comments explaining how the Android uid model works.

Alternatively, I can obviously compile a custom version of libc.so for Mono, but I do not think that is a good solution. Mono is already using #ifdef PLATFORM_WIN32 at the same points in code that I placed my PLATFORM_ANDROID, and I'm assuming that the Mono developers will be more open to my proposed changes.

For information about how to build Mono to target Android, you can reference my post: http://www.koushikdutta.com/2009/01/building-mono-for-android.html. Or feel free to contact me.


Koushik Dutta


Koushik Dutta


Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com<mailto:Mono-devel-list at lists.ximian.com>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090118/8d9a9e61/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: androidmono.patch
Type: application/octet-stream
Size: 2979 bytes
Desc: androidmono.patch
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090118/8d9a9e61/attachment-0001.obj 

More information about the Mono-devel-list mailing list