Gradle 全局配置

在学习一些项目的时候发现在工程目录有这样一个文件config.gradle,查资才知道这是全局配置gradle,因为在开发中会有几个module,设置全局gradle其他的module就可以使用这些配置,直接上代码!

首先在根目录下新建一个config.gradle文件,然后只需要在根目录的build.gradle下添加一行代码:

apply from: 'config.gradle'

config.gradle中一般这么写,根据自己项目进行配置

ext {

    //support
    compileSdkVersion = 26
    minSdkVersion = 21
    targetSdkVersion = 26
    buildToolsVersion = '26.0.2'
    supportLibVersion = "26.0.1"
    androidSupport = "com.android.support"
    dependencies = [
            // support
            supportAppCompat        : "$androidSupport:appcompat-v7:$supportLibVersion",
            supportDesign           : "$androidSupport:design:$supportLibVersion",
            supportRecyclerview     : "$androidSupport:recyclerview-v7:$supportLibVersion",
            supportCardView         : "$androidSupport:cardview-v7:$supportLibVersion",
            supportMultidex         : "$androidSupport:multidex:1.0.1", 
            //下面可以配置三方依赖
             //组件通信
            eventbus                : "org.greenrobot:eventbus:3.1.1",
    ]
}

module使用

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "com.XXXX"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    dependencies{
        compile rootProject.ext.dependencies["supportDesign"]
        compile rootProject.ext.dependencies["supportAppCompat"]
        compile rootProject.ext.dependencies["supportRecyclerview"]
        compile rootProject.ext.dependencies["eventbus"]
    }
 }

你可能感兴趣的:(android)