'Run Gradle task on the basis of Build Variant
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)?
Solution 1:[1]
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.
Solution 2:[2]
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)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | romtsn |
| Solution 2 | Archi |
