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.

amira
  • 416
  • 1
  • 7
  • 24

4 Answers4

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']
}
PineapplePie
  • 816
  • 2
  • 13
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
George
  • 2,292
  • 2
  • 10
  • 21
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"
        }
    }
Fakhar
  • 3,946
  • 39
  • 35
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.