Gradle依赖的统一管理

在项目开发中我们会经常引入多个Module,然而每个Module中又包含了V4、V7,为了升级新版本依赖包只用更改一次,我们决定采用Gradle依赖的统一管理,避免重复繁琐的劳动。

在Project目录下新建config.gradle文件(文件名字可以自定义)

ext {
    android = [
            compileSdkVersion: 26,
            buildToolsVersion: "26.0.2",
            applicationId    : "com.wj.baseutils",
            minSdkVersion    : 15,
            targetSdkVersion : 26,
            versionCode      : 1,
            versionName      : "1.0.0"
    ]

    def dependVersion = [
            support    : "26.1.0",
            butterknife: "8.8.1",
            glide      : "4.5.0",
    ]

    dependencies = [
            //base
            "support-v4"          : "com.android.support:support-v4:${dependVersion.support}",
            "appcompat-v7"        : "com.android.support:appcompat-v7:${dependVersion.support}",
            "annotations"         : "com.android.support:support-annotations:${dependVersion.support}",
            "constraint-layout"   : "com.android.support.constraint:constraint-layout:1.0.2",
            //test
            "junit"               : "junit:junit:4.12",
            "runner"              : "com.android.support.test:runner:1.0.1",
            "espresso-core"       : "com.android.support.test.espresso:espresso-core:3.0.1",
            //eventbus
            "eventbus"            : "org.greenrobot:eventbus:3.1.1",
            //butterknife
            "butterknife"         : "com.jakewharton:butterknife:${dependVersion.butterknife}",
            "butterknife-compiler": "com.jakewharton:butterknife-compiler:${dependVersion.butterknife}",
            //Glide
            "glide"               : "com.github.bumptech.glide:glide:${dependVersion.glide}",
            "glide-compiler"      : "com.github.bumptech.glide:compiler:${dependVersion.glide}"
    ]
}

然后在Project的build.gradle中添加

apply from: "config.gradle"

在modle或者app中添加引用

apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.ext.android.compileSdkVersion

    defaultConfig {
        minSdkVersion rootProject.ext.android.minSdkVersion
        targetSdkVersion rootProject.ext.android.targetSdkVersion
        versionCode rootProject.ext.android.versionCode
        versionName rootProject.ext.android.versionName

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation rootProject.ext.dependencies["constraint-layout"]
    //test
    testImplementation rootProject.ext.dependencies["junit"]
    androidTestImplementation rootProject.ext.dependencies["runner"]
    androidTestImplementation rootProject.ext.dependencies["espresso-core"]
    //support
    api(rootProject.ext.dependencies["support-v4"]) {
        exclude module: 'support-annotations'
    }
    api(rootProject.ext.dependencies["appcompat-v7"]) {
        exclude module: 'support-v4'
    }
    api rootProject.ext.dependencies["annotations"]

    //eventbus
    api rootProject.ext.dependencies["eventbus"]

    //butterknife
    api(rootProject.ext.dependencies["butterknife"]) {
        exclude module: 'support-annotations'
    }
    annotationProcessor rootProject.ext.dependencies["butterknife-compiler"]

    //glide
    api(rootProject.ext.dependencies["glide"]) {
        exclude module: 'support-fragment'
    }
    annotationProcessor(rootProject.ext.dependencies["glide-compiler"]) {
        exclude module: 'jsr305'
    }
}

    原文作者:烂吹笙
    原文地址: https://www.jianshu.com/p/538389205783
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞