Can I set a simple variable (a `val` in Kotlin DSL...
# community-support
s
Can I set a simple variable (a
val
in Kotlin DSL) inside a Gradle convention plugin / precompiled script plugin, and access it from the project that applies the plugin?
m
If your consuming script is also in Kotlin yes.
You can use top level properties there.
But you probably won't be able to in Groovy
s
Not being able to use Groovy is fine. But when I simple tried my approach, Gradle cannot resolve the variable. Do I need to prefix it with something?
m
Mmmm I'm almost sure I've done that before. You may need to add the import manually?
(if there's an IDE issue?)
I also don't use the
kotlin-dsl
plugin, just the regular
embedded-kotlin
one but I don't think it should change anything
b
I don‘t think it‘s possible because the code inside a pre-compiled script plugin is wrapped into an apply(Project) method of a generated plugin class. Usually your plugin would register an extension and that extension could expose a val or a provider of the value (to make it more idiomatic)
👀 1
👍 2
s
Usually your plugin would register an extension and that extension could expose a val or a provider of the value (to make it more idiomatic)
True, but that's something I wanted to avoid to just quickly extract some common property that I set in all of my project to the convention plugin, for common use.
m
the code inside a pre-compiled script plugin is wrapped into an apply(Project) method of a generated plugin class
Oooh that makes sense.
(also another reason to not use
kotlin-dsl
)
b
Another way of centralizing a static value is putting it into the top level gradle.properties file. This way you can access it from every project with
project.properties["name-of-the-prop"]
s
Unfortunately, the value is not fully static, but determined based on environment variables. So it's static per run, but might change between runs.
g
Beware of potential caching issues that might create. Depending on where that plugin and property are used, it can prevent cache from being re-used when it changes.
p
Depending on the value of the variable, you could put it in a kotlin file that’s part of the precompiled sourceSet. You would still need an import.
v
(also another reason to not use kotlin-dsl )
@Martin another? I'm not aware of any reason not to use
kotlin-dsl
. But actually all you answers here do not really match, as OP asked about precompiled script plugin and you said yes and you used it like that but also said you don't use
kotlin-dsl
but only
embedded-kotlin
, which means that you cannot even use Kotlin DSL precompiled script plugins. And even if you never use precompiled script plugins - which basically are just syntactic sugar over a normal binary plugin - it still makes sense to use
kotlin-dsl
, as it not only enables Kotlin DSL precompiled script plugins, but also brings some other benefits like the
kotlinDsl()
dependency, and various compiler plugins with their configuration like the
sam-with-receiver
plugin for the
Action
methods and the assignment to
Property
`val`s plugin, ...
To answer the original question, to do this the proper way you should register an extension as was mentioned. To do it the hacky way, you would set an
ext
/
extra
property. Those could be accessed directly from Groovy DSL or via
by extra
from Kotlin DSL. Or you have some Kotlin
object
outside the precompiled script plugin where you set the value and can then retrieve it from or similar.
👍 1
m
@Vampire I try to stay clear of
kotlin-dsl
because it fragments the ecosystem for little value IMO. Having to remember all the differences vs regular Kotlin is a lot of cognitive load. I would also argue that
kotlinDsl()
adds unecessary dependencies. All of that is personal preference so I won’t be dying on this hill but IIRC, there are also performance issues with them like https://github.com/android/nowinandroid/issues/39. Unless those were fixed recently?
I guess the issue is precompiled scripts plugins, not
kotlin-dsl
?
But even then, it’s a lot to process
v
I think you still mix things up. If you are arguing against Kotlin DSL precompiled script plugins for any reason, that might be fine. But as I said, Kotiln DSL precompiled script plugins are only one aspect of what
koltin-dsl
adds.
kotlinDsl()
is the Gradle Kotlin DSL like many extension functions and so on. And also the added Kotlin compiler plugins and so on. Not using
kotlin-dsl
also fragements the ecosystem, as the Gradle code in plugins looks much different from the Gradle code in build scripts. 🙂
m
Yes, I’m basically ranting about the whole confusing situation.
the Gradle code in plugins looks much different from the Gradle code in build scripts.
There’s a lot more Kotlin code written for non Gradle scripts usage so I’d rather stick with that as much as possible
v
Well, of course always depends on what ecosystem you are look at exactly 🙂
You could also look at the JVM ecosystem and thus argue just for that reason not to use Kotlin at all, but Java, as using Kotlin splits the ecosystem 😄
m
Yup
Anyways, happy that OP found a solution! And sorry everyone for the rant. Looking forward to declarative Gradle 🚀
👌 1