Android Resource Compilation Error: Can not extract resource from com.android.aaptcompiler
This error occurs when the Android resource compiler (aapt2) encounters malformed or invalid XML resources during the build process. The error message indicates a failure in the mergeDebugResources
task, specifically when compiling values resource files.
Problem Overview
The error typically manifests as:
Execution failed for task ':app:mergeDebugResources'
> Resource compilation failed (Failed to compile values resource file .../values.xml)
Cause: java.lang.IllegalStateException: Can not extract resource from com.android.aaptcompiler.ParsedResource@...
This indicates that one or more XML resource files contain syntax errors, malformed values, or structural issues that prevent proper compilation.
Common Causes and Solutions
1. XML Syntax and Formatting Issues
Missing or Extra Hash Symbols in Colors
<color name="colorPrimary">EAF5FF</color>
<color name="colorSecondary">##EAF5FF</color>
<color name="colorTertiary">###EAF5FF</color>
<color name="colorPrimary">#EAF5FF</color>
<color name="colorSecondary">#EAF5FF</color>
<color name="colorTertiary">#EAF5FF</color>
Malformed Color Values
<color name="colorError">#EAF5FFC</color> <!-- 7 characters -->
<color name="colorError">"#ff0000"</color> <!-- Quotes around value -->
<color name="colorError">#EAF5FF</color> <!-- 6 characters -->
<color name="colorError">#ff0000</color> <!-- No quotes -->
Incorrect Dimension Units
<dimen name="spacing_small">10p</dimen>
<dimen name="text_size">14s</dimen>
<dimen name="spacing_small">10dp</dimen>
<dimen name="text_size">14sp</dimen>
2. Character Escaping Issues
Apostrophes in Strings
<string name="message">Don't do that</string>
<string name="description">The dog's toy</string>
<string name="message">Don\'t do that</string>
<string name="description">The dog\'s toy</string>
Exclamation Marks in Strings
<string name="warning">Danger! High voltage</string>
<string name="excited">Wow! That's amazing</string>
<string name="warning">Danger\! High voltage</string>
<string name="excited">Wow\! That\'s amazing</string>
3. XML Structural Issues
Nested Style Definitions
<style name="Theme.MyApp">
<style name="SubStyle"> <!-- Nested style -->
<item name="colorPrimary">@color/primary</item>
</style>
</style>
<style name="Theme.MyApp">
<!-- Parent style -->
</style>
<style name="SubStyle"> <!-- Separate style -->
<item name="colorPrimary">@color/primary</item>
</style>
Invalid ID Definitions
<item name="my_button" type="id">my_button</item> <!-- Value not allowed for ids -->
<item name="my_button" type="id" /> <!-- No value for ids -->
Extra Characters in Tags
<color name="colorPrimary">>#D2070E</color> <!-- Extra > character -->
<color name="colorPrimary">#D2070E</color>
4. File and Configuration Issues
Unnecessary XML Files
Delete any accidentally created or unnecessary XML files in the res/values
directory, particularly:
ids.xml
files that weren't intentionally created- Duplicate theme files
- Test or temporary XML files
Enable Jetifier for Legacy Libraries
Add to your gradle.properties
file:
android.enableJetifier=true
This helps resolve compatibility issues with older libraries that may contain problematic resources.
Debugging Techniques
1. Review Build Output
Check the Build tab in Android Studio for detailed error messages:
- Open the Build tab in Android Studio
- Click on
:app:mergeDebugResources
(displays error count) - Look for blue links with red error messages
- Click links to navigate to problematic files
2. Validate XML Files
Use Android Studio's XML validation:
- Right-click on XML files
- Select "XML Actions" → "Validate"
- Fix any reported issues
3. Check Recent Changes
The error often occurs after recent XML modifications:
- Review recently edited resource files
- Use version control to identify changes
- Consider reverting recent edits to isolate the problem
4. Clean and Rebuild
Perform a clean build to eliminate cached errors:
./gradlew clean
./gradlew build
Prevention Best Practices
- Validate XML Resources: Regularly use Android Studio's XML validation tool
- Code Review: Have team members review XML changes
- Version Control: Use Git to track changes and easily revert problematic edits
- Consistent Formatting: Establish and follow XML formatting standards
- Testing: Test builds after significant resource modifications
WARNING
Always backup your project before making bulk changes to resource files, especially when using find-and-replace operations for character escaping.
Conclusion
The "Can not extract resource from com.android.aaptcompiler" error typically stems from malformed XML resources. By systematically checking color values, string escaping, XML structure, and file organization, you can identify and resolve the underlying issue. Use the debugging techniques outlined above to efficiently locate and fix problematic resources in your Android project.