Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the APK file name for product flavors?

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 and baseName 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 
like image 983
JJD Avatar asked Aug 03 '14 11:08

JJD


People also ask

How do I change the name of an APK file?

Navigate to the folder in which your apk is present. In that folder select your APK file. Right-click on it and click on rename option to rename your APK file.

How do I make android Flavours?

To write flavor specific code, create the folder with the same name as the flavor. The java classes with the same name in the flavor folders won't override the main folder. The res folder in the main should only have those directories that are common to all flavors.

What is missingDimensionStrategy?

TL;DR: If a library module includes a flavor dimension that the app flavor does not, then use missingDimensionStrategy to specify default flavors from the missing dimension. More generally, you can use this any time a consumed module includes a flavor that the consumer module does not.

What are build variants?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.


1 Answers

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.

like image 115
Lakshman Chilukuri Avatar answered Sep 21 '22 12:09

Lakshman Chilukuri