Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to manage the support libraries in a multi-module projects. Thanks to Fernando Cejas (http://fernandocejas.com/)

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
}
// Module build file
dependencies {
//......
compile supportDependencies.appCompat
compile supportDependencies.design
}
// 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: 'dependencies.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}",
]
}

Thank you so much, unfortunately it's giving me problems. I mean it gives errors which I couldn't trace

Very useful tip

Also, you can union all supportDependencies in following way:
compile supportDependencies.values()
instead of writing them one by one

lopspower commented Apr 25, 2016

Doesn't work for me.

I have:

Error:(48, 0) Cannot convert a null value to an object of type Dependency.
The following types/formats are supported:
  - Instances of Dependency.
  - String or CharSequence values, for example 'org.gradle:gradle-core:1.0'.
  - Maps, for example [group: 'org.gradle', name: 'gradle-core', version: '1.0'].
  - FileCollections, for example files('some.jar', 'someOther.jar').
  - Projects, for example project(':some:project:path').
  - ClassPathNotation, for example gradleApi().

Because of:

dependencies {
    // SUPPORT
    compile supportDependencies.appcompat
    compile supportDependencies.design
    compile supportDependencies.recyclerview
    ...
}

HELP :(

@lopspower you're not accessing the properties correctly. If you notice in the dependencies.gradle, the dependencies are declared as part of the ext closure. Also, if you notice in the root build.gradle, the dependencies.gradle is applied. Therefore, the dependency values are located on the root project's ext closure. Therefore, you would access them:
compile rootProject.ext.supportDependencies.appcompat

cosic commented May 5, 2016

There is a good sample from artem-zinnatullin

@lopspower you wrote supportDependencies.appcompat and it should be supportDependencies.appCompat. It is case sensitive :)

GitHubMurt commented Dec 27, 2016

You may also build your grade:app in following manner:

android{
[...]
ext{
    supportLibrary = '25.1.0'
    playServices = '9.8.0'
    firebase = playServices
}
dependencies {
   [...]
    compile "com.google.android.gms:play-services-location:${playServices}"
    compile "com.google.android.gms:play-services-maps:${playServices}"
    compile "com.google.firebase:firebase-core:${firebase}"
    compile "com.google.firebase:firebase-messaging:${firebase}"

    compile "com.android.support:appcompat-v7:${supportLibrary}"
    compile "com.android.support:design:${supportLibrary}"
    compile "com.android.support:palette-v7:${supportLibrary}"
}

Just don't forget to wrap dependencies in " " instead of ' '

Worked like a charm!

ruijun commented Jun 1, 2017

awsome tip !!

IHNEL commented Jul 10, 2017

By this way Android Studio doesn't notify for newer version of dependencies.

excellent

quezak commented Jan 11, 2018

Very nice!
Is it possible to similarily extract "complex" dependencies to another file, in a way they can be included with one line or together with a whole list of deps? Two examples:

    compile('com.crashlytics.sdk.android:crashlytics:2.6.7@aar') {
        transitive = true;
    }
    
    compile('com.github.tony19:logback-android-classic:1.1.1-6') {
        exclude group: 'com.google.android', module: 'android'
    }

Best I can think of is just replacing the full package-and-version leaving the rest intact: compile(libs.someVar) { ... }, but then I can't use that dependency together with others in a list, like you did with compile supportDependencies.values().

quezak commented Jan 11, 2018

Another question: is there a way for Android Studio to highlight dependencies with new versions using this method, same as they were highlighted in build.gradle originally? Same question for auto-detecting changes in the dependencies.gradle file (if I change it, there is no prompt to do a gradle sync, like when changing any build.gradle file).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment