Gradle: 在顶层和子模块中声明依赖 - 使用哪个?

Gradle: Declaring dependency in top level and sub module- which is used?

当在顶层build.gradle和子模块build.gradle中声明了不同的versions依赖时,哪个优先?

例如,如果在我的顶层 build.gradle 我有 junit:junit:4.8.2 但在子模块中有 junit:junit:4.10 ?

此外,在顶层 build.gradle 中声明依赖项有什么作用?是否应该只在子模块中声明所有依赖项?

拥有

// root build.gradle
subprojects {
    dependencies {
        compile "junit:junit:4.10"
    }
}

// submodule build.gradle
dependencies {
    compile "junit:junit:4.8.2"
}

本质上与

相同
// submodule build.gradle
dependencies {
    compile "junit:junit:4.8.2"
    compile "junit:junit:4.10"
}

这意味着您在子模块中声明了两个版本的 junit。现在 Gradle 冲突解决开始了。默认分辨率为 依赖冲突是选择较新的版本。所以 junit 4.10 将被选中。要更改此行为,您可以配置解析策略。