Skip to content

Instantly share code, notes, and snippets.

@ghale
Created June 25, 2020 21:42
Show Gist options
  • Save ghale/a6ca91d034333ae4de6a227154a3ccba to your computer and use it in GitHub Desktop.
Save ghale/a6ca91d034333ae4de6a227154a3ccba to your computer and use it in GitHub Desktop.
Capture Kotlin processor options as custom value
import java.lang.reflect.Field
tasks.withType(getKaptWithoutKotlincTaskClass()).configureEach { task ->
doFirst {
buildScan.value "${task.path}.processorOptions", getCompilerPluginOptions(task).collect { "${it.key} = ${it.value}" }.join("\n")
}
}
static def getCompilerPluginOptions(Task task) {
def processorOptionsField = getAccessibleField(task.class, "processorOptions")
def compilerPluginOptions = processorOptionsField.get(task)
return compilerPluginOptions.subpluginOptionsByPluginId['org.jetbrains.kotlin.kapt3']
}
static Class<?> getKaptWithoutKotlincTaskClass() {
return Class.forName("org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask")
}
private static Field getAccessibleField(Class<?> clazz, String fieldName) {
for (Field field : clazz.declaredFields) {
if (field.name == fieldName) {
field.setAccessible(true)
return field
}
}
if (clazz.superclass != null) {
return getAccessibleField(clazz.superclass, fieldName)
} else {
throw new RuntimeException("Field '${fieldName}' not found")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment