全局设置Gradle

来源:互联网 发布:制图软件手机 编辑:程序博客网 时间:2024/05/16 10:03

转载请注明出处:http://blog.csdn.net/u013813116/article/details/54344549

      大家好,接触android已经快两年了,在大学那会学得都比较杂:有写过微信开发、web端开发、HTML5,后面才接触的android。在这两年里多亏了各路大神的帮忙,一直都是看着大神们的博客成长的。不能总是一味的索取,不做任何贡献。所以决定从今天起也和大家分享下自己在成长路上的一些心得体会。

      哈!废话多了,那就让我们开始吧!
      很多东西我们都喜欢智能化点,当然对于程序我们也是希望如此。当我们进行工程项目开发时难免会有多个modules,而每个modules目录下都会有一个build.gradle,build.gradle只对相应的modules起作用,可以重写任何的参数,它大概应该是这样的:
android {    compileSdkVersion 24//编译的SDK版本    buildToolsVersion "24.0.3" //编译的Tools版本    defaultConfig { //默认配置        applicationId "xxxxxx"//应用程序的包名        minSdkVersion 15 //支持的最低版本        targetSdkVersion 24 //支持的目标版本        versionCode 1 //版本号        versionName "1.0" //版本名    }    signingConfigs {//签名配置        release {//发布版签名配置            storeFile file('xxxxxx')            storePassword 'xxxx'            keyAlias 'xxxxxxx'            keyPassword 'xxxxxx'        }        debug {//debug版签名配置            storeFile file('xxxxxx')            storePassword 'xxxx'            keyAlias 'xxxxxxx'            keyPassword 'xxxxxx'} } buildTypes { //build类型        release {            minifyEnabled false//混淆关闭            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//指定混淆规则文件            signingConfig signingConfigs.release//设置签名信息        }        debug {//调试            signingConfig signingConfigs.release        }    }}allprojects {    repositories {        maven { url "https://jitpack.io" }    }}dependencies {    compile fileTree(include: ['*.jar'], dir: 'jniLibs')//编译lib目录下的.jar文件    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:24.2.1'    testCompile 'junit:junit:4.12'    compile project(':xxxx')//编译附加的项目    compile 'com.jakewharton:butterknife:8.4.0'    apt 'com.jakewharton:butterknife-compiler:8.4.0'    compile files('xxxxx')}


当我们需要统一设置依赖库、版本号或者依赖库版本需要更新时,你会发现有点麻烦,需要进入到一个个build.gradle文件里进行修改。为什么我们不创建一个可以进行全局配置的gradle文件呢。

全局设置

首先在项目的根目录下创建一个config.gradle文件,如下图所示:

config.gradle里的数据根据项目需要进行设置,如下图所示:

设置好config.gradle文件后,接下来对每个modules下的build.gradle进行配置,如下图所示:

是不是 so easy!等等,你该不会以为这样就大功告成了吧!还有至关重要的一步哦,那就是工程项目根目录下的build.gradle 里还需添加一行:
apply from: "config.gradle"
然后sync下,ok!perfect。这样以后要是版本更新,修改数据就方便多了
    
    
0 0