Gradle依存の統合管理

6633 ワード

プロジェクト開発では多くのModuleを導入していますが、各ModuleにはV 4、V 7が含まれています.新しいバージョンの依存パッケージをアップグレードするには1回だけ変更するために、Gradle依存の統一管理を採用し、煩雑な労働を繰り返すことを避けることにしました.
プロジェクトディレクトリの下に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}"
    ]
}

そしてプロジェクトの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'
    }
}