Home > Enterprise >  Gradle task to take a build of specific build flavors (more than one)
Gradle task to take a build of specific build flavors (more than one)

Time:08-03

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.

CodePudding user response:

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']
}

CodePudding user response:

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("cmd") }

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
  • Related