141

I have declared a long string in string.xml of an application.

Declared like this

<string name="terms">PLEASE READ THESE TERMS OF USE CAREFULLY  BY ACCESSING THIS .................</string>

But this gives the following error :

error: Apostrophe not preceded by \ (in PLEASE READ THESE TERMS OF USE CAREFULLY
1
  • 1
    error is, because too many dots in string. `error: Apostrophe not preceded by ` is a dummy message, problem is in dots. As I remember, after more than three dot's you can get error. Commented Nov 3, 2014 at 2:23

12 Answers 12

348

post your complete string. Though, my guess is there is an apostrophe (') character in your string. replace it with (\') and it will fix the issue. for example,

//strings.xml
<string name="terms">
Hey Mr. Android, are you stuck?  Here, I\'ll clear a path for you.  
</string>

Ref:

http://www.mrexcel.com/forum/showthread.php?t=195353

https://code.google.com/archive/p/replicaisland/issues/48

Sign up to request clarification or add additional context in comments.

5 Comments

Why is no one talking about this idiotic requirement that we escape every single apostrophe in string data? Even within CDATA sections!
@PhilKulak heh, it's pretty crazy with french for example. We'll search and replace ftw.
@PhilKulak In Android strings.xml, you can enclose the whole string in the other type of enclosing quotes like <string name="good_example">"This'll work"</string> Reference: developer.android.com/guide/topics/resources/…
This can cause a problem when you have a lot of strings and use replace all. See my answer.
if you are using it in gradle file as resValue, use " \\' "
37

Solution

Apostrophes in the strings.xml should be written as

\'


Example

In my case I had an error with this string in my strings.xml and I fixed it.

<item>Most arguments can be ended with three words, "I don\'t care".</item>

Here you see my app builds properly with that code.

enter image description here

Here is the actual string in my app.

enter image description here

1 Comment

Silly me, since I was using XML, I was using &apos;, and that's an error too.
20

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

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>

1 Comment

XML encoding doesn't work with the &apos; form, but works with the &#39; form.
12

You have to put \ before an apostrophe. Like this \' , Also check that you are editing strings.xml and not values.xml (android studio directs you to this file when shows the error). Because if you edit values.xml and try to compile again, the error persists. This was happening to me recently.

1 Comment

Haha! That just happened to me too. The apostrophe kept popping back up for exactly that reason. Was driving me nuts.
9

Use this regex (?<!\\)' for searching an unescaped apostrophe. It finds an apostrophe that not preceded by a backslash.

Comments

5

The best answer of simply escaping the ' with \' works but I do not like the solution because when using replace all with ' to \', it only works once. The second time you'll be left with \\'.

Therefore, use: \u0027.

Comments

4

I use hebrew(RTL language) in strings.xml. I have manually searched the string.xml for this char: ' than I added the escape char \ infront of it (now it looks like \' ) and still got the same error!

I searched again for the char ' and I replaced the char ' with \'(eng writing) , since it shows a right to left it looks like that '\ in the strings.xml !!

Problem solved.

2 Comments

@akauppi, This comment is more for the RTL languages developers, since we encounter editors that cannot handle RTL very well, by showing characters in a wrong order on screen. typing in long hebrew sentenses in xml can gets messy
We solved for Hebrew apostrophes by escaping on both sides: \'\
2

You may be able to use unicode equivalent both apostrophe and other characters which are not supported in xml string. Apostrophe's equivalent is "\u0027" .

Comments

1

1. for error: unescaped apostrophe in string

what I found is that AAPT2 tool points to wrong row in xml, sometimes. So you should correct whole strings.xml file

In Android Studio, in problem file use :

Edit->find->replace

Then write in first field \' (or \&,>,\<,\")
in second put '(or &,>,<,")
then replace each if needed.(or "reptlace all" in case with ')
Then in first field again put '(or &,>,<,")
and in second write \'
then replace each if needed.(or "replace all" in case with ')

2. for other problematic symbols
I use to comment each next half part +rebuild until i won't find the wrong sign.

E.g. an escaped word "\update" unexpectedly drops such error :)

Comments

1
  1. To Find all the ' in your file, do the following:

Ctrl + F [Enter in the search field apostrophe] '

  1. ' should be written as "World's"

Comments

0
  1. In this case please care for all string that,

  2. not any word has " ' " this sign as normal sentence.

  3. just add " \ " before this mark.

    ex.  Normal sentence: I 'll don't make cry anyone.
           Solution sentence : I \'ll don\'t make cry anyone.
    

Comments

0

In Android XML files, the escape sequence &apos; is used to represent a single quote ('). This is the correct way to escape single quotes in XML. If you use card's, it should be correct for XML.

However, if ' is causing issues, it might be due to other factors like encoding problems or errors elsewhere in the XML. Ensure your XML file is saved with UTF-8 encoding, and all other strings are correctly formatted. If problems persist, check for hidden characters or invalid escape sequences.

Using card\'s is not correct in XML. In XML, the backslash () is not used for escaping characters. Always use ' for single quotes.

that is work for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.