Hi everyone, I get stuck this problem I have a st...
# community-support
o
Hi everyone, I get stuck this problem I have a standalone gradle plugin. problem is Apply another plugin to project with some version, based on condition from my plugin. This case can't be handled by settings plugin because the my condition based on extension properties from another gradle plugin, that already added to project.
Copy code
class MyPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        // some condition based on another plugin that already applied
        if (some condition) {
            // should apply plugin with version 1.0.0 
        } else (some condition) {
            // should apply plugin with version 2.0.0
        } else {
            // should apply plugin with version 3.0.0
        } 
    }
}
buildscript dependency won't work I have tried to add another plugin as buildscript dependency but this approach won't work.
Copy code
project.buildscript.dependencies.add(
    "classpath",
    "com.google.devtools.ksp:symbol-processing-gradle-plugin:2.0.0-1.0.22"
)
project.plugins.apply("com.google.devtools.ksp")

// and even

project.afterEvaluate { project.plugins.apply("com.google.devtools.ksp") }
s
In order to apply a plugin, it has to be present as a dependency on the classpath, and in order for it to be present as a dependency on the classpath, a particular version must have been selected at that point. So I don’t think what you want to do is really possible.
o
I've just find the way to do this.
Copy code
// Adding dependency to classpath
buildscript.dependencies.add(
        "classpath",
        "com.google.devtools.ksp:symbol-processing-gradle-plugin:2.0.0-1.0.21"
    )

// Using classloader to load dependency
buildscript.classLoader.apply {
    println("Applying classpath: $it")
    val classLoader = this as VisitableURLClassLoader
    classLoader.apply {
        addURL(it.toURI().toURL())
    }
}

// Adding plugin
plugins.apply("com.google.devtools.ksp")

// Apply dependency as ksp
dependencies.add("ksp", project(":annotation-api"))
s
oh… okay 🙂
v
I wouldn't say that is "the way to do this". I would say this is a veeery dirty hack to achieve what you want. That also only works as long a internals do not change. Maybe you should instead make three variants of your plugin and just let the consumer decide which one to apply.