113

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

Shiva
  • 11,485
  • 2
  • 67
  • 84
Jose M Lechon
  • 5,766
  • 6
  • 44
  • 60
  • first try to run `gradle` without params, then read output ... there will be hint to run `gradle tasks` ... – Selvin Jan 23 '14 at 12:01

4 Answers4

179

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
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Then, when I want to build the flavour's APK I have to use the assembleXXX. Cool. Thanks. – Jose M Lechon Jan 23 '14 at 12:16
  • 12
    @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. :-) – CommonsWare Jan 23 '14 at 12:39
  • What about just putting assembleDebugTest? This is like assembleDebug. – IgorGanapolsky Feb 20 '14 at 21:04
  • @IgorGanapolsky: There is no `assembleDebugTest` once you introduce product flavors. – CommonsWare Feb 20 '14 at 21:15
  • @CommonsWare Ah that is interesting, because when I list my tasks (and I have product flavors) it indeed does show me a task called assembleDebugTest – AgentKnopf Jan 23 '15 at 11:18
  • 2
    @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. – CommonsWare Jan 23 '15 at 12:29
  • @CommonsWare Ah true that, thanks for the hint! The Android gradle build system does still undergo a lot of changes it seems. – AgentKnopf Jan 23 '15 at 13:07
  • 1
    @Zainodis: It should slow down some now that Gradle for Android 1.0 has shipped. – CommonsWare Jan 23 '15 at 13:10
  • 3
    If you have modules, don't forget the module prefix `::assemble` – Torge Mar 30 '16 at 17:41
  • this link is dead - is there a new one? – Dwebtron Mar 18 '20 at 22:07
  • 1
    @Dwebtron: If you are simply looking for a `build.gradle` with product flavors, see https://github.com/commonsguy/cw-omnibus/blob/v9.0/Manifest/Merger/app/build.gradle – CommonsWare Mar 18 '20 at 22:17
  • Is there a way to specify product flavour as command line argument? More specifically something like `./gradlew assembleDebug -Dflavour=chocolate`? – Shivam Pokhriyal Sep 08 '20 at 11:33
  • @ShivamPokhriyal: Use `./gradlew assembleChocolateDebug`. – CommonsWare Sep 08 '20 at 11:34
  • 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. – Shivam Pokhriyal Sep 08 '20 at 11:37
  • 1
    @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. – CommonsWare Sep 08 '20 at 11:41
  • 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! – Shivam Pokhriyal Sep 08 '20 at 11:47
  • How to assemble all release builds for flavor chocolate? – Sumit Feb 27 '23 at 13:23
  • @Sumit: In this example, there is only one release build for chocolate, so it would be `assembleChocolateRelease` – CommonsWare Feb 27 '23 at 13:43
  • I have multiple dimensions and multiple flavors in app, managed by using variantFilter in build.gradle and disable buildType=="debug" flavor, Thanks @CommonsWare – Sumit Feb 28 '23 at 11:02
31

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

droid kid
  • 7,569
  • 2
  • 32
  • 37
25

If your productFlavor is chocolate you can do

./gradlew assembleChocolateRelease

or

./gradlew assembleChocolateDebug
Eric Kim
  • 10,617
  • 4
  • 29
  • 31
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
EFreak
  • 3,119
  • 3
  • 27
  • 31