Android/gradle: include version name from flavor in apk file name

前端 未结 2 933
天命终不由人
天命终不由人 2021-02-20 06:32

I have found similar questions but nothing that quite works for what I want to do. I\'m developing with Android Studio and gradle, and I have several flavors in my build file,

相关标签:
2条回答
  • 2021-02-20 07:10

    kcoppock's answer is correct, but there is a small typo. It should be:

    android {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(output.outputFile.parentFile,
                        output.outputFile.name.replace(".apk", "${variant.versionName}.apk"));
            }
        }
    }
    

    (I wrote this as an answer because I don't have the reputation to make a comment)

    0 讨论(0)
  • 2021-02-20 07:19

    EDIT: For modern versions of the Gradle tools, use @ptx's variation (use output.outputFile in place of variant.outputFile:

    android {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(output.outputFile.parentFile,
                        output.outputFile.name.replace(".apk", "${variant.versionName}.apk"));
            }
        }
    }
    

    As of Gradle tools plugin version 0.14, you should start using the following instead to properly support multiple output files (e.g. APK Splits):

    android {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                variant.outputFile = new File(variant.outputFile.parentFile,
                        variant.outputFile.name.replace(".apk", "${variant.versionName}.apk"));
            }
        }
    }
    

    For older versions:


    Yeah, we do this in our app. Just add the following in your android tag in build.gradle:

    android {
        applicationVariants.all { variant ->
            def oldFile = variant.outputFile
            def newPath = oldFile.name.replace(".apk", "${variant.versionName}.apk")
            variant.outputFile = new File(oldFile.parentFile, newPath)
        }
    }
    
    0 讨论(0)
提交回复
热议问题