用Gradle更改apk名称

我有一个使用Gradle构build过程的Android项目。 现在我想添加两个额外的构buildtypes的阶段和生产,所以我的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 } } } 

和我的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) } 

如果我执行任务assembleStaging然后我得到我的apk的正确名称,但是当我执行assembleProduction然后我得到我的apk的更改名称(如在分期大小写)。 例如:

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

它看起来像在生产构buildtypes执行appendVersionNameVersionCode 。 我怎样才能避免它?

正如CommonsWare在他的评论中写的,你应该只为appendVersionNameVersionCode变体调用appendVersionNameVersionCode 。 你可以很容易地做到这一点,只是稍微修改你的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) } } 

Lecho的解决scheme不适用于Android Gradle Plugin 0.14.3+,因为删除了弃用的APIS: http ://tools.android.com/tech-docs/new-build-system

几乎1.0:删除不推荐使用的属性/方法

  • Variant.packageApplication / zipAlign / createZipAlignTask / outputFile / processResources / processManifest(使用变体输出)

以下为我工作:

 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) } } 

我已经使用它的小通用版本。 build立和安装好。 例如你的projectName是“沙拉”,那么对于分级apk名称将是“Salad-staging-dd-MM-YY”,你也可以改变为debugging和释放apk。 希望我的解决scheme会更好。
更改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' } 

改变Android应用程序名称参考

这是我所做的:

  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")) } }