使用 Gradle 更改 apk 名称 [英] Change apk name with Gradle
问题描述
我有一个使用 Gradle 进行构建过程的 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
}
}
}
我的 appndVersionNameVersionCode 看起来像:
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
看起来在生产构建类型中执行了appendVersionNameVersionCode.我怎样才能避免它?
It looks like in production build type is executed appendVersionNameVersionCode. How can I avoid it?
推荐答案
正如 CommonsWare 在他的评论中所写的那样,你应该只为暂存变体调用 appendVersionNameVersionCode
.您可以轻松地做到这一点,只需稍微修改您的 appendVersionNameVersionCode
方法,例如:
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)
}
}
这篇关于使用 Gradle 更改 apk 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!