Skip to content

Instantly share code, notes, and snippets.

@gabrielemariotti
Last active February 24, 2021 10:52
Show Gist options
  • Save gabrielemariotti/ad6672902464ee2392d0 to your computer and use it in GitHub Desktop.
Save gabrielemariotti/ad6672902464ee2392d0 to your computer and use it in GitHub Desktop.
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}",
]
}
@Qw4z1
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
    }

@abhimuktheeswarar
Copy link

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
Copy link

Thanks a lot. This was helpful

@igorescodro
Copy link

@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