Android studio 3.2 version custom apk name compilation exception

Keywords: git Android Gradle

When the project is upgraded from version 3.x to version 3.2, the original code of custom output apk name is invalid, and the new writing method is changed to
//This script is written in a project level gradle file
//  AS3.2 version / / output apk custom name
android {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.versionCodeOverride = rootProject.gitVersionCode()
                output.versionNameOverride = rootProject.gitVersionTag()
                if (variant.name.endsWith("Debug")) {
                    //debug package uses git automatic version number
                    output.outputFileName = "$applicationId _v${variant.mergedFlavor.versionName}_${debugTime()}_debug.apk"
                } else {
                    //release package uses git automatic version number and version name
                    output.outputFileName = "$applicationId _v${variant.mergedFlavor.versionName}_code${variant.mergedFlavor.versionCode}_${releaseTime()}_release.apk"
                }
            }
        }
    }
  1. The rootProject.gitVersionCode() and rootProject.gitVersionTag() methods are used to automatically generate version number and version name in combination with git. The related code declared in the gradle file at the project level (the outermost declaration) is as follows
def gitVersionCode() {
//    def cmd = 'git rev-list --all-match --first-parent --count'
    def cmd = 'git rev-list HEAD --count'
    cmd.execute().text.trim().toInteger()
}

def gitVersionTag() {
    def cmd = 'git describe --tags'
    def version = cmd.execute().text.trim()

    def pattern = "-(\\d+)-g"
    def matcher = version =~ pattern

    if (matcher) {
        version = version.substring(0, matcher.start()) + "." + matcher[0][1]
    } else {
//        version = version + ".0"
    }
    return version
}

This is just a code snippet. Please refer to it for details on how to automatically generate version number and version name by combining git (there is a pit here, and the configuration methods of different versions of Android studio are different)

2. The debugtime () method and releaseTime() method are declared at the top of the script as follows

static def releaseTime() {
    return new Date().format("yyyy-MM-dd-HH-mm", TimeZone.getDefault())//Include hours, minutes and seconds
}

static def debugTime() {
//    return new Date().format("yyyy-MM-dd", TimeZone.getDefault())
    return new Date().format("yyyy", TimeZone.getDefault())
}
The original user-defined apk name is written as shown in the above 3.2 version
    //AS3.x version / / output apk custom name
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            variant.mergedFlavor.versionCode = rootProject.gitVersionCode()
            variant.mergedFlavor.versionName = rootProject.gitVersionTag()
            if (variant.name.endsWith("Debug")) {
                //debug package uses git automatic version number
                outputFileName = "$applicationId _v${variant.mergedFlavor.versionName}_${debugTime()}_debug.apk"
            } else {
                //release package uses git automatic version number and version name
                outputFileName = "$applicationId _v${variant.mergedFlavor.versionName}_code${variant.mergedFlavor.versionCode}_${releaseTime()}_release.apk"
            }
        }
    }
Versions before 3.x are written differently. I won't elaborate here

Posted by djmc48 on Wed, 18 Dec 2019 08:59:34 -0800