Centralizing Gradle Dependencies in Multi-Module Android Projects

Creating a Shared Configuration File

Create a new Gradle file named dependencies.gradle at the root of your project:

ext{
    sdkConfig = [
            minSdk           : 21,
            targetSdk        : 33,
            compileSdk       : 33,
            applicationId    : "com.example.myapp",
            versionCode      : 1,
            versionName      : "1.0.0"
    ]
    
    libraryVersions = [
            androidxCore      : "1.10.1",
            material         : "1.9.0",
            retrofit         : "2.9.0",
            okhttp           : "4.11.0",
            dagger           : "2.48",
            glide            : "4.15.1"
    ]

    dependencies = [
            // AndroidX Core
            androidx_appcompat     : "androidx.appcompat:appcompat:${libraryVersions.androidxCore}",
            androidx_material      : "com.google.android.material:material:${libraryVersions.material}",
            androidx_constraint    : "androidx.constraintlayout:constraintlayout:2.1.4",
            
            // Networking
            retrofit_runtime      : "com.squareup.retrofit2:retrofit:${libraryVersions.retrofit}",
            retrofit_converter     : "com.squareup.retrofit2:converter-gson:${libraryVersions.retrofit}",
            retrofit_adapter       : "com.squareup.retrofit2:adapter-rxjava3:${libraryVersions.retrofit}",
            okhttp_core            : "com.squareup.okhttp3:okhttp:${libraryVersions.okhttp}",
            okhttp_logger          : "com.squareup.okhttp3:logging-interceptor:${libraryVersions.okhttp}",
            gson                   : "com.google.code.gson:gson:2.10.1",
            
            // Image Loading
            glide_runtime          : "com.github.bumptech.glide:glide:${libraryVersions.glide}",
            glide_compiler         : "com.github.bumptech.glide:compiler:${libraryVersions.glide}",
            
            // Dependency Injection
            dagger_runtime         : "com.google.dagger:dagger:${libraryVersions.dagger}",
            dagger_processor       : "com.google.dagger:dagger-compiler:${libraryVersions.dagger}",
            
            // UI Components
            recycler_view          : "androidx.recyclerview:recyclerview:1.3.0",
            card_view              : "androidx.cardview:cardview:1.0.0",
            swiperefresh           : "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
            
            // Lifecycle
            lifecycle_runtime      : "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1",
            lifecycle_viewmodel    : "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1"
    ]
}

Applying the Configuration

In your root-level build.gradle file, add the following to import the shared configuration:

apply from: "dependencies.gradle"

Consuming from Modules

Each module can now access thece centralized configurations diretcly:

android {
    namespace 'com.example.mymodule'
    compileSdk sdkConfig.compileSdk

    defaultConfig {
        applicationId sdkConfig.applicationId
        minSdk sdkConfig.minSdk
        targetSdk sdkConfig.targetSdk
        versionCode sdkConfig.versionCode
        versionName sdkConfig.versionName
    }
}

dependencies {
    implementation dependencies.androidx_appcompat
    implementation dependencies.androidx_material
    implementation dependencies.retrofit_runtime
    implementation dependencies.okhttp_core
    implementation dependencies.okhttp_logger
    implementation dependencies.glide_runtime
    annotationProcessor dependencies.glide_compiler
}

This pattern keeps all version numbers and dependency coordinates in a single location, making updates straightforward and reducing the risk of version mismatches across the project.

Tags: Gradle Android Build Configuration Dependencies Android Studio

Posted on Sat, 22 Aug 2026 16:29:52 +0000 by dlebowski