为什么将存储库添加到 build.gradle 会使其找不到依赖项?

oli*_*ren 3 android gradle maven

我看到 jCenter 将在遥远的将来退役,并且 Gradle 警告要求人们停止使用它。因此我开始了过渡,并且由于我发现简单地替换jcenter()mavenCentral()会导致缺少依赖项,因此我选择在 JCenter 之前添加 Maven Central 作为第一次启动。

AFAIK,Gradle 将按顺序访问 Maven 存储库:google、mavencentral、jcenter。但发生的情况是,当我添加它时,Gradle 无法找到依赖项:

$ time ./gradlew build --stacktrace
FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:compileReleaseKotlin'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Could not find :unspecified:.
     Required by:
         project :app > project :cameramodule > id.zelory:compressor:2.1.0

...
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
...
Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not find :unspecified:.
Required by:
    project :app > project :cameramodule > id.zelory:compressor:2.1.0


* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 15s
Run Code Online (Sandbox Code Playgroud)

差异

Date:   Thu Sep 9 01:46:58 2021 +0200

    Prever Maven Central over JCenter repos

diff --git a/build.gradle b/build.gradle
index 6dd00f0..533e262 100644
--- a/build.gradle
+++ b/build.gradle
@@ -4,8 +4,8 @@ buildscript {
     ext.kotlin_version = '1.3.72'
     repositories {
         google()
-        jcenter()
-
+        mavenCentral()
+        jcenter() // read-only as of March 2021
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:4.2.0'
@@ -25,8 +25,8 @@ buildscript {
 allprojects {
     repositories {
         google()
-        jcenter()
-
+        mavenCentral()
+        jcenter() // read-only as of March 2021
     }
 }
Run Code Online (Sandbox Code Playgroud)

Shl*_*iel 6

id.zelory:compressor:2.1.0 从mavencentral查看.pom文件,发现如下依赖:

<dependency>
    <groupId/>
        <artifactId>unspecified</artifactId>
    <version/>
</dependency>
Run Code Online (Sandbox Code Playgroud)

.pom链接: https://repo1.maven.org/maven2/id/zelory/compressor/2.1.0/compressor-2.1.0.pom

这种依赖关系是未解决的:unspecified:,导致构建失败。

显然,这种依赖关系没有任何意义,并且发布脚本可能会生成错误的 pom.xml。

您应该实现压缩库并排除奇怪的:unspecified:依赖项:

implementation('id.zelory:compressor:2.1.0') {
    exclude module: 'unspecified'
}
Run Code Online (Sandbox Code Playgroud)

编辑:压缩机的 GitHub 项目有一个问题,表明迁移到 mavencentral 更改了 .pom 文件: https://github.com/zetbaitsu/compressor/issues/176

编辑2:显然它已在版本2.1.1中修复: https: //github.com/zetbaitsu/Compressor/issues/179