Hey, I’m building a Gradle plugin that adds some d...
# kapt
r
Hey, I’m building a Gradle plugin that adds some dependencies based on an extension value but if the dependency is added in the
afterEvaluate
Kapt doesn’t run. Any idea? Here the example:
Copy code
class MyPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        with(project) {
            val extension = extensions.create("projectConfiguration", MyExtension::class.java)
            
            // Adding the dependency in the apply works
            addDependency("kapt", "library-coordinate")
            
            // If the dependency is added in the afterEvaluate block kapt doesn't run.
            // :myproject:dependencies shows the dependency properly
            afterEvaluate {
                if (extension.value) {
                    addDependency("kapt", "library-coordinate")
                }
            }
        }
    }

    private fun Project.addDependency(configuration: String, dependency: String) {
        dependencies.add(configuration, dependency)
    }
}
t
afterEvaluate {}
is discouraged to use for plugin development for quite a long time
what is your idea behind using
afterEvaluate { .. }
for adding kapt dependency? To add it only when kapt plugin is applied?
r
@tapchicoma I’m not super happy about using
afterEvaluate{}
, but I haven’t found another way to read the actual extension value. Do you have any suggestion?
t
try to use
project.plugins.withId("org.jetbrains.kotlin.kapt") { your action that depends on kapt }
r
withId
seems very helpful (actually it solves something else :D) but I’m not how that would solve this issue. I’m sure already that whatever I’m try to do already happens the plugin has applied
t
could you give more info what you want to implement? So far my understanding it that whenever kapt plugin is applied you want to add some dependency?
r
no, I want to add a dependency based on a value specific in a custom extension (https://docs.gradle.org/current/userguide/custom_plugins.html#sec:getting_input_from_the_build)
I want to have something like
Copy code
myExtension {
 useDagger = true
}
and when
userDagger
is
true
I’ll add Dagger dependencies to the project.
t
Copy code
project.plugins.withId("org.jetrains.kotlin.kapt") {
    if (project.extenstions.getByType(MyExtension.class).useDagger) {
    add dagger
}
}
something like this
r
I’ll give it a try
looks like it’s not working… it falls back to the value read the from the extension default
t
ah, then it is a little more complicated. Will be back in a few minutes 🙂
https://github.com/JLLeitschuh/ktlint-gradle/blob/119533446e4be89250b2559e826b5f5d5726b803/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/Configurations.kt#L15 - here is the method that basically also lazy adds a dependency based on extension lazy property. You should adapt it to your needs and call it inside withId action.
r
thanks, will look into it
t
happy to answer questions if you will have some