Hello! I’m trying to apply ksp inside a convention...
# gradle
g
Hello! I’m trying to apply ksp inside a convention plugin like:
Copy code
with(pluginManager) {
    apply("com.android.library")
    apply("com.google.devtools.ksp")
}
but it always fails saying it cant be found. If i add it like this (in the build.gradle):
Copy code
plugins {
    alias(libs.plugins.ksp)
}
it works. Am i missing any syntax? I’ve tried also like this:
Copy code
val catalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
with(pluginManager) {
       apply("com.android.library")
       apply("com.google.devtools.ksp:${catalog.findVersion("gradleGoogleKsp").get()}")
}
But with no luck 🤔
v
For applying plugins, you do not need to use
pluginManager
, you can directly call
apply
or even better you should use the
plugins { ... }
block in your convention plugin. Do you have a dependency on the plugin you try to apply in the build script of the project that builds your convention plugin?
g
This is what I want (I have the same setup for dagger.hilt that uses kapt):
Copy code
class MyPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            val catalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
            with(pluginManager) {
                apply("com.android.library")
                apply("com.google.devtools.ksp")//:${catalog.findVersion("gradleGoogleKsp").get()}")
            }
            extensions.configure<LibraryExtension> { addVariantOptions() }
            dependencies {
                add("implementation", catalog.findLibrary...)
                add("ksp", catalog.findLibrary...)
            }
        }
    }

    private fun LibraryExtension.addVariantOptions() {
        apply {
            libraryVariants.all {
                sourceSets {
                    getByName(name) {
                        kotlin.srcDir("build/generated/ksp/$name/kotlin")
                    }
                }
            }
        }
    }
}
so that in my library modules build.gradle that uses this library it’s just one line:
Copy code
plugins {
    id("buildlogic.plugins.myplugin")
//    alias(libs.plugins.ksp)
}
are you suggesting replacing
with(pluginManager)
with
with(plugins)
? It throws the same error 😞
v
Ah, I thought you use a precompiled script plugin. In a full binary plugin you use
apply
method of course, but you can just use
Copy code
apply("com.android.library")
apply("com.google.devtools.ksp")
instead of
Copy code
with(pluginManager) {
    apply("com.android.library")
    apply("com.google.devtools.ksp")
}
as
Project
is
PluginAware
. It is not wrong, just unnecessary boilerplate.
plugins
should actually never be used according to its JavaDoc. But of course it throws the same error. As I said, you need to add the plugin as
implementation
dependency to the project building that convention plugin or the plugin is not on the class path and cannot be found.
g
oh that totally makes sense 🤦 I’ve just added to the classpath (com.google.devtools.ksp.gradle.plugin) and its ok now 🙂
625 Views