Q:

How to pass arguments to kapt compiler dynamicly inside my gradle plugin?

I'm currently writing a Gradle plugin to inject some arguments to an annotation processor. It works fine when using annotationProcessor in a Java based Android project. When I tried kapt in a Kotlin based project, it didn't work since kapt collected args from its own configurations.
What I did so far:
val args = variant.javaCompileOptions.annotationProcessorOptions.arguments
args["key"] = "value"

What I knew for kapt args passing is that you can set them via its extensions in the build.gradle, link: 
kapt {
    arguments {
        arg("key", "value")
    }
}

So how to pass arguments to kapt compiler dynamicly inside my gradle plugin? Thanks.

A:

Unfortunately it's not well documented and I have run into this particular issue before. Here is what I did
in your plugin apply function do this (target is the gradle project that the plugin is being applied to). If you  are applying the plugin into your root project then simply put the same code into target.subProjects{...}
target.afterEvaluate{
    target.plugins.all {
        when (it) {
            is AppPlugin -> {
                extensions[AppExtension::class].run {
                    configureJavaCompilerArguments("option1", "option2")
                }
            }
            //if your project has a library that requires/use kapt also do this
            is LibraryPlugin -> {
                extensions[LibraryExtension::class].run {
                    configureJavaCompilerArguments("option1", "option2")
                }
            }
         }
    }
}

For simplicity I have declared a fancy extension function to add the arguments
fun BaseExtension.configureJavaCompilerArguments(option1:String, option2: String) {
    with(defaultConfig.javaCompileOptions.annotationProcessorOptions) {
        includeCompileClasspath = true
        arguments(mapOf(
                "some.key.in.your.ap.1" to option1,
                "some.key.in.your.ap.2" to option2
        ))
    }
}

and also this fancy function that I took from Butter-knife source code
internal operator fun <T : Any> ExtensionContainer.get(type: KClass<T>): T {
    return getByType(type.java)
}

now in your annotation processor just declare interest to these options specifying the same keys!! that is so important.
Happy coding!

