首页 > 解决方案 > 导致项目其余部分降级到 guava android 的真相扩展

问题描述

如果我将com.google.truth.extensions:truth-proto-extension:1.1jar 添加到我的 bazel 工作区,它似乎完全从 中删除类com.google.guava:guava:28.2-jre,导致错误,例如

import static com.google.common.collect.ImmutableMap.toImmutableMap;
^
  symbol:   static toImmutableMap
  location: class ImmutableMap
java/com/google/fhir/protogen/ProtoGenerator.java:316: error: cannot find symbol
            .collect(toImmutableMap(def -> def.getId().getValue(), def -> def));
                     ^
  symbol:   method toImmutableMap((def)->def[...]lue(),(def)->def)
  location: class ProtoGenerator

你的文件说

One warning: Truth depends on the “Android” version of Guava, a subset of the “JRE” version.
If your project uses the JRE version, be aware that your build system might select the Android version instead.
If so, you may see “missing symbol” errors.
The easiest fix is usually to add a direct dependency on the newest JRE version of Guava.

这是否意味着除了 maven dep on 之外的任何东西com.google.guava:guava:28.2-jre?如果没有,下一个最简单的解决方法是什么?

标签: google-truth

解决方案


这里的关键词是“最新”:您需要依赖(在撰写本文时)30.1-jre。我编辑了文档以强调这一点

(您可以在各个位置看到最新版本,包括:Maven CentralMaven Central SearchGuava GitHub 页面。)

问题是:

  • 一些工具(包括 Gradle 以及maven_install来自 Bazel's 的规则rules_jvm_external)在您的传递依赖项中找到的所有版本中选择任何给定工件的“最新”版本。
  • Truth 1.1取决于版本 30.0-android
  • 30.0-android 被认为比 28.2-jre“更新”(因为 30 大于 28)。
  • -android 版本缺少 Java 8 API。

(因此,您实际上可以通过依赖30.0-jre 以上的任何-jre 版本来解决此问题:由于字母顺序,30.0-jre 被认为比 30.0-android“更新”。有趣!)

不幸的是,Maven 生态系统不支持为每个版本(JRE+Android)提供两种“风格”的好方法。(人们经常建议使用 Maven“分类器”,但这并不能真正解决问题。)

为未来:

  • Gradle:Gradle正在与我们合作提供自己的解决方案,但还没有完全准备好。
  • Maven:Maven 不太可能提供帮助。(它甚至不会尝试选择“最新”版本,更不用说支持“口味”了。)
  • Bazel:我不知道rules_jvm_external(使用Coursier的)是否有计划支持“口味”。(编辑一下:在理想的世界中,我宁愿自己指定我的所有 repo 的传递依赖项及其版本,而不是让构建系统尝试为我解决它。这有助于避免像这样的意外。但这会带来它自己的挑战,我们只是在我们自己的基于 Bazel 的项目中为解决这些问题做出了渐进的努力。)

推荐阅读