Skip to content

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

xml
<color name="colorPrimary">EAF5FF</color>
<color name="colorSecondary">##EAF5FF</color>
<color name="colorTertiary">###EAF5FF</color>
xml
<color name="colorPrimary">#EAF5FF</color>
<color name="colorSecondary">#EAF5FF</color>
<color name="colorTertiary">#EAF5FF</color>

Malformed Color Values

xml
<color name="colorError">#EAF5FFC</color> <!-- 7 characters -->
<color name="colorError">"#ff0000"</color> <!-- Quotes around value -->
xml
<color name="colorError">#EAF5FF</color> <!-- 6 characters -->
<color name="colorError">#ff0000</color> <!-- No quotes -->

Incorrect Dimension Units

xml
<dimen name="spacing_small">10p</dimen>
<dimen name="text_size">14s</dimen>
xml
<dimen name="spacing_small">10dp</dimen>
<dimen name="text_size">14sp</dimen>

2. Character Escaping Issues

Apostrophes in Strings

xml
<string name="message">Don't do that</string>
<string name="description">The dog's toy</string>
xml
<string name="message">Don\'t do that</string>
<string name="description">The dog\'s toy</string>

Exclamation Marks in Strings

xml
<string name="warning">Danger! High voltage</string>
<string name="excited">Wow! That's amazing</string>
xml
<string name="warning">Danger\! High voltage</string>
<string name="excited">Wow\! That\'s amazing</string>

3. XML Structural Issues

Nested Style Definitions

xml
<style name="Theme.MyApp">
    <style name="SubStyle"> <!-- Nested style -->
        <item name="colorPrimary">@color/primary</item>
    </style>
</style>
xml
<style name="Theme.MyApp">
    <!-- Parent style -->
</style>

<style name="SubStyle"> <!-- Separate style -->
    <item name="colorPrimary">@color/primary</item>
</style>

Invalid ID Definitions

xml
<item name="my_button" type="id">my_button</item> <!-- Value not allowed for ids -->
xml
<item name="my_button" type="id" /> <!-- No value for ids -->

Extra Characters in Tags

xml
<color name="colorPrimary">>#D2070E</color> <!-- Extra > character -->
xml
<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:

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

  1. Open the Build tab in Android Studio
  2. Click on :app:mergeDebugResources (displays error count)
  3. Look for blue links with red error messages
  4. 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:

bash
./gradlew clean
./gradlew build

Prevention Best Practices

  1. Validate XML Resources: Regularly use Android Studio's XML validation tool
  2. Code Review: Have team members review XML changes
  3. Version Control: Use Git to track changes and easily revert problematic edits
  4. Consistent Formatting: Establish and follow XML formatting standards
  5. 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.