无法解析 com.android.tools.build:gradle 没有匹配的变体

Sag*_*lly 4 java android compiler-errors build gradle

我的主要目标是创建一个新的应用程序,只有一个空活动:没有花哨的东西。然而,在尝试成为一名优秀的开发人员并升级 gradle 之后,就像 IDE 所说的那样,一切都出了问题。

什么都不会再构建了,它找不到我的 JDK,无论我把它放在哪里,它都无法编辑 gradle 依赖项锁定文件:整个九个。我设法通过移动所有内容来解决 JDK 和 gradle 编写问题,但我留下了最后一个问题:

> No matching variant of com.android.tools.build:gradle:7.4.1 was found. 
        The consumer was configured to find a runtime of a library compatible with Java 8,
        packaged as a jar, and its dependencies declared externally, as well as
        attribute 'org.gradle.plugin.api-version' with value '7.5' but:
Run Code Online (Sandbox Code Playgroud)

现在,这还不是完整的错误。完整的错误是一堵倒塌的文字墙。但是,我可以稍微减少它,因为这个错误是递归的:

它开头说:

A problem occurred configuring root project 'app'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:7.4.1.
     Required by:
         project : > com.android.application:com.android.application.gradle.plugin:7.4.1
         project : > com.android.library:com.android.library.gradle.plugin:7.4.1
Run Code Online (Sandbox Code Playgroud)

之后它列出了一些以特定能力使用 gradle v.7.4.1 的事情,但随后出现了最初的问题。

看起来它正在尝试使用 7.4.1,但一直在寻找 7.5,但我不明白其余的。然后记录重复错误,并集中在一些关键元素(或“变体”,因为它们是),这些元素是:

  1. 'api元素'
  2. 'javadocElements'
  3. '运行时元素',和
  4. '来源元素'

对于每一个,它都说了这样的话:

- Variant <insert variant here> capability com.android.tools.build:gradle:7.4.1 declares
            a library, packaged as a jar, and its dependencies declared externally:
Run Code Online (Sandbox Code Playgroud)

然后,对于每一个,它列出了......“原因”,为什么“但是:”必须在那里,以下是父原因:

 component compatible with Java 11 and the consumer needed a runtime of a component
 compatible with Java 8
Run Code Online (Sandbox Code Playgroud)

这对我来说没有意义,因为我应该使用 Java... 19,但也许 19 只是构建版本,而不是广告版本(?)...

“其他兼容属性:”子原因包含以下一些内容,我将使用列表编号来简化:

所有变体都有这个:

- Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
Run Code Online (Sandbox Code Playgroud)

变体 2 和变体 4 都具有以下特征:

- Doesn't say anything about its target Java version (required compatibility with Java 8)

- Doesn't say anything about its elements (required them packaged as a jar)
Run Code Online (Sandbox Code Playgroud)

现在,我想让你知道,是的,我一开始确实对设置进行了修改,但是当我发现我没有做得更好时,我卸载了整个 IDE,包括我的用户设置,并做了一个新的设置。下载并安装最新版本的 IDE。我检查了 .android 文件夹,发现 .lib 文件夹消失了,所以也许它有效(???),但我不确定,因为它打开了一个旧项目。我没有删除任何它想看到的东西。

我想使用这里另一篇文章中的这张图片,我也弄乱了它,试图通过将 7.4.1 更改为 7.5 并将它们设置为“true”来强制它使用 7.5。它不起作用,所以我把它放回去了。希望这些信息足以满足原始条件:

  1. 我给了你它应该做的事情(只需编译:我要求不多)
  2. 我给了你错误(gradle 一路崩溃)
  3. 我给出了最短的代码(没有任何代码......因为这是一个 gradle 问题)

也许它需要转移到不同的子SO:我不知道,但我在这里尽力了。另外,如果以上所有内容都是完全令人厌恶的,那么我将大部分原始错误原封不动地留在下面。然而,就像我说的,有一个更压缩的版本,可以走得更远,但是......我不认为你想要查看 150 多行错误。

...

但是,我可以给你顶级错误:

org.gradle.api.ProjectConfigurationException: A problem occurred configuring root project 'app'

...这只是说同样的事情。

          - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.1 declares
            a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.1 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.1 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.1 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
Run Code Online (Sandbox Code Playgroud)

小智 5

在 AndroidStudio Flamingo 2022.2.1 中,我打开“文件”->“设置”菜单,然后选择

Build, Execution, Deployment
  Build Tools
    Gradle
Run Code Online (Sandbox Code Playgroud)

我选择了当前的项目,并在“Gradle JDK”字段中选择了“Embedded JDK”(恰好是 v17.0.6)。单击“确定”关闭设置,然后单击 Gradle 同步失败通知中的“重试”。