How to customize the APK file name for product flavors?
29,737
Solution 1
Try to put this in your android closure of build.gradle
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");
}
}
}
Now the outputs should be like green.apk
and blue.apk
.
Solution 2
This will help you in 2022.
android {
//........
flavorDimensions "version"
productFlavors {
Free {
dimension "version"
applicationId "com.exampleFree.app"
}
Paid {
dimension "version"
applicationId "com.examplePaid.app"
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def appId = variant.applicationId// com.exampleFree.app OR com.examplePaid.app
def versionName = variant.versionName
def versionCode = variant.versionCode // e.g 1.0
def flavorName = variant.flavorName // e. g. Free
def buildType = variant.buildType.name // e. g. debug
def variantName = variant.name // e. g. FreeDebug
//customize your app name by using variables
outputFileName = "${variantName}.apk"
}
}}
Apk name FreeDebug.apk
Solution 3
For Android Gradle Plugin 0.13.+ you should use something like this:
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);
}
}
}
}
Solution 4
For Android Studio 3.0 you must change from:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, "whatever.apk")
}
}
To:
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "whatever.apk"
}
}
Solution 5
I did it like this:
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"));
}
}
}
}
Related videos on Youtube
Author by
JJD
Android, Kotlin, Java, Git, Python, Ruby, Ruby on Rails, JavaScript, MacOS, Ubuntu #SOreadytohelp http://stackoverflow.com/10m
Updated on January 26, 2022Comments
-
JJD over 2 years
I am customizing the name of the APK file of my Android application within the
build.gradle
script as follows:android { defaultConfig { project.ext.set("archivesBaseName", "MyApplication"); } }
Now that I am using product flavors:
android { productFlavors { green { applicationId "com.example.myapplication.green" } blue { applicationId "com.example.myapplication.blue" } } }
Is there a way to customize the name of each APK? I experimented with
archiveBaseName
andbaseName
without success. In the end I want to come up with the following files: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
-
JJD almost 10 yearsWhy do you check the product flavor name if you do the same for both "freeflavor" and "proflavor" anyways? What I really want is to change the "Modulename" as in your example - not appending something.
-
JJD almost 10 yearsStill: what is the reason you have two
if
blocks as I asked before? -
Lakshman Chilukuri almost 10 yearsYes. It is not serving much purpose here and can be further simplified.
-
JJD almost 10 yearsAlmost there. (1) For some reason the
-unaligned.apk
files inbuild/outputs/apk
still carry theMyApplication
prefix. (2) The variant is not capitalized. -
JJD almost 10 yearsAny idea how to complete this?
-
Jelle almost 10 years(1) According to line 1316 in the BasePlugin.groovy-link, you can change the
project.archivesBaseName
andvariantData.variantConfiguration.baseName
to adapt the unaligned-outputname. But I'm note sure if this will mess with other build-steps. Still, you shouldn't be using the unaligned-version, but rather the final apk (of which you can fully control the apk-name). (2) variant.name.capitalize() -
Quintin Balsdon about 8 yearsThe if statement rounded brackets are not complete... Although even putting them there has no impact for me
-
Sky Kelsey about 7 yearsThis has stopped working in Android Gradle 3.0.0-alpha1. You can no longer access the outputFile it seems.
-
William T. Mallard almost 7 yearsThis didn't work, missing paren and apparently incompatible with the Android plugin? I was getting an error about undefined attribute outputFile. See my answer below that shows how I fixed it.
-
IgorGanapolsky over 5 yearsWhat is outputFile??
-
AtomicBoolean over 5 yearsReally useful if you don't want to update your gradle version!
-
thiagolr over 4 yearsNot working for me, it does nothing. I added a "println outputFileName" and it is printing correctly, but for some reason gradle is not using the "outputFileName" value for the file name. By the way, I'm trying to use it for an app bundle, not an APK.
-
chrjs over 2 yearsDoes not work when you use Bundles in 2021.
-
Slion over 2 yearsWorks great with APK.