Gradle plugin 3.0 rewrite versionName

2020-07-16 02:45发布

问题:

I migrated to Android studio 3 and using gradle plugin v3.0.0-beta6. I would like to rewrite versionName/vesionCode of output .apk files. I used following code for gradle plugin 2.x in the build.gradle of my Android app module

applicationVariants.all { variant ->
    def flavor = variant.mergedFlavor

    flavor.versionName="${VERSION_NAME}"
    if (variant.buildType.isDebuggable()) {
        flavor.versionCode=9999
    } else {
        flavor.versionCode=Integer.parseInt(gitCommitCount)
    }
}

It doesn't work on gradle plugin v3.0.0-beta6. For versionCode, I successfully rewrite it with the solution in this Gradle 3.0.0 alpha variant output issue

回答1:

You can use the ApkVariantOutput.setVersionCodeOverride method. Just like the following:

applicationVariants.all { variant ->
    if (variant.buildType.name == "release") {
        variant.outputs.all {
            setVersionCodeOverride(project.VERSION_CODE.toInteger())
            setVersionNameOverride(project.VERSION_NAME)
        }
    }
}