Gradle: How to Build different flavours of different built types?
Asked Answered
H

4

114

Could someone tell me if it's possible to build only one of my different flavors through the command-line?

At the moment I haven't seen the way to execute, for example:

gradle buildDev 

when Dev is one of my different flavors. Indeed, I have to execute:

gradle build

And all flavors are build.

I'd like to skip some flavors. is it possible?

Thanks

Hessite answered 23/1, 2014 at 11:55 Comment(1)
first try to run gradle without params, then read output ... there will be hint to run gradle tasks ...Sight
K
182

While there is no flavor-specific version of the build task, there are flavor-specific versions of the assemble and install tasks. assemble will create the APK; install will install it on devices/emulators.

For example, in this sample project, I define two product flavors (chocolate and vanilla) and three total build types (debug, release, and mezzanine).

Running gradle tasks shows, among others:

Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleChocolate - Assembles all builds for flavor Chocolate
assembleChocolateDebug - Assembles the Debug build for flavor Chocolate
assembleChocolateDebugTest - Assembles the Test build for the ChocolateDebug build
assembleChocolateMezzanine - Assembles the Mezzanine build for flavor Chocolate
assembleChocolateRelease - Assembles the Release build for flavor Chocolate
assembleDebug - Assembles all Debug builds
assembleMezzanine - Assembles all Mezzanine builds
assembleRelease - Assembles all Release builds
assembleTest - Assembles all the Test applications
assembleVanilla - Assembles all builds for flavor Vanilla
assembleVanillaDebug - Assembles the Debug build for flavor Vanilla
assembleVanillaDebugTest - Assembles the Test build for the VanillaDebug build
assembleVanillaMezzanine - Assembles the Mezzanine build for flavor Vanilla
assembleVanillaRelease - Assembles the Release build for flavor Vanilla

Install tasks
-------------
installChocolateDebug - Installs the Debug build for flavor Chocolate
installChocolateDebugTest - Installs the Test build for the ChocolateDebug build
installChocolateMezzanine - Installs the Mezzanine build for flavor Chocolate
installChocolateRelease - Installs the Release build for flavor Chocolate
installVanillaDebug - Installs the Debug build for flavor Vanilla
installVanillaDebugTest - Installs the Test build for the VanillaDebug build
installVanillaMezzanine - Installs the Mezzanine build for flavor Vanilla
installVanillaRelease - Installs the Release build for flavor Vanilla
uninstallAll - Uninstall all applications.
uninstallChocolateDebug - Uninstalls the Debug build for flavor Chocolate
uninstallChocolateDebugTest - Uninstalls the Test build for the ChocolateDebug build
uninstallChocolateMezzanine - Uninstalls the Mezzanine build for flavor Chocolate
uninstallChocolateRelease - Uninstalls the Release build for flavor Chocolate
uninstallVanillaDebug - Uninstalls the Debug build for flavor Vanilla
uninstallVanillaDebugTest - Uninstalls the Test build for the VanillaDebug build
uninstallVanillaMezzanine - Uninstalls the Mezzanine build for flavor Vanilla
uninstallVanillaRelease - Uninstalls the Release build for flavor Vanilla
Kaiserslautern answered 23/1, 2014 at 12:1 Comment(19)
Then, when I want to build the flavour's APK I have to use the assembleXXX. Cool. Thanks.Hessite
@Lechon: gradle assembleChocolateDebug will result in build/apk/HelloProductFlavors-chocolate-debug-unaligned.apk being placed in your project. Though, I cannot rule out the possibility that this only works if the flavor is tasty. :-)Kaiserslautern
What about just putting assembleDebugTest? This is like assembleDebug.Russia
@IgorGanapolsky: There is no assembleDebugTest once you introduce product flavors.Kaiserslautern
@Kaiserslautern Ah that is interesting, because when I list my tasks (and I have product flavors) it indeed does show me a task called assembleDebugTestVeliavelick
@Zainodis: This answer is over a year old, and it is possible that they have added a task that assembles the debug build type for all product flavors.Kaiserslautern
@Kaiserslautern Ah true that, thanks for the hint! The Android gradle build system does still undergo a lot of changes it seems.Veliavelick
@Zainodis: It should slow down some now that Gradle for Android 1.0 has shipped.Kaiserslautern
If you have modules, don't forget the module prefix :<module>:assemble<FlavorName>Giles
this link is dead - is there a new one?Histoplasmosis
@Dwebtron: If you are simply looking for a build.gradle with product flavors, see github.com/commonsguy/cw-omnibus/blob/v9.0/Manifest/Merger/app/…Kaiserslautern
Is there a way to specify product flavour as command line argument? More specifically something like ./gradlew assembleDebug -Dflavour=chocolate?Affricative
@ShivamPokhriyal: Use ./gradlew assembleChocolateDebug.Kaiserslautern
Yeah, that would definitely work, I know. I kinda need some sort of way to specify the product flavor as an argument. Actually, I'm using ./gradlew assembleAndroidTest -DtestType=debug command which basically generates android test debug apk for all flavour, and I'd like to restrict it to a single one. But whenI use ./gradlew assembleFLAVOR1AndroidTest -DtestType=debug it says task not found.Affricative
@ShivamPokhriyal: "I kinda need some sort of way to specify the product flavor as an argument" -- sorry, I do not know of a way to do that. If you are using some outer program (e.g., shell script) to run the ./gradlew command, it could use string concatenation or interpolation to add the flavor in the proper spot in the assemble...Debug argument.Kaiserslautern
Ohh, that does sounds like a good option. Thanks! I was probably thinking of using variant filters maybe or explore some other built-in gradle stuff before using a script. Again thanks!Affricative
How to assemble all release builds for flavor chocolate?Cabinda
@Sumit: In this example, there is only one release build for chocolate, so it would be assembleChocolateReleaseKaiserslautern
I have multiple dimensions and multiple flavors in app, managed by using variantFilter in build.gradle and disable buildType=="debug" flavor, Thanks @KaiserslauternCabinda
A
33

I would simplify the answer given by @CommonsWare because going through the answer i was litte confused.

Consider these are the product flavours

  • Dev
  • Preprod
  • Prod

Run

gradlew task

This will list out all Product flavours along with there build types

assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleDEV - Assembles all DEV builds.
assemblePREPROD - Assembles all PREPROD builds.
assemblePROD - Assembles all PROD builds.
assembleRelease - Assembles all Release builds.

From this you can easily choose the flavours and will generate a build based on that

gradlew assemblePREPROD

Antilogism answered 3/1, 2016 at 13:14 Comment(0)
W
25

If your productFlavor is chocolate you can do

./gradlew assembleChocolateRelease

or

./gradlew assembleChocolateDebug
Walkthrough answered 24/12, 2018 at 7:42 Comment(0)
R
5

To add to the above answers, if you want to build an Android Bundle (AAB) then you can use this

# build flavor 'flavorName' only
./gradlew bundleFlavorName
Robrobaina answered 28/12, 2019 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.