27

Somehow I can't get my UTF-8 sources to play nice with Ant.

I get a whole lot of "warning: unmappable character for encoding ascii". I'm going crazy, really. Hours and hours and hours. Btw, I noticed 5 people already used the tag crazy. :-)

And yes, I've read this, this and others. Google also (the first 5 page results of at least 3 or 4 different searches, at least). There are javac options. I've tried. There is also some preset or something (sorry, 3 AM). Didn't work either.

I'm generating Android apk files with Ant. I can't use Eclipse, so no. And by the way, the ant documentation is gibberish to me. Those examples are of no use at all. I've lost count on how much I've tried.

I've tried using the Dfile.encoding option, tried mixing that with CHCP 65001 Windows command. Did all the combinations, and it even makes Ant (Javac I guess) stop spitting errors, but it still doesn't matter. My code still ends up with garbage carachters (a bunch of ?? instead of á, í etc).

Community
  • 1
  • 1
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • 1
    I can understand your frustration, but you aren't giving us much to help you with. If you could boil it down to a simple example with a build.xml and a Java source file, you will increase your chances of getting an answer quite a bit. – JesperE Nov 21 '10 at 08:21
  • I know I'm not. That exactly why I was so frustrated. I know how horrible it is to answer those questions. Problem is that even I didn't know where to start. That's why I'm sorry I could add any more information. I should probably add the 'android' tag also, but then again I can only add 5 tags (I know, beginners ruled low, right?). Anyway, I couldn't even see any tasks... I guess that was my problem. I finally discovered the solution this morning. It was quite simple, but somewhat well hidden... at least for me. But thank you... – davidcesarino Nov 21 '10 at 16:00
  • Would you mind sharing your solution with others? – Joonas Pulakka Nov 21 '10 at 17:55
  • Of course! I was having lunch! I guess I'm excused, right? But I was writing a solution. In a few minutes I will post a detailed guide to what was wrong and how to fix it!!! – davidcesarino Nov 21 '10 at 18:13
  • I mean "of course not", sure! :-P OK, that was the best solution for me. As I said, it's very inelegant (and I am aware of that), but it was the easiest for me since I always work with UTF-8 files. Best regards! – davidcesarino Nov 21 '10 at 18:55

5 Answers5

32

Are you specifying the file encoding to the compiler correctly? The java compiler will otherwise default to use the platform's default encoding, which on Windows (for example) is not UTF-8. The encoding is specified by using the -encoding flag to javac.

javac -encoding utf8 ...

And in ant build.xml:

<javac ... encoding="UTF-8" ... />
Zds
  • 4,311
  • 2
  • 24
  • 28
JesperE
  • 63,317
  • 21
  • 138
  • 197
21

Just specify encoding in your build.xml script:

<javac encoding="UTF-8" ... />

But for code re-use it's better to extract value into project-specific configuration file:

<property file="build.properties" />
<property name="java.encoding" value="ascii" />
<javac encoding="${java.encoding}"
       source="${java.source}" ....>
       <src path="${source.dir}" />
       <classpath>
            <fileset ... />
       </classpath>
 </javac>

And then just add java.encoding=UTF-8 to build.properties to override encoding on per-project basis.

Alexander Kosenkov
  • 1,597
  • 1
  • 10
  • 21
5

You should set the same page in many places:

  1. in Ant Script javac encoding="UTF-8" ... /
  2. -Dfile.encoding=UTF-8 in parameters of Ant calling
  3. In file-properties-resource-encoding - again choose the same page. You can also change it for all files in the container by the IDE (preferences for Eclipse). Then make all files to inherit it from the container.

  4. If some files were created in wrong encoding, rework their content through PsPad or maybe, another text editor (copy-paste, change file encoding, back copy-paste). These files will be mentioned in unmappable character errors.

After setting all these four problems you will have NO unmappable character lines.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
5

To all you who use UTF-8 source files on Windows and was trying to build Android packages with Proguard using the famous blog post by Dan Galpin/Tim Bray.

This encoding problem happens with javac (thanks for the tip, JesperE). However, I was unable to create a new javac rule on my project's files, because of needed parameters that I didn't know anything about. So here is the easy answer (probably not the only answer):

  1. Try the usual "ant release" command.
  2. Notice that, on the start, there is an output talking about some imported ANT rules. Right on the start you will see some [setup] rules. Look for this one:

    [setup] Importing rules file: tools\ant\ant_rules_r3.xml

  3. Find that file and open it. Search for "javac encoding". You will see that is set to "ascii". Change to "UTF-8".

  4. Do the "ant release" again and it will be fine.

That's how I did here. I'm sure there is a way to override this on a per-project basis. But it kept giving me errors on mandatory parameters, as I said. So for me at least it was that much easier doing this way. Besides, I only work with UTF-8 anyway.

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • I know this is not the best solution (not a per-project one). But until I learn the javac and ant inner-workings, this is what I'm going to do and what works best for my problem. If you do this, remember that you're changing the option on the SDK. YOU HAVE BEEN WARNED! – davidcesarino Nov 21 '10 at 18:59
1

below is the usage of -Dfile.encoding=UTF-8 in ant build.xml file:

<java classname="${main-class}" fork="true">
<jvmarg value="-Dfile.encoding=UTF-8"/>

This will solve the problem. Working for all UTF 8 char's.

Ravi Kumar
  • 11
  • 1