Create a gist now

Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Show comment
Hide comment
@AkshayChordiya

AkshayChordiya Jul 22, 2015

Thank you so much

AkshayChordiya commented Jul 22, 2015

Thank you so much

@jfabrix101

This comment has been minimized.

Show comment
Hide comment
@jfabrix101

jfabrix101 Sep 13, 2015

Very useful tip

Very useful tip

@IlyaEremin

This comment has been minimized.

Show comment
Hide comment
@IlyaEremin

IlyaEremin Feb 11, 2016

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

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

@lopspower

This comment has been minimized.

Show comment
Hide comment
@lopspower

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

@dennisdrew

This comment has been minimized.

Show comment
Hide comment
@dennisdrew

dennisdrew Apr 26, 2016

@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

@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

This comment has been minimized.

Show comment
Hide comment
@cosic

cosic May 5, 2016

There is a good sample from artem-zinnatullin

cosic commented May 5, 2016

There is a good sample from artem-zinnatullin

@samardzija

This comment has been minimized.

Show comment
Hide comment
@samardzija

samardzija Oct 13, 2016

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

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

@GitHubMurt

This comment has been minimized.

Show comment
Hide comment
@GitHubMurt

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

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

@HamzehSoboh

This comment has been minimized.

Show comment
Hide comment
@HamzehSoboh

HamzehSoboh Dec 27, 2016

Worked like a charm!

Worked like a charm!

@ruijun

This comment has been minimized.

Show comment
Hide comment
@ruijun

ruijun Jun 1, 2017

awsome tip !!

ruijun commented Jun 1, 2017

awsome tip !!

@IHNEL

This comment has been minimized.

Show comment
Hide comment
@IHNEL

IHNEL Jul 10, 2017

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

IHNEL commented Jul 10, 2017

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

@renhuihhh

This comment has been minimized.

Show comment
Hide comment

excellent

@quezak

This comment has been minimized.

Show comment
Hide comment
@quezak

quezak 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

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

This comment has been minimized.

Show comment
Hide comment
@quezak

quezak 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).

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).

@vishnuharidas

This comment has been minimized.

Show comment
Hide comment
@vishnuharidas

vishnuharidas Jan 30, 2018

That's a very useful gist! 😮 👍

That's a very useful gist! 😮 👍

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