-
Notifications
You must be signed in to change notification settings - Fork 2
Home
dieyi edited this page Jan 3, 2019
·
3 revisions
This guide walks you through the process of creating reusable build logic in a capt plugin.
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, unless you need Android 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 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
}
}
Create the following properties file:
main/resources/META-INF/capt-plugins/com.example.safecatcher.properties
implementation-class=coffeepartner.capt.sample.safecatcher.SafePlugin
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:
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 {
...
}
}
}
}
- Capt will create different configurations for differencet Android variants, you can use it like
implementation
logic. - 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. - Capt in library module will only accept classes in library.
Detailed example is here