Android Gradle全局配置

项目由于是有组件分模块开发,所以会有很多个module,每一个module下都有一个gradle文件,其中都涉及到各种版本号的设置、尤其是编译版本相关,如果一个改变其他的都得跟着变否则就可能导致编译不过,这时候就可以把基本信息抽调出来另外自定义一个gradle专门配置这些基本信息,需改更改版本号时,只需要改配置的gradle了。

先贴一张我的项目结构图出来

18561089-CB47-4200-8D16-E48F564AECA1.png
apply plugin: 'com.android.library'
android {
    compileSdkVersion 25   //编译sdk版本
    buildToolsVersion "25.0.3" //构建工具版本
    //
    defaultConfig {
        minSdkVersion 14    //最小适应sdk版本
        targetSdkVersion 25  //目标sdk版本
        versionCode 1      //app版本号
        versionName "1.0"  //app 版本名
        resConfigs "zh"    //语言配置
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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

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

我的项目里一跟分了很多模块,每个模块都有一个gradle,其中 版本信息都是一样的,如果纯粹这么写,我改一个gradle 里的版本,然后每个都要改一遍就很麻烦了,所以我们需要建一个config.gradle 的文件来管理。

android {
    compileSdkVersion 25   //编译sdk版本
    buildToolsVersion "25.0.3" //构建工具版本
    //
    defaultConfig {
        minSdkVersion 14    //最小适应sdk版本
        targetSdkVersion 25  //目标sdk版本
        versionCode 1      //app版本号
        versionName "1.0"  //app 版本名
        resConfigs "zh"    //语言配置
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    compile 'com.android.support:appcompat-v7:25.3.1'

步骤:
1、在根目录建config.gradle

360C14E7-23A8-449B-AC2E-BEE7D8901224.png

在config.gradle 中我们可以这么写

//support 包的版本号
def supportVersion = "25.2.0"

ext {
   //版本信息配置
    version = [
            "buildToolsVersion": "25.0.2",
            "compileSdkVersion": 25,
            "minSdkVersion"    : 15,
            "targetSdkVersion" : 25,
            "versionCode"      : 260,
            "versionName"      : "2.6.1"
    ]
    //support 包相关包的引用
    dependencies = [
            "support-v4"   : "com.android.support:support-v4:${supportVersion}",
            "appcompat-v7" : "com.android.support:appcompat-v7:${supportVersion}",
            "recyclerview-v7" : "com.android.support:recyclerview-v7:${supportVersion}",
            "design" : "com.android.support:design:${supportVersion}",
            "constraint-layout" : "com.android.support.constraint:constraint-layout:1.0.2"
    ]
}

2、在项目根目录的build.gradle中apply config.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "config.gradle"
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.meituan.android.walle:plugin:1.1.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

3、在所有Module下的gradle中引用这些配置

apply plugin: 'com.android.library'
android {
    compileSdkVersion  rootProject.ext.version.compileSdkVersion
    buildToolsVersion  rootProject.ext.version.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.version.minSdkVersion
        targetSdkVersion rootProject.ext.version.targetSdkVersion
        versionCode rootProject.ext.version.versionCode as int
        versionName rootProject.ext.version.versionName
        resConfigs "zh"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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

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

这样就大功告成了,如果我们需要更改编译版本号,只需要改掉config.gradle中的对应的版本号就可以,其余任何地方都不需要改动。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • 工作中我们要善于总结,就像我们做销售一样,遇到的顾客穿衣风格不同,消费水平也不一样,我们要善于总结这些细节,抓住消...
    Ding欣欣阅读 187评论 0 0
  • 如果你见过cc,一定会被她的双商震撼~ 初见cc,浓眉深目,高挺的鼻梁,衣着朴素,脸上带着些许坚毅和精明,身上却微...
    珠圆玉润的李大福阅读 183评论 0 0