How to centralize the support libraries dependencies in gradle

Working with multi-modules project, it is very useful to centralize the dependencies, especially the support libraries.

A very good way is to separate gradle build files, defining something like:
root
  --gradleScript
  ----dependencies.gradle
  --module1
  ----build.gradle
  --build.gradle
In gradleScript/dependecies.gradle:

ext {
    //Version
    supportLibrary = '22.2.1'

    //Support Libraries dependencies
    supportDependencies = [
            design           :    "com.android.support:design:${supportLibrary}",
            recyclerView     :    "com.android.support:recyclerview-v7:${supportLibrary}",
            cardView         :    "com.android.support:cardview-v7:${supportLibrary}",
            appCompat        :    "com.android.support:appcompat-v7:${supportLibrary}",
            supportAnnotation:    "com.android.support:support-annotations:${supportLibrary}",
    ]
}
In the top level file build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}

// Load dependencies
apply from: 'gradleScript/dependencies.gradle'
In the module1/build.gradle:

// Module build file

dependencies {
    //......
    compile supportDependencies.appCompat
    compile supportDependencies.design
}
Here you can find the full gist.

Thanks to Fernando Cejas and this blog.

Popular posts from this blog

Expand and collapse animation

Android-5: Card and images with rounded corners in Android 4