Gradle task to take a build of specific build flavors (more than one)
Asked Answered
O

4

8

In my app, I have more than 30 build variants. Every time when I release the app, I need to publish it to different platforms, therefore I build 5 different build variants.

Currently, I am doing this:

  • switch to build variant A
  • wait for the Gradle build
  • build APK/Bundle of build variant A
  • the same steps for B, C, E, and D.

What I am looking for is a Gradle task that just builds me these specific build variants when I run it. I know there is a task to build all build variants but it is too much for me.

I searched SO but couldn't find anything related to a point that I started to think it is impossible.

Could someone point me in the right direction? Thanks.

Orthopedics answered 2/8, 2022 at 18:30 Comment(3)
Maybe do it by command line?Felder
yeah, but I was thinking about how we would write that Gradle task that generates apks/aabs for specific build variants. By that, I mean one single task to build 5 build flavors.Orthopedics
https://mcmap.net/q/188590/-gradle-how-to-build-different-flavours-of-different-built-typesFelder
C
0

Calling a gradle task from another gradle task is not the best idea. You should rather describe their relationship as it usually works with all other gradle parts - by using mustRunAfter, dependsOn and so on. For your purposes you can use GradleBuild. I think you're searching for this - if I got your point right :D

I would assume that your task will look something like that (add your flavours instead of mine mocked)

task assembleFlavourBuilds(type: GradleBuild) {
    description = 'creating flavour builds for the provided config'
    tasks = ['assembleFree', 'assemblePro']
}
Crest answered 2/8, 2022 at 20:44 Comment(0)
D
0

If you can use CommandLine then Gradle has good support for that.

For example :

task makeDir(type: Exec) {
    workingDir "."
    commandLine("cmd", "/c", "mkdir", "example")
}

Am just trying to show an example of how this work, by creating a folder here named example.

You can even add this to be automated with an already defined task, Like a build task. This can be done by using finalizeBy

tasks.named("build") { finalizedBy("makeDir") }

This will only call the task after a successful build.

And my suggestion is to make a .bat file with all the needed commands and call it in the same way as the following code :

task BuildAll(type: Exec) {
    workingDir "."
    commandLine("cmd", "/c", "mybat.bat")
}

And mybat.bat will contain all the needed commands to

  • switch build variant
  • build
  • bundle
  • repeat
Decathlon answered 2/8, 2022 at 22:10 Comment(1)
Can you explain commands in batch file?Felder
S
0

Switch Build variant

enter image description here

productFlavors {
        variantA {
            dimension "version"
            versionNameSuffix ".a"
        }
       variantB {
            dimension "version"
            versionNameSuffix ".b"
        }

       variantC {
            dimension "version"
            versionNameSuffix ".c"
        }

       variantD {
            dimension "version"
            versionNameSuffix ".d"
        }

       variantE {
            dimension "version"
            versionNameSuffix ".e"
        }
    }
Sackcloth answered 27/10, 2022 at 7:1 Comment(1)
Could you give more explanation @Fakhar?Orthopedics
T
0

you can use gradle task. in your root build.gradle define task like bellow:

task assembleFlavorBuilds(type: GradleBuild) {
    description = 'Description of task.'
    tasks = ['assembleFlavorName1Debug', 'assembleFlavorName1Release']
}

each of item in tasks are like this: "assemble"+FlavorName(first of name must be capital)+build type(Debug or Release).

for instance if you have flavors with names "free" and "paid", your task must be like this:

task assembleFlavorBuilds(type: GradleBuild) {
    description = 'Description of task.'
    tasks = ['assembleFreeRelease', 'assemblePaidRelease']
}

after sync project you can see this task in gradle area in idea or just click on run icon next of this task.

Tadd answered 26/11, 2022 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.