如何自定义产品口味的APK文件名?

我在build.gradle脚本中自定义Android应用程序的APK文件的名称,如下所示:

 android { defaultConfig { project.ext.set("archivesBaseName", "MyApplication"); } } 

现在我正在使用产品口味:

 android { productFlavors { green { applicationId "com.example.myapplication.green" } blue { applicationId "com.example.myapplication.blue" } } } 

有没有办法自定义每个APK的名称? 我尝试使用archiveBaseNamebaseName但没有成功。 最后我想提出以下文件:

 build/outputs/apk/Blue-debug-1.2.1.apk build/outputs/apk/Blue-debug-unaligned.apk build/outputs/apk/Blue-release-1.2.1.apk build/outputs/apk/Blue-release-unaligned.apk build/outputs/apk/Green-debug-1.2.1.apk build/outputs/apk/Green-debug-unaligned.apk build/outputs/apk/Green-release-1.2.1.apk build/outputs/apk/Green-release-unaligned.apk 

Solutions Collecting From Web of "如何自定义产品口味的APK文件名?"

尝试将它放在build.gradle的android闭包中

 buildTypes { debug { // debug buildType specific stuff } release { // release buildType specific stuff } applicationVariants.all { variant -> if (variant.buildType.name.equals("release") && variant.productFlavors[0].name.equals("green") && variant.zipAlign) { def apk = variant.outputFile; variant.outputFile = new File(apk.parentFile, "green.apk"); } else if(variant.buildType.name.equals("release") && variant.productFlavors[0].name.equals("blue") && variant.zipAlign) { def apk = variant.outputFile; variant.outputFile = new File(apk.parentFile, "blue.apk"); } } } 

现在输出应该像green.apkblue.apk

对于Android Gradle Plugin 0.13。+你应该使用这样的东西:

 android{ buildTypes { applicationVariants.all { variant -> variant.outputs.each { output -> def apk = output.outputFile; def newName = "mysms-" + variant.baseName.replace("-release", "") + "-" + defaultConfig.versionName + ".apk"; output.outputFile = new File(apk.parentFile, newName); } } } } 

对于Android Studio 3.0,您必须更改:

 applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parent, "whatever" + ".apk") } } 

至:

 android.applicationVariants.all { variant -> variant.outputs.all { outputFileName = "whatever" + ".apk") } } 

我是这样做的:

 productFlavors { production { applicationId "com.example.production" } staging { applicationId "com.example.production.staging" } applicationVariants.all { variant -> variant.outputs.each { output -> if(variant.productFlavors[0].name.equals("staging")){ output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-staging-release", "test")); }else{ output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-production-release", "production")); } } } } 

你需要这个是奇怪的,因为默认情况下apk文件已经不同了。

如果您在第1346行查看,您可以看到variantData.variantConfiguration.baseName在outputFile中使用。

 variantData.outputFile = project.file("$project.buildDir/apk/${project.archivesBaseName}-${variantData.variantConfiguration.baseName}.apk") 

baseName的文档是

 /** * Full, unique name of the variant, including BuildType, flavors and test, dash separated. * (similar to full name but with dashes) */ private String mBaseName; 

所以运行gradle assembleFreeDebug应该会得到一个ProjectName-free-debug.apk文件。

但如果不是这种情况,或者您需要不同的文件名,则可以使用以下代码进行自定义。

 android { buildTypes { debug {} alpha {} release {} } productFlavors { free{} paid{} } applicationVariants.all { variant -> def newApkName = variant.name + "my-custom-addition" + ".apk"; variant.outputFile = new File("${project.buildDir}/outputs/apk/", newApkName); } } 

这项工作对我来说:

在APK名称中添加variant.productFlavors[0].name

代码示例:

 buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parent, "APPNAME_" + variant.productFlavors[0].name + "_" + variant.versionName + ".apk") } } } } 

Android Gradle Plugin 0.13.0已弃用用于以下内容的outputFile:

 applicationVariants.all { variant -> def newApkName = variant.name + "my-custom-addition" + ".apk"; variant.outputFile = new File("${project.buildDir}/outputs/apk/", newApkName); } 

对我有用的解决方案是:

 android { ... } project.archivesBaseName = "AndroidAppGeneric-${_buildVersionNameForMaven}" 

我正在使用以下内容,因此文件不会互相覆盖:

 applicationVariants.all { variant -> variant.outputs.each { output -> def newApkName = variant.name + "-" + variant.versionName + "(" + variant.versionCode +")" + ".apk"; output.outputFile = new File("${project.projectDir}/outputs/apk/" + variant.name, newApkName); } } 

这是我上面的Lakshman答案的变体(heh),不知道为什么我需要“variants.outputs.each”,但我做到了。

 defaultConfig { applicationId "my.company.com" minSdkVersion 16 targetSdkVersion 25 applicationVariants.all { variant -> if (variant.productFlavors[0].name.equals("VariantA")) { variant.outputs.each { output -> def apk = output.outputFile; output.outputFile = new File(apk.parentFile, "Blue.apk"); } } else { // Only two variants variant.outputs.each { output -> def apk = output.outputFile; output.outputFile = new File(apk.parentFile, "Green.apk"); } } } }