Sunday, October 23, 2011

Android NDK no love for wchar_t

I had to port some existing C/C++ code to Android (NDK) recently.
The code was using wchar_t and wstring quite often.
After a bit of research found out that it might not be as easy as I initially thought.

Basically, the wchar_t support is broken and most of the w* functions are not implemented as far as I can tell.
The wchar_t was essentially defined as one byte char before Android Froyo (2.2) and only starting from 2.3 it is 4 bytes long.

Here is the quite entertaining comment from their wchar.h file:

/* IMPORTANT: Any code that relies on wide character support is essentially
 *            non-portable and/or broken. the only reason this header exist
 *            is because I'm really a nice guy. However, I'm not nice enough
 *            to provide you with a real implementation. instead wchar_t == char
 *            and all wc functions are stubs to their "normal" equivalent...
 */

Is seems like there are at least couple of possible ways to solve this problem:
1. Fix the existing code, and remove the dependency on wchar_t (wstring etc).
2. Try to use the CrystaX android development toolkit instead, that seems to have a better support for wchar_t and other w* APIs: http://www.crystax.net/en/android/ndk/6

Will continue digging into this tomorrow.

No comments: