Home > OS >  Run Gradle task on the basis of Build Variant
Run Gradle task on the basis of Build Variant

Time:02-23

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)?

CodePudding user response:

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.

CodePudding user response:

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