136

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.
    – Dudeist
    Nov 3, 2014 at 2:23

11 Answers 11

345

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

5
  • 36
    Why is no one talking about this idiotic requirement that we escape every single apostrophe in string data? Even within CDATA sections!
    – Phil Kulak
    Mar 15, 2012 at 19:02
  • 3
    @PhilKulak heh, it's pretty crazy with french for example. We'll search and replace ftw.
    – Warpzit
    May 24, 2014 at 16:31
  • 8
    @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/…
    – situee
    Mar 18, 2015 at 2:44
  • This can cause a problem when you have a lot of strings and use replace all. See my answer.
    – Dan Bray
    Oct 23, 2018 at 14:08
  • if you are using it in gradle file as resValue, use " \\' " Apr 13, 2021 at 15:33
36

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
  • 2
    Silly me, since I was using XML, I was using &apos;, and that's an error too. Mar 2, 2020 at 19:33
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
  • XML encoding doesn't work with the &apos; form, but works with the &#39; form.
    – Thomas
    Jul 8, 2021 at 16:49
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
  • 1
    Haha! That just happened to me too. The apostrophe kept popping back up for exactly that reason. Was driving me nuts.
    – DrMcCleod
    Oct 15, 2018 at 13:02
9

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

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.

3
  • @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
    – Li3ro
    May 6, 2014 at 19:22
  • Ah, Right-To-Left. It all makes sense now. Thanks.
    – akauppi
    May 7, 2014 at 11:15
  • 3
    We solved for Hebrew apostrophes by escaping on both sides: \'\
    – radley
    Jul 30, 2014 at 22:48
3

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.

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" .

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

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"
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.

** ☻♥ Done Keep Code.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.