运行Material Design Android示例代码时出现资源解析错误
解决Material Components Android资源打包错误(标签类型无法识别)
问题描述
运行从Material Components Android仓库获取的示例代码时,出现资源打包失败,错误信息如下:
failed :lib:packageDebugResources tokens.xml Can't determine type for tag '<macro name="m3_comp_bottom_app_bar_container_color">?attr/colorSurface</macro>' tokens.xml Can't determine type for tag '<macro name="m3_sys_color_dark_surface_tint">?attr/colorPrimary</macro>' fab_tokens.xml Can't determine type for tag '<macro name="m3_comp_fab_primary_container_color">?attr/colorPrimaryContainer</macro>' tokens.xml Can't determine type for tag '<macro name="m3_comp_switch_selected_icon_color">?attr/colorOnPrimaryContainer</macro>' tokens.xml Can't determine type for tag '<macro name="m3_sys_motion_path">linear</macro>' tokens.xml Can't determine type for tag '<macro name="m3_sys_shape_corner_full_family">rounded</macro>' tokens.xml Can't determine type for tag '<macro name="m3_ref_typeface_brand_regular">sans-serif</macro>' /Users/mac/AndroidStudioProjects/material-components-android/lib/java/com/google/android/material/bottomappbar/res/values/tokens.xml: Error: Can't determine type for tag '<macro name="m3_comp_bottom_app_bar_container_color">?attr/colorSurface</macro>'
解决方案
这些错误是因为Android Gradle Plugin(AGP)版本过低,不支持Material 3中引入的<macro>资源标签。按以下步骤修复:
- 升级项目根目录下
build.gradle(或build.gradle.kts)中的AGP版本到7.0.0及以上:dependencies { classpath 'com.android.tools.build:gradle:7.0.0' // 或更高稳定版本,比如7.4.2、8.0.2等 } - 同步项目后,重新运行构建。
如果升级AGP后仍有问题,检查以下几点:
- 确保项目的
compileSdkVersion设置为31及以上(Material 3最低要求) - 确认Material Components库的版本与仓库代码版本匹配,避免版本冲突
内容的提问来源于stack exchange,提问作者neerajt
相关产品推荐
相关产品推荐

