i’m migrating from groovy to kotlin and i have a q...
# gradle
s
i’m migrating from groovy to kotlin and i have a question about extensions. I would like to create an extension in a kotlin file under
buildSrc
and access that extension from all the gradle scripts in my project. I was under the impression that it was possible since all files under
buildSrc
are automatically added to the class path but it doesn’t seem to work for me. When i define the extension in the same class as its usage, it works fine
build.gradle.kts
Copy code
repositories {
    myExtension()
}

fun RepositoryHandler.myExtension(): MavenArtifactRepository =
maven {
    // Some code
}
But if i define the extension in another file, i can’t seem to get it to work as here:
build.gradle.kts
Copy code
import extensions.myExtension

repositories {
    myExtension()
}
buildSrc/src/main/kotlin/extensions/MyExtensionFile.kt
Copy code
package extensions
// some imports

fun RepositoryHandler.myExtension(): MavenArtifactRepository =
maven {
    // Some code
}
Specifying the extension in another file yields -
Unresolved reference: extensions
.Is it possible to have such a setup with reusable extensions accessible from all the gradle files?
v
You miss
package extensions
in your
MyExtensionFile.kt
, don't you?
s
i forgot to add it to the snippet above, but it is specified in the actual code
v
Works fine here
🤔 1
Just tried it in a play project
s
was it able to build? it imports fine for me but fails to build 🤔
v
I have added a
println("FOO")
to the extension and executed the
help
task
FOO
was printed, build successful
Can you provide a full reproducer, maybe it is just a tiny bit you are having wrong
s
hmmm..interesting 🤔 let me try and see if i can reproduce in an empty project, might be a small mistake indeed
created a new Android project and was able to reproduce this, it complains about the same thing
Unresolved reference: extensions
here’s a link to the reproducible code https://gitlab.com/samuelprince411/dsltest
v
Does it complain about the one in
buildSrc/build.gradle.kts
? Because that is expected.
buildSrc/build.gradle.kts
defines how to build the stuff in
buildSrc/src
so you cannot use things from
buildSrc/src
in the definition of how to build it.
🙏 1
hen and egg, you know
s
ahhh! that makes total sense now! usages outside the
build.gradle.kts
work perfectly fine, thank you for the help 😃
v
yw