0

I have been struggling on this for past 2 days. There are several solutions on stack overflow which didn't work for me. I am trying to ignore gradle task for prod environment. To execute my task i.e. dokkaHtml on build, I am using this command -

tasks.named('preBuild') { finalizedBy(dokkaHtml) }

Is there a way to disable dokkaHtml task when running on different build variant(eg - ignore the task for production builds)?

Archi
  • 11
  • 2
  • Which task do you want to ignore? `preBuild`? I'm afraid it's not possible, because it runs some checks, etc. that are necessary to build your variant. Also, what do you mean by "prod environment"? Is it the `release` build type? – romtsn Feb 17 '22 at 08:46
  • I want to ignore dokkaHtml. Right now it will run after pre build everytime, but I dont want it to be executed in production environment i.e for different build variant. I have 3 product flavors in my gradle file - dev, test and prod. – Archi Feb 17 '22 at 17:24

2 Answers2

1

I am checking the current product flavor and based on the result, enabling the gradle task. This solution is working for me -


def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    Pattern pattern
    if( tskReqStr.contains( "assemble" ) )
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

    Matcher matcher = pattern.matcher( tskReqStr )
    if( matcher.find() )
        return matcher.group(1).toLowerCase()
    else
        return ""
}

var currentFlavor = getCurrentFlavor().toString()
//disable dokka for prod environment
if(!currentFlavor.contains("production")){
    tasks.preBuild.finalizedBy(dokkaHtml)
}
Archi
  • 11
  • 2
0

You could try to connect dokka task to a flavor-specific preBuild, so in your case, it would be something like this:

tasks.named('preDevDebugBuild') { finalizedBy(dokkaHtml) }
tasks.named('preDevReleaseBuild') { finalizedBy(dokkaHtml) }
tasks.named('preTestDebugBuild') { finalizedBy(dokkaHtml) }
tasks.named('preTestReleaseBuild') { finalizedBy(dokkaHtml) }

This way you omit dokka for the prod flavor.

romtsn
  • 11,704
  • 2
  • 31
  • 49
  • Thanks for the response. Though, the solution looks promising, but it didn't work for me. Gradle couldn't find task named- preDevDebugBuild. – Archi Feb 20 '22 at 20:10
  • What if you wrap this and put inside `afterEvaluate` block? – romtsn Feb 20 '22 at 20:26
  • Adding this will not be able to figure out the current build variant, I am building into. I have found the other solution wherein I am calling the method to get the build variant and enabling the gradle task based on the value. Let me add the solution. – Archi Feb 22 '22 at 19:08