Android 多Module统一配置Gradle,只需三步

为什么要统一配置Gradle

最近在尝试模块化开发,当我创建到第二个module时,就突然一阵心慌,gradle这样配置下去后边怎么维护啊,难道改个buildToolsVersionbuildToolsVersion什么的要到每个module里的 build.gradle文件里改一遍?

先看一下正常的build.gradle配置:

Android 多Module统一配置Gradle,只需三步_第1张图片
normal_gradle.png

里面 minSdkVersioncompileSdkVersion依赖库版本...很多配置都是每个module必备的,肯定有办法动一动手指就可以将每个module里的环境配置改掉了...
ok,下边进入装逼时刻,三步教你统一gradle配置。

1.创建config.gradle文件

在project根目录下创建一个config.gradle文件

def tinkerVersion = "1.7.7"
def supportLibraryVersion = "25.2.0"
def frescoVersion = "1.2.0"
def butterknifeVersion = "8.4.0"

ext {
    android = [
            buildToolsVersion: "25.0.2",
            compileSdkVersion: 25,
            minSdkVersion    : 15,
            targetSdkVersion : 25,
            versionCode      : 47,
            versionName      : "1.3.6"
    ]
    
    dependencies = ["appcompat-v7"                : "com.android.support:appcompat-v7:${supportLibraryVersion}",
                    "support-v4"                  : "com.android.support:support-v4:${supportLibraryVersion}",
                    "support-v13"                  : "com.android.support:support-v13:${supportLibraryVersion}",
                    "recyclerview-v7"             : "com.android.support:recyclerview-v7:${supportLibraryVersion}",
                    "design"                      : "com.android.support:design:${supportLibraryVersion}",
                    "cardview-v7"                 : "com.android.support:cardview-v7:${supportLibraryVersion}",
                    "support-vector-drawable"     : "com.android.support:support-vector-drawable:${supportLibraryVersion}",
                    "junit"                       : 'junit:junit:4.12',
                    "tinker-android-anno"         : "com.tencent.tinker:tinker-android-anno:${tinkerVersion}",
                    "tinker-android-lib"          : "com.tencent.tinker:tinker-android-lib:${tinkerVersion}",
                    "fresco"                      : "com.facebook.fresco:fresco:${frescoVersion}",
                    "fresco-animated-gif"         : "com.facebook.fresco:animated-gif:${frescoVersion}",
                    "fresco-animated-webp"        : "com.facebook.fresco:animated-webp:${frescoVersion}",
                    "fresco-webpsupport"          : "com.facebook.fresco:webpsupport:${frescoVersion}",
                    "fresco-animated-base-support": "com.facebook.fresco:animated-base-support:${frescoVersion}",
                    "butterknife"                 : "com.jakewharton:butterknife:${butterknifeVersion}",
                    "butterknife-compiler"        : "com.jakewharton:butterknife-compiler:${butterknifeVersion}",
                    "gson"                        : "com.google.code.gson:gson:2.8.0"
    ]
}

如你所见,配置文件主要分sdk版本和依赖库两部分,另外第三方库的版本也被提了出来,一目了然...
PS:这里dependencies= []只是起到一个声明作用,并不会将所有的依赖库打包到apk中,so,在这里你可以声明所有常用的依赖库,各个module只需按需引用即可

2.导入config.gradle

一行代码apply from: "config.gradle"将刚才创建的config.gradle导入到project根目录的build.gradle

apply from: "config.gradle"

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

3.引用

配好矣亦导好矣,你可以在各个module里引用你的配置了。
声明两个变量,将配置引用进来

apply plugin: 'com.android.library'

def cfg = rootProject.ext.android // 工程配置
def libs = rootProject.ext.dependencies // 库依赖

配置version

android {
    compileSdkVersion cfg.compileSdkVersion
    buildToolsVersion cfg.buildToolsVersion

    defaultConfig {
        minSdkVersion cfg.minSdkVersion
        targetSdkVersion cfg.targetSdkVersion
        versionCode cfg.versionCode
        versionName cfg.versionName

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

    }
}

按需添加依赖库

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile libs["junit"]
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile libs["fresco"]
    compile libs["fresco-animated-gif"]
    compile libs["gson"]
    compile libs["appcompat-v7"]
    compile libs["design"]
    compile libs["recyclerview-v7"]
    compile libs["support-v4"]
    compile libs["cardview-v7"]
    
}

应该不用过多解释吧,相信你们已经秒懂...

总结

后来事实证明我的心慌还是很及时的

image.png
,我现在总共维护了十几个module...按照如上配置就好啦,真的是动动手指分分钟改掉版本配置( config.gradle文件),还可以避免因为第三方库版本不统一引起的编译错误。

你可能感兴趣的:(Android 多Module统一配置Gradle,只需三步)