android string.xml apostrophe quoting

I have a line in a strings.xml file which builds fine when I build through eclpise but fails when I use the tools through IntelliJ 12.1.2 community edition.

This line:


    <string name="dcsNotifDismiss">Dismiss\n(“Don't Remind Me Again”)</string>


I get this error when building through IDEA:

android-apt-compiler: [DCS] PROJECTPATH/res/values/strings.xml:325: error: Apostrophe not preceded by \ (in Dismiss\n(“Don't Remind Me Again”))

The problem is, according to this page:

https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

It says it should work

Escaping apostrophes and quotes

If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:

<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>





I would just quote it and be done with it, but on some devices when the apostrophe is quoted inside of the double quotes, it doesn't get drawn in the UI.

Howard

2 comments

Sorry, I didn't understand the problem. The page https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling says "If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes".
But your string "Dismiss\n(“Don't Remind Me Again”)" contains the quotes are not escaped and it is not enclosed "in the other type of enclosing quotes", so it produces compilation error. Probably Eclipse escapes the quotes automatically sometimes, so it compiles it without errors.

0

The issue is that both of these are legal acording to the android documentation:

    <string name="dcsNotifDismiss">Dismiss\n("Don\'t Remind Me Again")</string>

    <string name="dcsNotifDismiss">Dismiss\n("Don't Remind Me Again")</string>


And yet only the first one compiles in IntelliJ.

The documentation shows these examples:

<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>


I have found that the first form of my string:

    <string name="dcsNotifDismiss">Dismiss\n("Don\'t Remind Me Again")</string>


compiles, but does not always work on all devices (the apostraphe was missing on some devices, but not all) so I had to use the other form:

    <string name="dcsNotifDismiss">Dismiss\n("Don't Remind Me Again")</string>


which won't compile.


I ultimately had to use this string, which does compile and does work on all devices:

    <string name="dcsNotifDismiss">Dismiss\n(\""Don\'t Remind Me Again\"")</string>


It just seems excessive...

Howard
0

Please sign in to leave a comment.