Android 开发问题:Invalid <color> for given resource value.

  • 在 Android 开发中,构建项目时,出现如下错误信息

    Task :appbluetooth:mergeDebugResources FAILED
    D:\AndroidCode\DeviceController\smallscreen\src\main\res\values\colors.xml:11:4: Invalid for given resource value.

    Execution failed for task ':appbluetooth:mergeDebugResources'.

    A failure occurred while executing com.android.build.gradle.internal.res.ResourceCompilerRunnable
    Resource compilation failed (Failed to compile values resource file D:\AndroidCode\DeviceController\smallscreen\build\intermediates\incremental\debug\mergeDebugResources\merged.dir\values\values.xml. Cause: java.lang.IllegalStateException: Can not extract resource from com.android.aaptcompiler.ParsedResource@708b155a.). Check logs for more details.

问题原因
  1. 这个错误表示在 colors.xml 文件中有一个无效的颜色值,导致在处理 values.xml 资源文件时出现了资源编译错误

  2. values.xml 文件通常不是由开发者直接创建和维护的,是由 Android Gradle 插件(AGP)在编译过程中自动生成的,它是一个合并了所有资源值的总文件

  • Android 颜色值格式如下
格式 示例
#RGB #FFF
#ARGB #FFFF
#RRGGBB #FF9811
#AARRGGBB #FFFF9811
占位符 说明
R
G 绿
B
A 透明度