gradle依赖项失败,原因未知(可能是元数据中的变量选择?)

ecfsfe2w  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(123)

我创建了一个空白项目:

  1. mkdir gradleExp
  2. cd gradleExp
  3. gradle init#1.basic 1.groovy随机名称
    1.使用以下内容更新空的build.gradle
plugins {
    id 'java'
}
repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation "androidx.browser:browser:1.4.0"
}

然后我就跑

dependencies --configuration compileClasspath

它返回了一个失败:

但是如果我用androidx.browser:browser:1.0.0替换build.gradle中的androidx.browser:browser:1.4.0,那么它就可以完美地工作:

androidx.browser:browser:1.4.0有什么问题?如何知道失败的确切原因?我的gradle版本是7.5.1,我从https://gradle.org/下载了它
注意:如果可以的话,用--debug参数我可以看到:
[调试] [组织.gradle.内部.组件.模型.组件属性匹配器]从候选项中[]为{组织.gradle.类别=库,组织. gradle.依赖性.绑定=外部,组织. gradle.jvm.环境=标准jvm,组织. gradle. jvm.版本=8,组织. gradle.库元素=类,组织. gradle.用法=java-api}选择了匹配项[][androidx.浏览器:浏览器:1.4.0变体版本变体发布变体发布变体发布ApiPublication,androidx.浏览器:浏览器:1.4.0变体发布变体发布运行时发布,androidx.浏览器:浏览器:1.4.0变体源元素]
我不知道问题是否出在这里,但看起来Gradle无法选择变体(即:日志中的Selected matches []
这是gradle-module-metadata的内容,如果它可以帮助:

{
  "formatVersion": "1.1",
  "component": {
    "group": "androidx.browser",
    "module": "browser",
    "version": "1.4.0",
    "attributes": {
      "org.gradle.status": "release"
    }
  },
  "createdBy": {
    "gradle": {
      "version": "7.2"
    }
  },
  "variants": [
    {
      "name": "releaseVariantReleaseApiPublication",
      "attributes": {
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.libraryelements": "aar",
        "org.gradle.usage": "java-api"
      },
      "dependencies": [
        {
          "group": "androidx.core",
          "module": "core",
          "version": {
            "requires": "1.1.0"
          }
        },
        {
          "group": "androidx.annotation",
          "module": "annotation",
          "version": {
            "requires": "1.1.0"
          }
        },
        {
          "group": "com.google.guava",
          "module": "listenablefuture",
          "version": {
            "requires": "1.0"
          }
        }
      ],
      "files": [
        {
          "name": "browser-1.4.0.aar",
          "url": "browser-1.4.0.aar",
          "size": 203214,
          "sha512": "5fabb554ca4dbea2c367df0decf175fee0a90bbdd7cc641c645b80a26e8bd13e85d5031c51bfbab25582a07f66e3eee829fadfe8b2e4e3383c4d877ce37935b2",
          "sha256": "e1906f84512f1ea5e4c926dbb915789ad8f7494fa4ee9ad245163f76185cecda",
          "sha1": "403c3cf31622ec0c70b3c0633be3e903c8062104",
          "md5": "8b6dfac591dd4b320351218bae97040e"
        }
      ]
    },
    {
      "name": "releaseVariantReleaseRuntimePublication",
      "attributes": {
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.libraryelements": "aar",
        "org.gradle.usage": "java-runtime"
      },
      "dependencies": [
        {
          "group": "androidx.collection",
          "module": "collection",
          "version": {
            "requires": "1.1.0"
          }
        },
        {
          "group": "androidx.concurrent",
          "module": "concurrent-futures",
          "version": {
            "requires": "1.0.0"
          }
        },
        {
          "group": "androidx.interpolator",
          "module": "interpolator",
          "version": {
            "requires": "1.0.0"
          }
        },
        {
          "group": "androidx.core",
          "module": "core",
          "version": {
            "requires": "1.1.0"
          }
        },
        {
          "group": "androidx.annotation",
          "module": "annotation",
          "version": {
            "requires": "1.1.0"
          }
        },
        {
          "group": "com.google.guava",
          "module": "listenablefuture",
          "version": {
            "requires": "1.0"
          }
        }
      ],
      "files": [
        {
          "name": "browser-1.4.0.aar",
          "url": "browser-1.4.0.aar",
          "size": 203214,
          "sha512": "5fabb554ca4dbea2c367df0decf175fee0a90bbdd7cc641c645b80a26e8bd13e85d5031c51bfbab25582a07f66e3eee829fadfe8b2e4e3383c4d877ce37935b2",
          "sha256": "e1906f84512f1ea5e4c926dbb915789ad8f7494fa4ee9ad245163f76185cecda",
          "sha1": "403c3cf31622ec0c70b3c0633be3e903c8062104",
          "md5": "8b6dfac591dd4b320351218bae97040e"
        }
      ]
    },
    {
      "name": "sourcesElements",
      "attributes": {
        "org.gradle.category": "documentation",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.docstype": "sources",
        "org.gradle.usage": "java-runtime"
      },
      "files": [
        {
          "name": "browser-1.4.0-sources.jar",
          "url": "browser-1.4.0-sources.jar",
          "size": 96524,
          "sha512": "cd150044fd65d22c61f9d724a18b6a7386e4aed98644b146ffef0447b4f6f93044cb9fe58590f6111d33723733abc34039896a88a974ab0e465d05534294b101",
          "sha256": "3acbcce900ca5c2d03eb70d93136fc732d3b64dec185eaa86de3e64dff8ee792",
          "sha1": "2a2741f63427dcdb3b6821dd17703bc16ced13a1",
          "md5": "9eed77c8c21aeed4da0477c8bc6b72b9"
        }
      ]
    }
  ]
}
68bkxrlz

68bkxrlz1#

即使是androidx.browser:browser:1.2.0也会工作得很好,就像预期的那样。
这里是问题,在androidx.browser:browser:1.2.01.0.0中,正如你提到的,输出完全符合预期。为什么?
对于版本1.2.0和1.0.0,只有两种类型的工件pomarr,它们不会导致解析问题。
对于版本1.4.0,有4种类型,分别为sourceaargradle-module-metadatapom
在处理Gradle时,您只能假设最优先考虑的是gradle-module-metadata,根据这里的官方文档,这实际上是正确的。
很明显,解析gradle-module-metadata时存在问题,因此让我尝试重新配置顺序
根据Gradle文档,您可以通过以下方式轻松实现

repositories {
    maven {
        url "http://repo.mycompany.com/repo"
        metadataSources {
            mavenPom()
            artifact()
        }
    }
}

所以repositories部分的代码应该如下所示

repositories {
    
    mavenCentral()
    { metadataSources {
        mavenPom()
        }
    }
    google()
    { metadataSources {
        mavenPom()
        }
    }
}

这将导致他们忽略gradle-module-metadata并优先考虑pom.xml
由于某些原因,这将不会按预期的方式运行,并且它不会工作,除非我添加了configurations.implementation.canBeResolved = true
整个build.gradle看起来像

plugins {

    id 'java'
}

repositories {
    
    mavenCentral()
    { metadataSources {
        mavenPom()
        }
    }
    google()
    { metadataSources {
        mavenPom()
        }
    }
}

configurations.implementation.canBeResolved = true

dependencies {
    implementation 'androidx.browser:browser:1.4.0'
}

结果将仅显示使用gradle dependencies命令或gradle dependencies --configuration implementation的实现部分的依赖关系,以获得有限的输出。

相关问题