Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emoji symbol 👍 in string.xml crashes app

Tags:

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 ;)

like image 829
toom Avatar asked Mar 04 '16 09:03

toom


People also ask

Does XML support Emoji?

You can put emojis in an XML, and the squares are just the editor's way to render characters it can't recognize, like all other characters, you can specify the character by its UTF8 value instead. For example, 😃 has the char value of 128515, so you could use &#128515; to specify it.

How do you send an emoji in XML?

Use HTML Entity (decimal) i.e. &#128072; to add 👈 in strings. xml and use it in your app. Show activity on this post. However, just pasting it as the emoji character from clipboard ❤️ direct into the translations editor worked just fine, and perhaps I needn't have worried at all.


2 Answers

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

like image 72
Menny Avatar answered Oct 05 '22 23:10

Menny


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.

like image 45
Suragch Avatar answered Oct 06 '22 00:10

Suragch