Android dependency version inside ext vs dependencies block

Viewed 496

Where do we declare Android dependencies versions in a project? Should the versions be defined in the project level ex{ } block

top-level android build script

or the module level dependencies { } block?

android module-level build script

2 Answers

Where do we declare Android dependencies versions in a project?

In brief,

  • for a multi-module project (app module etc.) use project's build.gradle,
  • for a single-module project (app module only) use app's build.gradle

When following the rule for a multi-module project, a library's version change is done only in one place and effects all the dependent modules.

Consider the following example with the Kotlin library version.

In project's build.gradle:

ext {
    kotlin_version = '1.4.21'
    ...
    kotlin_stdlib = "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

In a module's (e.g. app's) build.gradle:

dependencies {
    implementation kotlin_stdlib
    ...
}

build.gradle - project module with at the bottom

ext.dependenciesVersion = [:]
dependenciesVersion.libraryName = "x.x.x"

build.gradle - app module

implementation "librarySource.$dependencyVersion.libraryName"
Related