The Android build stops with Failed to compile values file on a strings.xml or other values file
Failed to compile values fileShort answer
AAPT2 compiles every file in res/values on its own and stops at the first one it cannot parse. The error line names the file and line number. The usual culprit is text that needs escaping: an unescaped apostrophe or quote, a raw & or <, a string that starts with @ or ?, or a file that is not valid resources XML.
Fix it fast
1Find out which cause you have
Build from the terminal and read the lines that start with a file path and say error. They give the exact values file and line that AAPT2 rejected, which is faster than reading the whole Gradle output. Translation folders such as values-fr count too.
./gradlew assembleDebug ls app/src/main/res | grep '^values'2Match what you see, then jump to its fix
- 1
The failing line is a string that contains a single quote, as in Don't, or a double quote inside the textAn apostrophe or a double quote is not escapedFix - 2
The failing line holds & or < inside a string, for example in Terms & Conditions or a less-than signA raw ampersand or angle bracket is in the textFix - 3
The failing line is a string whose text begins with an at sign or a question mark, such as an @handleA string starts with @ or ?Fix - 4
The error names a line you did not expect, and the file was edited by hand, merged, or written by a toolThe file is not valid resources XMLFix
- 1
What does the error look like?
Failed to compile values file
Cause 1 of 4
An apostrophe or a double quote is not escaped
1Check
Android requires escaped apostrophes and quotation marks in string resources. A single quote needs a backslash, or the whole string must sit inside double quotes. A double quote needs a backslash, and surrounding the text with single quotes does not work. Every values folder has its own copy of the strings, so a translation such as values-fr can fail while the default file is fine.
grep -n "'" app/src/main/res/values*/strings.xml2Fix
Escape the character, or wrap the string in double quotes.
<resources>
<string name="stop_one">Don\'t stop</string>
<string name="stop_two">"Don't stop"</string>
<string name="said">He said \"stop\"</string>
</resources>3Verify
Compile just that file with AAPT2 from the SDK build tools, then build the app.
mkdir -p compiled
aapt2 compile app/src/main/res/values/strings.xml -o compiled/
./gradlew assembleDebugConfirmed by developer.android.com
Cause 2 of 4
A raw ampersand or angle bracket is in the text
1Check
A values file is XML. The XML standard says & and < must not appear in literal form except as markup, and must be escaped as & and <. Android's documentation says the same for string resources. Look for & and < in the failing line.
grep -n -E '&[^a-z#]|<[^/a-z!?]' app/src/main/res/values*/*.xml2Fix
Use the XML entities for the characters.
<resources>
<string name="terms">Terms & Conditions</string>
<string name="limit">Value < 10</string>
</resources>3Verify
Compile the file and build again.
mkdir -p compiled
aapt2 compile app/src/main/res/values/strings.xml -o compiled/
./gradlew assembleDebugConfirmed by w3.org
Cause 3 of 4
A string starts with @ or ?
1Check
In Android resources a leading @ or ? has a special meaning, so the character in plain text has to be escaped with a backslash.
grep -n -E '>[@?]' app/src/main/res/values*/*.xml2Fix
Put a backslash in front of the character.
<resources>
<string name="handle">\@example</string>
<string name="question">\?</string>
</resources>3Verify
Compile the file and build again.
./gradlew assembleDebugConfirmed by developer.android.com
Cause 4 of 4
The file is not valid resources XML
1Check
A string resource file needs a resources element as the root node. The XML standard adds that a document has exactly one root, that every start-tag is closed by an end-tag, and that elements nest properly; a processor stops on the first violation. A leftover merge conflict marker or a missing closing tag breaks that.
head -n 5 app/src/main/res/values/strings.xml
grep -n -E '^(<<<<<<<|=======|>>>>>>>)' app/src/main/res/values*/*.xml2Fix
Restore the structure: one XML declaration, one resources root, and complete elements inside it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My App</string>
</resources>3Verify
AAPT2 compiles the file without any output.
mkdir -p compiled
aapt2 compile app/src/main/res/values/strings.xml -o compiled/Confirmed by w3.org
How do I stop it coming back?
- Run a debug build before every push that touches a strings.xml, and after every merge of translation files.
- Give translators a short note: apostrophes and double quotes need a backslash, an ampersand is written &.
- Keep one string per line, so the line number in the error tells you the string straight away.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
Android build stops with 'resource linking failed' from AAPT2
AAPT2 aapt2-x.x.x-xxxxxxx-linux Daemon #0: Command failed
AAPT2 links your XML resources and manifest into one package, and it fails on the first thing it cannot resolve.
3 causes, each with a fixMobile apps
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile apps
Android Studio cannot pair the phone over Wi-Fi
The Pair devices over Wi-Fi dialog lists no device
Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.
4 causes, each with a fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fix