Error in strings.xml file in Android
Asked Answered
I

11

139

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
Indubitability answered 13/4, 2011 at 23:55 Comment(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.Petrarch
P
347

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

Pashm answered 14/4, 2011 at 0:15 Comment(5)
Why is no one talking about this idiotic requirement that we escape every single apostrophe in string data? Even within CDATA sections!Annamaeannamaria
@PhilKulak heh, it's pretty crazy with french for example. We'll search and replace ftw.Haskell
@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/…Tuxedo
This can cause a problem when you have a lot of strings and use replace all. See my answer.Ruffianism
if you are using it in gradle file as resValue, use " \\' "Devindevina
C
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

Cosmopolis answered 3/11, 2014 at 2:17 Comment(1)
Silly me, since I was using XML, I was using &apos;, and that's an error too.Overage
T
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>
Tuxedo answered 18/3, 2015 at 2:52 Comment(1)
XML encoding doesn't work with the &apos; form, but works with the &#39; form.Cablegram
L
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.

Leslileslie answered 3/7, 2016 at 2:43 Comment(1)
Haha! That just happened to me too. The apostrophe kept popping back up for exactly that reason. Was driving me nuts.Patroclus
A
9

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

Aerophyte answered 28/12, 2018 at 10:11 Comment(0)
H
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.

Herschel answered 3/4, 2012 at 8:43 Comment(2)
@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 messyHerschel
We solved for Hebrew apostrophes by escaping on both sides: \'\Luminescence
R
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.

Ruffianism answered 23/10, 2018 at 14:3 Comment(0)
C
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" .

Columnar answered 29/2, 2016 at 8:23 Comment(0)
D
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 :)

Datnow answered 24/5, 2018 at 10:44 Comment(0)
M
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"
Merriemerrielle answered 24/12, 2021 at 13:9 Comment(0)
B
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.
    
Birdbath answered 7/6, 2023 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.