How to use unicode in Android resource?

Your character (U+1F4E1) is outside of Unicode BMP (Basic Multilingual Plane – range from U+0000 to U+FFFF).

Unfortunately, Android has very weak (if any) support for non-BMP characters. UTF-8 representation for non-BMP characters requires 4 bytes (0xF0 0x9F 0x93 0xA1). But, Android UTF-8 parser only understands 3 bytes maximum (see it here and here).

It works for you when you use UTF-16 surrogate form representation of this character: "\uD83D\uDCE1". If you were able to encode each surrogate UTF-16 character in modified UTF-8 (aka CESU-8) – it would take 6 bytes total (3 bytes in UTF-8 for each member of surrogate pair), then it would be possible. But, Android does not support CESU-8 explicitly either.

So, your current solution – hard-coding this symbol in source code as surrogate UTF-16 pair seems easiest, at least until Android starts fully supporting non-BMP UTF-8.

UPDATE: this seems to be partially fixed in Android 6.0. This commit has been merged into Android 6, and permits presence of 4-byte UTF-8 characters in XML resources. Its not perfect solution – it will simply automatically convert 4-byte UTF-8 into appropriate surrogate pair. However, it allows to move them from your source code into XML resources. Unfortunately, you can’t use this solution until your application can stop supporting any Android version except for 6.0 and later.

Leave a Comment