Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android displays text in wrong encoding after update to Java 8

I've updated my project to SDK version 24 and Java 8 and encountered a strange encoding issue.

By some strange reason Android treats my hardcoded UTF-8 strings as Windows-1251 and thus the text is garbled.

Like this:

enter image description here

This is what I changed:

compileSdkVersion 24
buildToolsVersion "24.0.2"

compileOptions {
    encoding = 'utf-8'
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

And what I've tried:

  • changed encoding in Android Studio settings
  • added compileOptions.encoding = 'windows-1251' and compileOptions.encoding = 'utf-8'
  • added system variable JAVA_TOOL_OPTIONS:-Dfile.encoding=UTF8

If I revert back to Java 7 everything is ok.

Any ideas?

like image 898
FelisManulus Avatar asked Sep 15 '16 14:09

FelisManulus


2 Answers

This is a bug in Jack compiler, have a look at this post.

Jack has been using the default VM encoding, which on windows is windows-1252. So, your text is encoded with UTF-8 and decoded with windows-1252 by Jack compiler, and this is why they became garbled.

Until now, Gradle Plugin doesn't support an API to set the decode format, so we need to wait...

like image 53
L. Swifter Avatar answered Nov 09 '22 16:11

L. Swifter


I had the same issue in Windows(but not Mac OSX). this line solve my problem(put it in gradle.properties of your project):

org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

note that this part change encoding:

-Dfile.encoding=UTF-8
like image 30
Hamed Ghadirian Avatar answered Nov 09 '22 17:11

Hamed Ghadirian