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
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With