Android 使用 Gradle 更改 apk 名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22126299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change apk name with Gradle
提问by Grzegorz
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:
我有一个使用 Gradle 进行构建过程的 Android 项目。现在我想添加两个额外的构建类型暂存和生产,所以我的 build.gradle 包含:
android {
buildTypes {
release {
runProguard false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
staging {
signingConfig signingConfigs.staging
applicationVariants.all { variant ->
appendVersionNameVersionCode(variant, defaultConfig)
}
}
production {
signingConfig signingConfigs.production
}
}
}
and my appndVersionNameVersionCode looks like:
我的 appndVersionNameVersionCode 看起来像:
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)
}
If I execute task assembleStagingthen I get proper name of my apk, but when I execute assembleProductionthen I get changed names of my apk (like in staging case). For example:
如果我执行任务assembleStaging,那么我会得到我的 apk 的正确名称,但是当我执行assembleProduction 时,我会得到我的 apk 的更改名称(就像在登台情况下一样)。例如:
MyApp-defaultFlavor-production-9.9.9-999.apk
MyApp-defaultFlavor-production-9.9.9-999.apk
It looks like in production build type is executed appendVersionNameVersionCode. How can I avoid it?
看起来在生产构建类型中执行appendVersionNameVersionCode。我怎样才能避免它?
采纳答案by Leszek
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:
正如 CommonsWare 在他的评论中所写的那样,您应该appendVersionNameVersionCode
只调用staging 变体。您可以轻松地做到这一点,只需稍微修改您的appendVersionNameVersionCode
方法,例如:
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)
}
}
回答by Piotr Zawadzki
Lecho's solution doesn't work for Android Gradle Plugin 0.14.3+ because of removal of deprecated APIS: http://tools.android.com/tech-docs/new-build-system
由于删除了已弃用的 API,Lecho 的解决方案不适用于 Android Gradle 插件 0.14.3+:http://tools.android.com/tech-docs/new-build-system
Almost 1.0: removed deprecated properties/methods
...
- Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest (use the variant output)
几乎 1.0:删除不推荐使用的属性/方法
...
- Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest(使用变体输出)
The following works for me:
以下对我有用:
def appendVersionNameVersionCode(variant, defaultConfig) {
variant.outputs.each { output ->
if (output.zipAlign) {
def file = output.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.outputFile = new File(file.parent, fileName)
}
def file = output.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.packageApplication.outputFile = new File(file.parent, fileName)
}
}
回答by Qamar
I have use little generic version of it. build and install fine.
for example your projectName is "Salad" then for staging apk name will be "Salad-staging-dd-MM-YY" you can also change for debug and release apk. hope my solution will be better.
Change in projectName/app/build.gradle
我使用了它的小通用版本。构建和安装很好。例如,您的 projectName 是“Salad”,那么暂存 apk 名称将是“Salad-staging-dd-MM-YY”,您还可以更改调试和发布 apk。希望我的解决方案会更好。
在某一方面的变化projectName/app/build.gradle
buildTypes {
debug{
}
staging{
debuggable true
signingConfig signingConfigs.debug
applicationVariants.all { variant ->
variant.outputs.each { output ->
def date = new Date();
def formattedDate = date.format('dd-MM-yyyy')
def appName = getProjectDir().getParentFile().name
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace(getProjectDir().name +"-staging", "$appName-staging-" + formattedDate)
//for Debug use output.outputFile = new File(output.outputFile.parent,
// output.outputFile.name.replace("-debug", "-" + formattedDate)
)
}
}
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
to change Android app name ref
更改 Android 应用名称参考
回答by Droid Chris
Here is what I have done:
这是我所做的:
def appName
if (project.hasProperty("applicationName")) {
appName = applicationName
} else {
appName = parent.name
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", appName + "_V" + versionCode + ".apk"))
}
}