https://kotlinlang.org logo
#gradle
Title
# gradle
j

Ji Sungbin

08/19/2022, 1:41 PM
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

Grégory Lureau

08/19/2022, 1:44 PM
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

Ji Sungbin

08/19/2022, 1:45 PM
Hi Grégory Lureau, thanks for the reply. Did you mean this?
m

mkrussel

08/19/2022, 1:49 PM
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

Ji Sungbin

08/19/2022, 1:53 PM
wow mkrussel you are awesome. Thanks everyone!
m

mkrussel

08/19/2022, 1:55 PM
You might want to consider enabling the Lambda Kotlin inlay hints . It can clear up a lot of these types of confusion.
j

Ji Sungbin

08/19/2022, 1:56 PM
Thanks for the great tip till the end!
v

Vampire

08/19/2022, 2:00 PM
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

Ji Sungbin

08/19/2022, 2:28 PM
Hi Björn Kautler, thanks for the explanation!
15 Views