Skip to content

How to Create Customized Transformer

Johnson Lee edited this page Jun 27, 2019 · 2 revisions

Implementing Transformer SPI

Booster provides Binary Transformer, ASM-based Transformer and Javassist-based Transformer, the following sample code shows how to create an instance of transformer for bytecode manipulation.

MyTransformer.kt

@AutoService(ClassTransformer::class)
class MyTransformer : ClassTransformer {

    override fun transform(context: TransformContext, klass: ClassNode): ClassNode {
        // Do whatever you want
        return klass
    }

}

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>"
            ......
        }
    }

Sample Code

Please see transformer-with-asm and transformer-with-javassist