How do I share dependencies between Android modules

后端 未结 5 1092
长情又很酷
长情又很酷 2020-12-29 02:30

I have an Android application module (app) and an Android library module (library). Both app and library contain these same dependencies:

dependencies {
             


        
5条回答
  •  旧时难觅i
    2020-12-29 03:01

    You could do something like this where the project build.gradle will specify the dependencies needed as variable names then in the app build.gradle files you just need to include the variable names. This is very useful when you have many modules and don't want to edit everyone when a version number changes!

    project build.gradle

    buildscript {
        ext {
            googlePlayServicesVersion = '7.5.0'
            supportLibVersion = '22.2.0'
        }
    ... (the rest of your repositories/dependency info here) ...
    }
    
    ext {
        minSdkVersion=16
        targetSdkVersion=21
        buildToolsVersion='22.0.1'
        compileSdkVersion=21
    
        //Android Dependencies
        supportV4 = 'com.android.support:support-v4:' + supportLibVersion
        supportAnnotations = 'com.android.support:support-annotations:' + supportLibVersion
        recyclerView = 'com.android.support:recyclerview-v7:' + supportLibVersion
        cardView = 'com.android.support:cardview-v7:' + supportLibVersion
        palette = 'com.android.support:palette-v7:' + supportLibVersion
        appCompat = 'com.android.support:appcompat-v7:' + supportLibVersion
        multidex = 'com.android.support:multidex:1.0.1'
        appCompat = 'com.android.support:appcompat-v7:' + supportLibVersion
        supportDesign = 'com.android.support:design:' + supportLibVersion
        playServicesAnalytics = 'com.google.android.gms:play-services-analytics:' + googlePlayServicesVersion
    }
    

    app build.gradle file

    dependencies {
       compile rootProject.ext.supportV4
        compile rootProject.ext.appCompat
        compile rootProject.ext.supportAnnotations
        compile rootProject.ext.recyclerView
        compile rootProject.ext.cardView
        compile rootProject.ext.palette
        compile rootProject.ext.appCompat
        compile rootProject.ext.multidex
        compile rootProject.ext.supportDesign
        compile rootProject.ext.playServicesAnalytics
    
    }
    

    Hope that this helps!

提交回复
热议问题