How to configure a groovy plugin with custom sourceSet extension in Kotlin DSL using AndroidSourceSet

I’ve written a thorough presentation of the issue here

To summarize I want to use a gradle groovy plugin i android, which extends sourceSet. However I cannot get that sourceSet extension from Android as its an AndroidSourceSet.

I tried various tricks.

I cannot get dynamic-groovy-plugin to work at all, gradle 6.7.1 complains if I add that plugin, as mentioned in Kotlin Dsl Primer. It could be a general interesting method to fix such troublesome cases.

Also I couldn’t seem to get extensions via the sourceSet.withGroovyBuilder , though I’m not sure that’s the intended purpose.

I couldn’t figure out how to get an SourceSet from AndroidSourceSet.

I kind of found an answer here: How to access `extensions` on any DSL object statically?

It shows how to access the groovy extensions for any object, which was the biggest part of my problem.