How can I manage dependency versions for sub-projects?


(Arian Treffer) #1

I know how I can add a dependency to all my subprojects. However, I want the dependency only added for projects that actually need it. Yet, I also want to configure the version in one single place. What is the best way to do so?


(Peter Niederwieser) #2

A common idiom is to describe dependencies in the root project, and apply them selectively in subprojects. For example:

build.gradle

ext.libs = [
    junit: "junit:junit:4.11",
    spring_core: "org.springframework:spring-core:3.1.0.RELEASE"
  ]

child/build.gradle

dependencies {
    compile libs.spring_core
    testCompile libs.junit
}

Other values that you can use in the ‘ext.libs’ map are lists of strings and values created with ‘dependencies.create’. The latter allows you to use the full dependency notation as known from the ‘dependencies’ block. For details see the DependencyHandler API.