如何解决Android中「Illegal char <:> at index 37」资源合并错误
解决attrs.xml中莫名出现<:>字符的编译错误
以下是项目中的attrs.xml内容:
<declare-styleable name="WeekdaysPicker"> <attr name="enabled" format="boolean" /> <attr name="highlight_color" format="color" /> <attr name="text_unselected_color" format="color" /> <attr name="sunday_first_day" format="boolean" /> <attr name="show_weekend" format="boolean" /> <attr name="week_end_darker" format="boolean" /> <attr name="weekend_color" format="color" /> <attr name="weekend_text_color" format="color" /> <attr name="full_size" format="boolean" /> <attr name="recurrence" format="boolean" /> <attr name="border_color" format="color" /> <attr name="border_thickness" format="integer" /> <attr name="border_highlight_color" format="color" /> <attr name="border_highlight_thickness" format="integer" /> </declare-styleable> <attr name="text_color" format="color" /> <attr name="button_text_color" format="reference" /> <attr name="button_background_color" format="reference" /> <attr name="button_disabled_background_color" format="reference" /> <attr name="background_color" format="reference" /> <attr name="input_layout_background_color" format="reference" /> <attr name="icon_color" format="reference" />
项目中未发现<:>字符,已尝试Invalidate Caches and Restart、修改attr属性名,但均无法解决该错误。
可行的排查与解决方法
- 检查其他资源文件语法:错误提示指向attrs.xml,但实际问题可能出在其他XML文件(如layout、style)中,比如属性引用时使用了错误的命名空间、拼写错误,或者格式不规范,导致解析器抛出异常。
- 手动清理构建缓存:直接删除项目根目录下的
build文件夹和app/build文件夹,再执行Clean Project和Rebuild Project,比IDE内置的缓存清理更彻底。 - 排查第三方依赖冲突:如果近期添加了新的依赖库,尝试临时移除该依赖,看错误是否消失,排查是否存在属性名或资源冲突。
- 检查属性重复定义:确认其他module或第三方库中是否存在同名属性,重复的属性定义会在资源合并阶段引发异常。
- 验证文件编码:确保attrs.xml文件的编码为UTF-8,非标准编码可能导致XML解析异常。
- 重建attrs.xml文件:将当前内容复制到记事本,删除原文件后新建attrs.xml,再粘贴内容并同步项目,避免文件本身存在隐藏的格式问题。
内容的提问来源于stack exchange,提问作者mohamadjamous
相关产品推荐
相关产品推荐

