Skip to content
dieyi edited this page Jan 5, 2019 · 3 revisions

Writing Capt Plugins

This guide walks you through the process of creating reusable build logic in a capt plugin.

Create Project

Capt is built by Gradle, Android library or java library on Gradle is both OK. The cpat plugin will invoked by Gradle, so Gradle's runtime is also avalable. It's recommend to use java or groovy plugin, because Android specific classes is not exists on Gradle runtime.

Your library's build.gradle may like this:

apply plugin: 'com.android.library'
// apply plugin: 'java'
// apply plugin: 'groovy'


dependencies {
    implementation 'coffeepartner.capt:api:1.0.0-RC1'
}

Create the plugin

Create the class GreetingPlugin in the directory you just created, setting its contents to the following:

// @Def is required, it tells capt what type of annotations you need, only Class / Method / MethodParameter annotations are supported.
@Def(supportedAnnotationTypes = "coffeepartner.capt.sample.safecatcher.rt.Match")
public class SafePlugin extends Plugin<Capt> {
    @Override
    public void onCreate(Capt capt) throws IOException {
        // Capt is a interface with variant API
    }
    
    public AnnotationProcessor onProcessAnnotations() {
        // override this method to parse class with your interested annotation declared in @Def
    }
    
    public ClassTransformer onTransformClass() {
        // override this method to transform classes in APK
    }
}

Declare a plugin identifier

Create the following properties file:

main/resources/META-INF/capt-plugins/com.example.safecatcher.properties

implementation-class=coffeepartner.capt.sample.safecatcher.SafePlugin

Use the plugin in Android module

Capt supports both Android application and library module.

root build.gradle:

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.x.x'
        classpath 'coffeepartner.capt:plugin:1.0.0-RC1'
    }
}

module build.gradle:

apply plugin: 'com.android.application' 
// or apply plugin: 'com.android.library'
apply plugin: 'capt'

dependencies {
    // 1
    capt project(":safecatcher")
    debugCapt 'xx:xx:0.0.1'
}

capt {
    plugins {
        // 2
        'com.example.safecatcher' {
            // default both enable
            scope CaptPluginExtension.ASSEMBLE | CaptPluginExtension.ANDROID_TEST 
            
            
            plugin_defined_args1 121322
            plugin_defined_args2 {
                ...
            }
        }
    }
}
  1. Capt will create different configurations for differencet Android variants, you can use it like implementation logic.
  2. In capt.plugins{} block, the 'com.example.safecatcher' means register this id to capt, this line is required although you don't pass other arguments in block. Capt will load the plugin correspond to the id.
  3. Capt in library module will only accept classes in library.

Detailed example is here

Clone this wiki locally