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}",
]
}
@AkshayChordiya

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

@jfabrix101

Very useful tip

@IlyaEremin

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

@lopspower
lopspower commented Apr 25, 2016 edited

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 :(

@dennisdrew

@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
cosic commented May 5, 2016 edited

There is a good sample from artem-zinnatullin

@samardzija

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

@GitHubMurt
GitHubMurt commented Dec 27, 2016 edited

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 ' '

@HamzehSoboh

Worked like a charm!

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