The escape rules that break builds
Android reads a strings.xml value in steps. The file is parsed as XML first. Android
then collapses whitespace and applies its own backslash escapes. For <string>
elements, AAPT2 also checks the format arguments. Each step has its own rules.
Apostrophes and quotes
An unescaped apostrophe fails the build with the AAPT2 error "unescaped apostrophe in string".
Don't tap here must be written as Don\'t tap here, or the whole value
wrapped in double quotes: "Don't tap here".
A literal double quote needs \". Without the backslash, a double quote starts or
ends a quoted region and does not appear in the text. Wrapping the string in single quotes does
not work.
Quoting has a side effect. Inside double quotes, all whitespace is kept as written, new lines included. Outside quotes, Android collapses each run of whitespace into one space.
A curly apostrophe (U+2019) is a different character from the XML apostrophe, so AAPT2 accepts it without an escape. This tool converts curly apostrophes and quotes to straight ones, then escapes them.
The @ and ? prefix
A value that starts with @ can be read as a resource reference, such as
@string/name. A value that starts with ? can be read as a theme
attribute reference. To show the character as text, escape it: \@user mentioned you.
Only the first character matters, so contact us @ support needs no escape. The tool
escapes only a leading @ or ?.
Format arguments, positional or not
A string with one format specifier can use the short form: Welcome, %s. With more
than one, use positional specifiers: %1$s gave you %2$d coins. A translation can
then put the arguments in a different order, for example %2$d Münzen von %1$s.
Two or more non-positional specifiers fail the AAPT2 build with "multiple substitutions specified
in non-positional format". A mix of positional and non-positional specifiers fails the same way.
AAPT2 skips this check for strings marked formatted="false" or
translatable="false".
A literal percent sign matters when the string goes through a format call.
getString(id, args) substitutes arguments with String.format rules,
where a literal percent sign is written %%. getString(id) with no
arguments does no formatting. formatted="false" only turns off the AAPT2 check. It
does not change how String.format reads the text at runtime.
Ampersands and angle brackets
& and < follow the standard XML escaping rules.
Coffee & Tea must be written Coffee & Tea. The tool also
escapes >. A string can contain HTML styling tags, and those must stay as tags.
The Android docs list the supported tags, for example b, i,
u, br, tt, big, small,
sub, sup, strike, font and em.
With the markup option on, the tool keeps those tags and strong as markup and
escapes every other angle bracket. Turn the option off to escape all of them.
New lines and whitespace
Android collapses each run of whitespace into a single space, and a line break in the XML counts
as whitespace. For a real line break, use \n. For a tab, use \t.
AAPT2 removes leading and trailing whitespace from a string that has no styling tags. To keep it,
wrap the value in double quotes.
What this tool does not do
It does not check that your XML is well-formed and does not look for duplicate string names.
AAPT2 reports both at build time. The output has one <string> element per
line, so your original indentation and comments are not kept. It reads only
<string> elements. <plurals> and
<string-array> entries are left out of the output, although their
<item> values follow the same escape rules.
How Android parses strings.xml
The Android docs state that whitespace collapsing and Android escaping happen after the file is
parsed as XML. So an XML character reference such as   becomes a space
during XML parsing, then collapses like any other space. To keep such spaces, put them inside
double quotes or write them as \u escapes.
The format argument check runs after that, on the processed text of each
<string>. A single string can break rules at more than one step. The tool
applies the XML escapes, the backslash escapes and the positional rewrite in one pass.
Build time and runtime
The escapes in strings.xml are resolved when the app is built. At runtime,
getString() returns the decoded text: \' is an apostrophe and
\n is a new line. You do not undo escapes in Kotlin or Java.
getString() strips styling tags. getText() keeps them.
Formatting methods such as getString(id, args) also strip styling. To combine
styling with format arguments, the Android docs store the tags HTML-escaped, as in
<b>, format the string, then convert the result with
AnnotatedString.fromHtml(). Escape each argument with
TextUtils.htmlEncode() first, because fromHtml() reads HTML in the
whole string.