Skip to content

How to Create Customized Task

johnsonlee edited this page Apr 24, 2019 · 1 revision

Implementing VariantProcessor SPI

The following sample code shows how to create an instance of MyTask for each variant.

MyVariantProcessor.kt

@AutoService(VariantProcessor::class)
class MyVariantProcessor : VariantProcessor {

    override fun process(variant: BaseVariant) {
        val tasks = variant.scope.globalScope.project.tasks
        tasks.create("my${variant.name.capitalize()}Task", MyTask::class.java) {
            it.variant = variant
            it.outputs.upToDateWhen { false }
        }
    }

}

MyTask.kt

open class MyTask : DefaultTask() {

    lateinit var variant: BaseVariant

    @TaskAction
    fun run() {
        // Do whatever you want
    }

}

Configuring

Typically, there are two approaches to apply customized tasks:

  • Applying with buildSrc

    Just simply put the classes above into ${rootProject}/buildSrc/src.

  • Applying as standalone module

    For this approach, a standalone gradle module have to be created, and then, add this module into buildscript classpath:

    buildscript {
        ......
        dependencies {
            classpath "com.didiglobal.booster:booster-gradle-plugin:$booster_version"
            classpath "<standalone-group-id>:<standalone-artifact-id>:<standalone-version>"
            ......
        }
    }