0

What is the difference in declaring a dependency in a top level build.gradle file vs a module's build.gradle?

I see many projects that have dependencies declared in both the top level and also within the specific modules.

Which method is seen as optimal?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
java123999
  • 6,974
  • 36
  • 77
  • 121
  • 1
    Could you please provide an example? – Opal Dec 02 '15 at 10:12
  • For example declaring a dependency at the top level (project) build.gradle vs declaring one in a sub module of the project? – java123999 Dec 02 '15 at 10:12
  • http://stackoverflow.com/questions/23241681/why-are-there-two-build-gradle-files-in-an-android-studio-project – ashiquzzaman33 Dec 02 '15 at 10:20
  • 1
    @java123999, you have repeated the question. I'm asking for `build.gradle` examples. – Opal Dec 02 '15 at 10:22
  • What I want to know is why should dependencies be declared in the module level build files rather than declaring them all at the top level (project) build file? – java123999 Dec 02 '15 at 11:03

1 Answers1

1

If I had a dependency which was required by ALL subprojects I would declare it at the top level. For example all subprojects probably use junit, so I would put that at in the build.gradle for the top level project. If a dependency only related to one particular subproject it would be better to only declare the dependency in that project, since this would prevent it from being pulled in to other subprojects that dont require the dependency.

robjwilkins
  • 5,462
  • 5
  • 43
  • 59
  • Ok I understand this now, is this due to memory space? – java123999 Dec 02 '15 at 14:29
  • Its really just because its inefficient to package a lot of additional dependencies with code unless they're needed. It will slow down your build, and cause various other annoying problems. – robjwilkins Dec 02 '15 at 14:33
  • Yeah so if a dependency is declared at top level it will be downloaded for each sub module? if this was the case would it override an older version of the dependency in the module build.gradle? Thanks, this is very helpful for me as a gradle newbie! – java123999 Dec 02 '15 at 14:55