Skip to content

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.

Copy link

AkshayChordiya commented Jul 22, 2015

Thank you so much

@jfabrix101

This comment has been minimized.

Copy link

jfabrix101 commented Sep 13, 2015

Very useful tip

@IlyaEremin

This comment has been minimized.

Copy link

IlyaEremin commented Feb 11, 2016

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.

Copy link

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.

Copy link

dennisdrew commented 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

@cosic

This comment has been minimized.

Copy link

cosic commented May 5, 2016

There is a good sample from artem-zinnatullin

@samardzija

This comment has been minimized.

Copy link

samardzija commented Oct 13, 2016

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

@GitHubMurt

This comment has been minimized.

Copy link

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.

Copy link

HamzehSoboh commented Dec 27, 2016

Worked like a charm!

@ruijun

This comment has been minimized.

Copy link

ruijun commented Jun 1, 2017

awsome tip !!

@IHNEL

This comment has been minimized.

Copy link

IHNEL commented Jul 10, 2017

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

@renhuihhh

This comment has been minimized.

Copy link

renhuihhh commented Sep 4, 2017

excellent

@quezak

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

vishnuharidas commented Jan 30, 2018

That's a very useful gist! 😮 👍

@Belka1000867

This comment has been minimized.

Copy link

Belka1000867 commented Jan 10, 2019

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

+1 Also interested in this.

@Qw4z1

This comment has been minimized.

Copy link

Qw4z1 commented Feb 11, 2019

@quezak and @Belka1000867 compile (and implementation) is a function taking a string as an agrument. So in the same way as you can use the vars directly, i.e. implementation thirdParty.crashlytics you can also use them as such:

    implementation(thirdParty.crashlytics) {
        transitive = true
    }
@abeemukthees

This comment has been minimized.

Copy link

abeemukthees commented Feb 15, 2019

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

Is there any way to highlight new versions by using this approach?

@OlukaDenis

This comment has been minimized.

Copy link

OlukaDenis commented May 2, 2019

Thanks a lot. This was helpful

@igorescodro

This comment has been minimized.

Copy link

igorescodro commented Jun 12, 2019

@abeemukthees, @Belka1000867 and @quezak, you can use the following library to check for updates: gradle-versions-plugin.

It does not make the dependency yellow, but it generates a report with all updates, like this:

 - com.android.tools.lint:lint-gradle [26.4.1 -> 26.6.0-alpha03]
     https://developer.android.com/studio
 - com.google.android.material:material [1.0.0 -> 1.1.0-alpha07]
     http://developer.android.com/tools/extras/support-library.html
 - com.pinterest:ktlint [0.32.0 -> 0.33.0]
     https://github.com/pinterest/ktlint
 - io.gitlab.arturbosch.detekt:detekt-cli [1.0.0-RC14 -> 1.0.0-RC15]
     https://arturbosch.github.io/detekt
 - io.reactivex.rxjava2:rxandroid [2.1.0 -> 2.1.1]
     https://github.com/ReactiveX/RxAndroid
 - io.reactivex.rxjava2:rxjava [2.2.0 -> 2.2.9]
     https://github.com/ReactiveX/RxJava
 - junit:junit [4.12 -> 4.13-beta-3]
     http://junit.org

It is better than check one by one manually. 😝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.