21

I would like to integrate the emoji symbol in my android app. Therefore I looked up the hex code for the utf-8 symbol and added the following to my string.xml file:

<string name="thumbsup">Perfect <node>&#x1f44d;&#x1f44d;</node></string>

This should result into Perfect . However, instead my app crashes when the call activity tries to display this:

JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0

Not particularly perfect ;)

toom
  • 12,864
  • 27
  • 89
  • 128
  • I don't know the format of the `string.xml` file but your example is not valid XML. Your `` node contains some text and another XML node (``). Just `Perfect 👍👍` as value might work. – AndrĂ© Stannek Mar 04 '16 at 09:54
  • Hello please refer this `http://stackoverflow.com/questions/24852806/how-can-i-put-utf-16-characters-in-android-string-resource`. – rushank shah Mar 04 '16 at 10:00
  • 1
    @toom sorry for the wrong link - here is the link `http://stackoverflow.com/questions/33288225/how-can-i-put-a-ora-any-other-emoji-insidea-an-xml-string` – rushank shah Mar 04 '16 at 10:09
  • Okay, thanks. I directy integrated it into my code and removed it from the string.xml – toom Mar 04 '16 at 10:13
  • I'm getting a similar crash in Android 5 but not in Android 7. – Suragch Apr 03 '17 at 02:46

2 Answers2

7

The fix for that is: Add "--utf16" to aapt by adding

android {
    aaptOptions {
        additionalParameters '--utf16'
    }
}

to your build.gradle file, and make sure you are not using aapt2.

See https://issuetracker.google.com/issues/37140916

emrekose26
  • 683
  • 1
  • 8
  • 21
Menny
  • 473
  • 6
  • 13
  • 3
    this does not work anymore in [aapt2](https://developer.android.com/studio/command-line/aapt2) which is enabled by default when you use gradle 3.0+ – Santiago Hernández Oct 04 '18 at 20:01
3

It seems that newer versions of Android don't cause the crash (API 24 worked in my tests), but that doesn't help if you are supporting older versions. The best I have been able to figure out is to use Java coded strings.

public class AppEmojiStrings {

    // This is only a workaround for emoji causing crashes in XML strings.
    // Use the standard strings.xml for all other strings.

    public static final String thumbsUp = "Thumbs up "; 
    public static final String iLoveNY = "I \uD83D\uDC99 NY";
}

There are a number of disadvantages with this method, the main one being that it can't be accessed in the layout XML files. But it may be a viable workaround for some situations.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • You might be able to use Data binding if you really needed it in the XML – OneCricketeer Sep 04 '17 at 22:02
  • @cricket_007, I haven't done much with [Android Data Binding](https://developer.android.com/topic/libraries/data-binding/index.html). I also wonder if [formatted strings](https://stackoverflow.com/questions/3656371/dynamic-string-using-string-xml/40715374#40715374) might work. If it does work for anyone, please leave a comment. – Suragch Sep 04 '17 at 23:59