Android Gradle edit

Centralizing dependencies via dependencies.gradle file

When working with multi-module projects, it is helpful to centralize dependencies in a single location rather than having them spread across many build files, especially for common libraries such as the Android support libraries and the Firebase libraries.

One recommended way is to separate the Gradle build files, with one build.gradle per module, as well as one in the project root and another one for the dependencies, for example:

  +- gradleScript/
  |     dependencies.gradle
  +- module1/
  |     build.gradle
  +- module2/
  |     build.gradle
  +- build.gradle

Then, all of your dependencies can be located in gradleScript/dependencies.gradle:

ext {
    // Version
    supportVersion = '24.1.0'

    // Support Libraries dependencies
    supportDependencies = [
            design:            "${supportVersion}",
            recyclerView:      "${supportVersion}",
            cardView:          "${supportVersion}",
            appCompat:         "${supportVersion}",
            supportAnnotation: "${supportVersion}",

    firebaseVersion = '9.2.0';

    firebaseDependencies = [
            core:         "${firebaseVersion}",
            database:     "${firebaseVersion}",
            storage:      "${firebaseVersion}",
            crash:        "${firebaseVersion}",
            auth:         "${firebaseVersion}",
            messaging:    "${firebaseVersion}",
            remoteConfig: "${firebaseVersion}",
            invites:      "${firebaseVersion}",
            adMod:        "${firebaseVersion}",
            appIndexing:  "${firebaseVersion}",

Which can then be applied from that file in the top level file build.gradle like so:

// Load dependencies
apply from: 'gradleScript/dependencies.gradle'

and in the module1/build.gradle like so:

// Module build file
dependencies {
    // ...
    compile supportDependencies.appCompat
    compile firebaseDependencies.crash

Another approach

A less verbose approach for centralizing library dependencies versions can be achieved by declaring the version number as a variable once, and using it everywhere.

In the workspace root build.gradle add this:

ext.v = [

And in every module that uses the same library add the needed libraries

compile "${v.supportVersion}"
compile "${v.supportVersion}"
compile "${v.supportVersion}"
compile "${v.supportVersion}"

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents
3 Gradle
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close