【问题标题】:What's the use of constraint in Gradle dependencies resolution?Gradle 依赖项解析中的约束有什么用?
【发布时间】:2021-10-15 19:04:29
【问题描述】:

有一个库,即io.github.elye:simplekotlinlibrary,版本为 1.0.0、2.0.0 和 3.0.0。

io.github.elye:easyandroidlibrary:1.0.0 包含io.github.elye:simplekotlinlibrary:1.0.0

io.github.elye:simpleandroidlibrary:2.0.0 包含io.github.elye:simplekotlinlibrary:2.0.0

所以在我的 Gradle 中,如果我有的话

    implementation ('io.github.elye:easyandroidlibrary:1.0.0')
    implementation ('io.github.elye:simpleandroidlibrary:2.0.0')
    implementation ('io.github.elye:simplekotlinlibrary:3.0.0')

对于包含传递依赖项的项目,它将自动解析为io.github.elye:simplekotlinlibrary:3.0.0

但是,对于https://docs.gradle.org/current/userguide/dependency_constraints.html#sec:adding-constraints-transitive-deps,它声明constrains 还会强制将依赖项升级到指定的版本3.0.0

    implementation ('io.github.elye:easyandroidlibrary:1.0.0')
    implementation ('io.github.elye:simpleandroidlibrary:2.0.0')
    implementation ('io.github.elye:simplekotlinlibrary')

    constraints {
        implementation('io.github.elye:simplekotlinlibrary:3.0.0') {
            because 'testing force upgrade'
        }
    }

根据我的理解,以上两个例子会产生相同的结果。好像constraint 没有实际用处? constraint 与不同的自动依赖解析有何不同?

【问题讨论】:

    标签: gradle dependencies constraints gradle-dependencies


    【解决方案1】:

    我认为在上面的例子中它们是相同的。

    但是,如果顶级项目不需要io.github.elye:simplekotlinlibrary,使用constraint,仍然可以强制所有传递依赖项至少由@987654324 指示的版本@如下图。

        implementation ('io.github.elye:easyandroidlibrary:1.0.0')
        implementation ('io.github.elye:simpleandroidlibrary:2.0.0')
    
        constraints {
            implementation('io.github.elye:simplekotlinlibrary:3.0.0') {
                because 'testing force upgrade'
            }
        }
    

    可以找到更详细的说明here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      相关资源
      最近更新 更多