Create a plugin that adds plugins and dependencies temporarily

Hi guys!

I was wondering if it was possible to create a gradle plugin which temporarily adds other plugins and dependencies before performing a task and then remove (reversing the effects of adding them) afterwards ?

I know it sounds weird but here is my use case : in Kotlin, there is no way to generate code based on code analysis, except when targeting the JVM when we can use a java annotation processor. The problem is that I’d like to generate code in a multiplatform (common) module. For those who don’t know Kotlin, a typical gradle project targeting the JVM applies the kotlin plugin and depends on the kotlin-stdlib library. On the other hand, a multiplatform (common) Kotlin project applies the kotlin-platform-common plugin and depends on the kotlin-stdlib-common library.

To solve my problem, I thought that I can temporarily disable the kotlin-platform-common plugin and remove the kotlin-stdlib-common dependency of my project, replace them by the JVM plugin/dependency, run the build (that will run my annotation processor and generate some code), put the generated code in a source set of my common project, put back the original plugin and dependency and run the build again.

Is it feasible, or is it completely unrealistic ?

Thanks a lot !