Subrat
Subrat

Reputation: 3988

Error in strings.xml file in Android

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

Upvotes: 140

Views: 156796

Answers (12)

Mohamad suhieb ajaj
Mohamad suhieb ajaj

Reputation: 3

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

Upvotes: 0

Nikhil S Marathe
Nikhil S Marathe

Reputation: 781

  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.
    

Upvotes: 0

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2728

  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"

Upvotes: 1

Elad Finish
Elad Finish

Reputation: 101

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

Upvotes: 9

Dan Bray
Dan Bray

Reputation: 7832

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.

Upvotes: 4

CodeToLife
CodeToLife

Reputation: 4171

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

Upvotes: 1

Priyank
Priyank

Reputation: 10623

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

Upvotes: 347

Photon Point
Photon Point

Reputation: 808

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

Upvotes: 2

Walmac
Walmac

Reputation: 183

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.

Upvotes: 12

situee
situee

Reputation: 2740

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>

Upvotes: 20

JGallardo
JGallardo

Reputation: 11383

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

Upvotes: 36

Li3ro
Li3ro

Reputation: 1877

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.

Upvotes: 4

Related Questions