在defaultConfig中获取版本代码

时间:2016-09-01 12:54:55

标签: android gradle android-gradle android-productflavors

我有两种口味

 productFlavors {

    one{
        applicationId "com.test"
    }

    two{
        applicationId "com.test"
    }
}

defaultconfig如下所示:

defaultConfig {
    applicationId "com.test"
    multiDexEnabled true
    versionCode getCode(versionProps)
    versionName '10.1.8'
    minSdkVersion 18
    targetSdkVersion 23}

getcode

def getCode(Properties versionProps) {
def name
    if (getCurrentFlavor().equals("one")) {
        name = versionProps['build.version.one'].toInteger()
    }
    else if (getCurrentFlavor().equals("two")) {
        name = versionProps['build.version.two'].toInteger()
    }
return name}

Version.prop文件有2个变量

build.version.one=203
build.version.two=101

现在,当我正常从studio中运行项目时,getCode()函数返回null。

def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

Pattern pattern;

if (tskReqStr.contains("assemble"))
    pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
    pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

Matcher matcher = pattern.matcher(tskReqStr)

if (matcher.find()) {
    println matcher.group(1)
    return matcher.group(1)
} else {
    println "NO MATCH FOUND"
    return "";
}}

0 个答案:

没有答案