svn 规范apk的生成命名

第一步:新建SVNVersion.gradle  放置于build.gradle统计目录下面

/*task svnversion {
    description 'Get SVN revision number.'
    new ByteArrayOutputStream().withStream { os ->
        exec {
            executable = 'svnversion'
            standardOutput = os
        }
        ext.revid = os.toString()
    }
}*/


def svnversion() {
    description 'Get SVN revision number.'
    new ByteArrayOutputStream().withStream { os ->
/*        exec {
            executable = 'svnversion'
            standardOutput = os
        }
        ext.revid = os.toString()*/
        def result = exec {
            executable = 'svn'
            args = ['info']
            standardOutput = os
        }
        def outputAsString = os.toString()
        def matchLastChangedRev = outputAsString =~ /Last Changed Rev: (d+)/
        // 因为要兼容之前的版本,所以codeVersion不能比之前的小
        ext.svnRev = 10000 + "${matchLastChangedRev[0][1]}".toInteger()
    }
    return svnRev
}
/*
task printsvn(description: 'Demonstrate calling svnversion task.') << {
    println 'Current SVN Version: ' + svnversion()+""
}
*/


import java.util.regex.Pattern

task('increaseVersionCode') << {
    description 'Get SVN revision number1.'
//    def manifestFile = file("src/main/AndroidManifest.xml")
    def manifestFile = file("build.gradle")
//    def pattern = Pattern.compile("versionCode="(\d+)"")
    def pattern = Pattern.compile("versionCode (\d+)")
    def manifestText = manifestFile.getText()
    def matcher = pattern.matcher(manifestText)
    matcher.find()
//    def versionCode = Integer.parseInt(matcher.group(1))
//    logger.debug("versionCode: ", versionCode);
    def manifestContent = matcher.replaceAll("versionCode " + svnversion())
    description 'Get SVN revision number2.'
    manifestFile.write(manifestContent)
}

task('incrementVersionName') << {
//    description 'Get SVN revision number3.'
//    def manifestFile = file("src/main/AndroidManifest.xml")
//    def patternVersionNumber = Pattern.compile("versionName="(\d+)\.(\d+)\.(\d+)\.(\d+)"")
//    def manifestText = manifestFile.getText()
//    def matcherVersionNumber = patternVersionNumber.matcher(manifestText)
//    matcherVersionNumber.find()
//    def majorVersion = Integer.parseInt(matcherVersionNumber.group(1))
//    def minorVersion = Integer.parseInt(matcherVersionNumber.group(2))
//    def pointVersion = Integer.parseInt(matcherVersionNumber.group(3))
////    def buildVersion = Integer.parseInt(matcherVersionNumber.group(4))
//    def mNextVersionName = majorVersion + "." + minorVersion + "." + pointVersion + "." + svnversion()
//    def manifestContent = matcherVersionNumber.replaceAll("versionName="" + mNextVersionName + """)
//    manifestFile.write(manifestContent)
}

tasks.whenTaskAdded { task ->
//    if (task.name == 'generateReleaseBuildConfig' || task.name == 'generateDebugBuildConfig') {
    task.dependsOn 'increaseVersionCode'
    task.dependsOn 'incrementVersionName'
//    }
}

第二步:在build.gradle中加入以下代码:

  buildTypes {
        release {
            signingConfig signingConfigs.release
            zipAlignEnabled true
            minifyEnabled true
            //混淆文件直接放在: proguard-rules.pro 里面即可
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 输出apk名称为wandoujia_v1.0.0.1320_20150115.apk
                        def fileName = "${variant.productFlavors[0].name}_v${defaultConfig.versionName}.${defaultConfig.versionCode}_${releaseTime()}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationIdSuffix '.bata'
            versionNameSuffix '.d'
            debuggable true
        }
    }
原文地址:https://www.cnblogs.com/xiaorenwu702/p/4887024.html