Error in strings.xml file in Android

AndroidXmlEscaping

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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>

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

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.

Solution 7 - Android

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.

Solution 8 - Android

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

Solution 9 - Android

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

Solution 10 - Android

  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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSubratView Question on Stackoverflow
Solution 1 - AndroidPriyankView Answer on Stackoverflow
Solution 2 - AndroidJGallardoView Answer on Stackoverflow
Solution 3 - AndroidsitueeView Answer on Stackoverflow
Solution 4 - AndroidWalmacView Answer on Stackoverflow
Solution 5 - AndroidElad FinishView Answer on Stackoverflow
Solution 6 - AndroidLi3roView Answer on Stackoverflow
Solution 7 - AndroidDan BrayView Answer on Stackoverflow
Solution 8 - AndroidPhoton PointView Answer on Stackoverflow
Solution 9 - AndroidCodeToLifeView Answer on Stackoverflow
Solution 10 - AndroidPrajwal WaingankarView Answer on Stackoverflow