与摇篮变化的apk的名字摇篮、名字、apk

2023-09-12 07:15:48 作者:彼岸花逝丶

我有一个Android项目,该项目采用的摇篮构建过程。现在,我想补充两个额外的构建类型的临时和生产,所以我build.gradle包括:

I have an Android project which uses Gradle for build process. Now I want to add two extra build types staging and production, so my build.gradle contains:

android {
    buildTypes {
        release {
            runProguard false
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }

        staging {
            signingConfig signingConfigs.staging

            applicationVariants.all { variant ->
                appendVersionNameVersionCode(variant, defaultConfig)
            }
        }

        production {
            signingConfig signingConfigs.production
        }
    }
}

和我appndVersionNameVersion code是这样的:

and my appndVersionNameVersionCode looks like:

def appendVersionNameVersionCode(variant, defaultConfig) {
    if(variant.zipAlign) {
        def file = variant.outputFile
        def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
        variant.outputFile = new File(file.parent, fileName)
    }

    def file = variant.packageApplication.outputFile
    def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
    variant.packageApplication.outputFile = new File(file.parent, fileName)
}

如果我执行任务的 assembleStaging 的话,我得到我的apk的正确名字,但是当我执行的 assembleProduction 的然后我得到(分期情况等)改变了我的apk的名字。例如:

If I execute task assembleStaging then I get proper name of my apk, but when I execute assembleProduction then I get changed names of my apk (like in staging case). For example:

MyApp-defaultFlavor-production-9.9.9-999.apk
MyApp-defaultFlavor-production-9.9.9-999.apk

它看起来像在生产中生成类型执行的 appendVersionNameVersion code 的。我怎样才能避免呢?

It looks like in production build type is executed appendVersionNameVersionCode. How can I avoid it?

推荐答案

由于CommonsWare在他的评论中写道,你应该叫 appendVersionNameVersion code 只为分期变种。您可以轻松地做到这一点,只要稍微修改 appendVersionNameVersion code 方法,例如:

As CommonsWare wrote in his comment, you should call appendVersionNameVersionCode only for staging variants. You can easily do that, just slightly modify your appendVersionNameVersionCode method, for example:

def appendVersionNameVersionCode(variant, defaultConfig) {
    //check if staging variant
    if(variant.name == android.buildTypes.staging.name){
        if(variant.zipAlign) {
            def file = variant.outputFile
            def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
            variant.outputFile = new File(file.parent, fileName)
        }

    def file = variant.packageApplication.outputFile
    def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
    variant.packageApplication.outputFile = new File(file.parent, fileName)
    }
}