1

In app-level build.gradle, I often include the following dependencies

dependencies {
  implementation "androidx.appcompat:appcompat:$appCompatVersion"
  implementation "androidx.cardview:cardview:$cardVersion"
  implementation "com.google.android.material:material:$materialVersion"
  implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
  implementation "androidx.annotation:annotation:$androidXAnnotations"
  ...
}

It makes that file becomes longer and longer, thus I think about moving all such dependencies into project-level build.gradle, such as (just example):

ext {
  includeUnitTestDeps() {
    implementation "androidx.appcompat:appcompat:$appCompatVersion"
    implementation "androidx.cardview:cardview:$cardVersion"
    implementation "com.google.android.material:material:$materialVersion"
    implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
    implementation "androidx.annotation:annotation:$androidXAnnotations"
  }
}

(The reason why to project-level but not app-level because we might have multiple modules in project, thus project-level is the best places)

Then in app-level build.gradle we call

dependencies {
  ext.includeUnitTestDeps()
  ...
}

Note: I'm not so familiar with Groovy/Gradle syntax, therefore not sure it works (In fact I tried but it doesn't allow to define such method in ext). But if you know any solution, please help me. Thanks so much.

Boken
  • 4,825
  • 10
  • 32
  • 42
anticafe
  • 6,816
  • 9
  • 43
  • 74

1 Answers1

3

There are couple of ways that I can think of

1 - Add dependencies to all sub projects (in the parent)

subprojects {
  dependencies {
     implementation 'com.google.guava:guava:23.0'
     testImplementation 'junit:junit:4.12'
  }
  ....
}

2 - Add dependencies to specific sub projects (in the parent)

// this will add dependencies to project a, b, and c
// when You add a new subproject, You have to add it to here also 
// If You these need dependencies available in it
configure([project(':a'), project(':b'), project(':c')]) {
   dependencies {
      implementation 'com.google.guava:guava:23.0'
      .....
   }
}

3 - Using a method to add dependencies

//in parent
// define a method to add dependencies
// sub projects who need these dependencies will call this method 
def addDependencies(subProject) {   
   subProject.dependencies.add("implementation", "com.google.guava:guava:23.0")
   subProject.dependencies.add("implementation", "org.apache.commons:commons-lang3:3.8.1")
  // add others
}

// in child
dependencies {    
   addDependencies(this)
   // You can add other dependencies here If this child has any   
}

4 - Define dependencies as a list in the parent

// parent 
ext.appDependencies = [
     [configuration: "implementation", dependency: "org.apache.commons:commons-lang3:3.8.1"],
     [configuration: "implementation", dependency: "com.google.guava:guava:23.0"]
]

// child
dependencies {
    rootProject.appDependencies.each {
        add(it.configuration, it.dependency)
    }
}

There is a detailed explanation of this method in the following link, which uses external file to define these dependencies.

https://hackernoon.com/android-how-to-add-gradle-dependencies-using-foreach-c4cbcc070458

You can also combine 3. and 4. methods like define a list that has dependencies, call a function which iterate and add dependencies in that list. I would use the first or second method If I can. (Also there might be other ways to achieve this.)

miskender
  • 7,460
  • 1
  • 19
  • 23
  • 1
    Thanks @Silver Shroud. Your solution is really helpful. That's what I search for long time until now. – anticafe Mar 31 '19 at 11:51