(#) Invalid string escapes !!! ERROR: Invalid string escapes This is an error. Id : `StringEscaping` Summary : Invalid string escapes Severity : Error Category : Correctness: Messages Platform : Android Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Since : 7.3.0 (September 2022) Affects : Resource files Editing : This check runs on the fly in the IDE editor Implementation : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/StringEscapeDetector.kt) Tests : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/StringEscapeDetectorTest.kt) Apostrophes (') must always be escaped (with a \\\\), unless they appear in a string which is itself escaped in double quotes (\"). !!! Tip This lint check has an associated quickfix available in the IDE. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text res/values/strings.xml:3:Error: Apostrophe not preceded by \ [StringEscaping] <string name="some_string">'ERROR'</string> ^ res/values/strings.xml:5:Error: Apostrophe not preceded by \ [StringEscaping] <string name="some_string3">What's New</string> ^ res/values/strings.xml:12:Error: Bad character in \u unicode escape sequence [StringEscaping] <string name="some_string10">Unicode\u12.</string> ^ res/values/strings.xml:19:Error: Apostrophe not preceded by \ [StringEscaping] <item>It's incorrect</item> ^ res/values/strings.xml:23:Error: Apostrophe not preceded by \ [StringEscaping] <item quantity="few">%d piose'nki.</item> ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `res/values/strings.xml`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers <?xml version="1.0" encoding="utf-8"?> <resources> <string name="some_string">'ERROR'</string> <string name="some_string2">"'OK'"</string> <string name="some_string3">What's New</string> <string name="some_string4">Unfinished\</string> <string name="some_string5">Unicode\u</string> <string name="some_string6">Unicode\u1</string> <string name="some_string7">Unicode\u12</string> <string name="some_string8">Unicode\u123</string> <string name="some_string9">Unicode\u1234</string> <string name="some_string10">Unicode\u12.</string> <string name="news"> " What's New " </string> <string name="space_slash"> \</string> <string name="space_slash2"> \</string> <string name="space_slash3"> \</string> <string-array name="array_of_string"> <item>It\'s correct</item> <item>It's incorrect</item> </string-array> <plurals name="numberOfSongsAvailable"> <item quantity="one">%d piosenkÄ™.</item> <item quantity="few">%d piose'nki.</item> <item quantity="other">%d piosenek.</item> </plurals> <string name="foo">Letzte Freischaltung:\\ </string> <!-- b/387281249 --> </resources> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/StringEscapeDetectorTest.kt) for the unit tests for this check to see additional scenarios. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Adding the suppression attribute `tools:ignore="StringEscaping"` on the problematic XML element (or one of its enclosing elements). You may also need to add the following namespace declaration on the root element in the XML file if it's not already there: `xmlns:tools="http://schemas.android.com/tools"`. ```xml <?xml version="1.0" encoding="UTF-8"?> <resources xmlns:tools="http://schemas.android.com/tools"> ... <string tools:ignore="StringEscaping" .../> ... </resources> ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="StringEscaping" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'StringEscaping' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore StringEscaping ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).