Hi, How do I get arguments in TaskProvider#configu...
# gradle
j
Hi, How do I get arguments in TaskProvider#configure lambda? If you look at Google’s code, it is receiving arguments from configure, but when I actually try it, an error appears. Also, I checked if Google has overridden the TaskProvider#configure function, but I couldn’t find it.
g
Have you tried to remove the argument (jarTask) and directly use this? (I presume it's not the same method/version between both projects)
j
Hi Grégory Lureau, thanks for the reply. Did you mean this?
m
The task is the context receiver to the lambda and not an argument. So you would want something like
jarTask = this as Jar
. Or use
tasks.named<Jar>("jar") { // this is now a Jar
j
wow mkrussel you are awesome. Thanks everyone!
m
You might want to consider enabling the Lambda Kotlin inlay hints . It can clear up a lot of these types of confusion.
j
Thanks for the great tip till the end!
v
Kotlin has a
sam-with-receiver
compiler plugin. With this plugin you can specify classes / interfaces for which you can use
foo.configure { doSomething() }
instead of
foo.configure { it.doSomething() }
. In the Kotlin DSL (and also if you apply the
kotlin-dsl
plugin to a project) this plugin is configured to for example include the
Action
interface. In the Google code you linked, this compiler plugin is not active, so you have to use the
foo.configure { it.doSomething() }
version there.
j
Hi Björn Kautler, thanks for the explanation!