I would like to create an extension function like ...
# gradle
d
I would like to create an extension function like this in my
buildSrc
module. However I cannot access
implementation()
,
kapt()
, etc extensions functions that are declared in the package
org.gradle.kotlin.dsl
from the
gradle-kotlin-dsl-accessors
artifact. Is it possible to have access to them from my
buildSrc
module? Thanks for you help.
j
Copy code
fun DependencyHandler.implementation(dependencyNotation: Any): Dependency? =
    add("implementation", dependencyNotation)
You can't access to implemention because you are not applying the plugin in that file
d
yeah at the end I just copied pasted the functions that I needed
but it doesn’t feel very right to me. What I was wondering is if I can import the
gradle-kotlin-dsl-accessors
artefact somehow in my buildSrc module
so that I don’t need to duplicate code
n
those accessors are generated for one of the plugins which is applied to your project. you should have access to it if you apply that plugin to your buildsrc as well.
d
Hi Nelson, thanks for your answer 🙂 Which plugin should I apply? Currently I’m applying the
kotlin-dsl
plugin in my buildSrc, but still I cannot access those accessors.
j
kotlin("jvm")
, or multiplatform, android, etc
d
thanks Javier. I’ve tried to apply the jvm and android plugins but then it doesn’t even compile the
build.gradle.kts
file from the buildSrc module
do you have any clue why?
j
ah, android one is different,
id("com.android.library")
, try
kotlin("jvm")
d
hmm same problem with
jvm
, it cannot find the declarations for these accessors
j
Ah, I didn't see the screenshot, that implementación should work with only kotlin dsl
I have this in my
buildSrc/build.gradle.kts
n
are they just red in the ide, or does the build fail as well?
d
yeah the build fails as well with this error
@Javier, with that config, do you have any function in your buildSrc that makes use of some of this accessors? Like this one
n
@David Miguel if you fix the typo in the dependency you requested, does the build still fail?
builPd
->
build
d
uhh
😅
let me retry
sorry the typo was introduced in the last retry. After removing it, I get the error I was getting before, that it cannot find those extensions functions
j
@David Miguel yeah, creating the extension function I shared some time ago
d
ahh I think we are not understanding each other hehe If I declare the extension functions myself, it works fine 👍 But ideally, I would like to avoid having to declare those extension functions myself, and instead access the ones provided in the
gradle-kotlin-dsl-accessors
artefact. But that is what I don’t know how to achieve it (maybe is not even posible).
but it’s a minor thing, I’m happy declaring them myself. I was just wondering whether the other approach was posible
j
I can only get that function if I am creating a precompiled plugin and I am applying some plugin which includes it. I have to try @no solution to check if I can remove my own extensions.
I added it to
buildSrc/build.gradle.kts
and it is not working, I can't access to implementation in the extensions file
I tried to add the plugins inside
dependencies { implementation(...) }
in buildSrc dependencies but I cannot access to implementation functions in extensions too. https://plugins.gradle.org/plugin/org.gradle.kotlin.kotlin-dsl
n
I think since these are generated accessors they are not part of the API. Being able to use them doesn't mean that these methods will be available when you ship your plugin. It is best to redeclare them.
👍 1
d
thanks @Javier for trying it out 😊 @no that was one of my hypothesis, thanks for confirming! We’ll have to redeclare them for now
👍 1