Android Material Design示例代码出现缺少资源错误

18
我正在尝试运行来自以下链接的 Android Material Design:https://github.com/material-components/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>'

6个回答

28

当我升级到这个版本时,我遇到了同样的错误。

implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.8.0-alpha01'

但在工作方面还是很好的

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'

奇怪的是这个问题还没有被解决。这似乎是唯一的解决方案。 - James Wahome

11

使用当前版本的Android Studio(从4.2更新到2021.2.1并使用推荐的Gradle插件)后,问题得以解决。

当我为同样的问题打开缺陷时,从GitHub获得以下回复:“您附加的错误消息说无法识别资源标记,这需要AGP 7.2。您必须使用比1.7.0-alpha02更高的版本,请确保您的应用程序构建与最低所需的插件版本一起使用。”


1
在更新到Android Studio Dolphin和AGP 7.3.1之后,问题得到了解决。 - Gopal Kohli
仍然无法工作。在提供distributionUrl=https://services.gradle.org/distributions/gradle-7.4-bin.zip之后。 - Priyanka

3

替换:

implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'

使用:

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'

build.gradle(:app)文件中的dependencies { }下,涉及IT技术相关内容。

2

将Android Gradle插件更新至7.3.3 (gradle-7.3.3-bin.zip)

一切将会没问题。


1

这个问题应该与那些深入挖掘并且不直接使用这些依赖的人有关,因为在Android Studio中构建和运行应用程序时它可以正常工作。

看起来材料1.7.0正在使用非法标签<macro>,当您查看它们的发布更改时,特别是**/tokens.xml文件。我在AGP文档中找不到任何关于此标签的信息,我从this git repo找到的唯一参考是代码的这一部分。

private fun parseMacro(
    element: StartElement, eventReader: XMLEventReader, parsedResource: ParsedResource): Boolean {
...
  // Macros can only be defined in the default config
  val defaultConfig = ConfigDescription()
  if (parsedResource.config != defaultConfig) {
    logError(
      blameSource(source, element.location),
      "<macro> tags cannot be declared in configurations other than the default configuration")
      return false
    }

所以我不确定他们是如何解决这个问题的。 在未来,AGP有可能会在其文档中支持这个标签,并将其作为外部支持,但目前您需要使用解决方法。


1

请尝试在build.gradle(app)中降级Material Design库。

我已经进行了更改。

implementation 'com.google.android.material:material:1.7.0'

为了

implementation 'com.google.android.material:material:1.6.0'

这解决了我的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接