Gradle之全局配置

为什么要全局配置Gradle?

答:为方便在不同module中设置版本号等配置信息,可以通过配置全局变量来统一所有module的公共配置信息。

Gradle全局配置流程

  1. 在项目的根目录下新建config.gradle文件(如下图)
    2..将gradle中的公共部分写入 config.gradle 文件中:

androidSupportVersion =
ext {
    android = [        : 26,//编译的 SDK 版本,如API20
               //构建工具的版本,其中包括了打包工具aapt、dx等,如API20对应的build-tool的版本就是20.0.0
                       : "26.0.2",
               //兼容的最低 SDK 版本
                           : 21,
               //向前兼容,保存新旧两种逻辑,并通过 if-else 方法来判断执行哪种逻辑
                        : 22,
                           : 'com.suchengkeji.andeoid.confingdemo',
                              : 1,
                              : 'v 1.1.1',// versionName  最后组成的是1.1.1
                     : 'release',
                       : true,
                : 'android.support.test.runner.AndroidJUnitRunner'
    ]

    dependencies = ["appcompat-v7": "com.android.support:appcompat-v7:${androidSupportVersion}",
                    //"appcompat-v7": "com.android.support:appcompat-v7:25.3.1",
                    "constraint"  : "com.android.support.constraint:constraint-layout:1.0.2",
                    "junit"       : 'junit:junit:4.12',
    ]
}
复制代码

3.在主项目根目录的build.gradle中申明一下(声明在project/buile.gradle下):


apply   : 
复制代码

4在项目中(app/build.gradle)引用我们的路径配置(如图:)

app/build.gradle整体文件


apply plugin: 'com.android.application'
def Buildconfig = rootProject.ext.android
def BuildLibray = rootProject.ext.dependencies

android {
    //版本配置
    buildToolsVersion Buildconfig["buildToolsVersion"]
    compileSdkVersion Buildconfig["compileSdkVersion"]

    defaultConfig {
        applicationId Buildconfig["applicationId"]
        //SDK配置
        minSdkVersion Buildconfig["minSdkVersion"]
        targetSdkVersion Buildconfig["targetSdkVersion"]
        versionCode Buildconfig["versionCode"]
        versionName Buildconfig["versionName"]
    }

    //签名的配置
    signingConfigs {
        debug {
            storeFile file('H:\\configTest.jks')//签名文件路径
            storePassword "123456"//签名密钥密码
            keyAlias "configTest"//别名
            keyPassword "123456"  //签名公钥密码
            println("====== signingConfigs.debug ======")
        }
        release {
            storeFile file('H:\\configTest.jks')//签名文件路径
            storePassword "123456"
            keyAlias "configTest"
            keyPassword "123456"  //签名密码
            println("====== signingConfigs.release ======")
        }
    }

    buildTypes {
        release {
            minifyEnabled false//是否启动混淆
            zipAlignEnabled false//是否启动zipAlign优化
            shrinkResources false // 是否移除无用的resource文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release//打包命令行:gradlew assembleRelease

            android {
                applicationVariants.all { variant ->
                    variant.outputs.each { output ->
                        def outputFile = output.outputFile
                        if (outputFile != null && outputFile.name.endsWith('.apk')) {
                            File outputDirectory = new File(outputFile.parent);
                            if (variant.buildType.name == "release") {
                                //app名称_vnandroid.support.test.runner.AndroidJUnitRunner_vc1_release.apk
                                //def fileName = "app名称_vn${defaultConfig.versionName}_vc${defaultConfig.versionCode}_release.apk"
                                //名称ConfigTest_版本v 1.1.1_时间2017-12-26_打包渠道wandoujia.apk
                                def fileName = "ConfigTest_${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
                                output.outputFile = new File(outputDirectory, fileName)
                            }
                        }
                    }
                }
            }

        }
    }

    //配置多渠道打包
    productFlavors {
        zejian {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
        huawei {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
        _360 {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
        wandoujia {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
    }

    //配置多渠道打包简写方法
//    productFlavors {
//
//        wandoujia {}
//        baidu {}
//        c360 {}
//        uc {}
//
//        productFlavors.all { flavor ->
//            flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
//        }
//
//    }

    lintOptions {
        //设置编译的lint开关,程序在buid的时候,会执行lint检查,有任何的错误或者警告提示,都会终止构建
        abortOnError false
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    依赖配置
    compile BuildLibray["appcompat-v7"]
    compile BuildLibray["constraint"]
}

def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("GMT"))
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值