Could not determine the dependencies of task '...' - Cannot convert kotlin.Unit to a task

I’ve migrated all my groovy stuff to kotlin, the compiler is happy so far, but at runtime some of my tasks in different subprojects are now failing with this message:

E.g.:

* What went wrong:
Could not determine the dependencies of task ':subproject:assemble'.
> Cannot convert kotlin.Unit to a task.
  The following types/formats are supported:
    - A String or CharSequence task name or path
    - A Task instance
    - A TaskReference instance
    - A Buildable instance
    - A TaskDependency instance
    - A Provider that represents a task output
    - A Provider instance that returns any of these types
    - A Closure instance that returns any of these types
    - A Callable instance that returns any of these types
    - An Iterable, Collection, Map or array instance that contains any of these types

“–stacktrace” is not helpful either - is there some way to find the explicit location where gradle does have a kotlin.Unit instead of a task now - the project is huge and without a hint it’s somewhat difficult to fix it?

Hard to say from the information you gave.
Somewhere you call a method that historically accepts Object (probably dependsOn in your case), but now with something that returns Unit which cannot be converted to a task.

Sorry, I did not had anything more, because there was no hint where it is failing, but with try/error I found this:

I had the following code:

val cycloneDxBom =
    tasks.cyclonedxBom {
...
   }

the task itself is from the cyclonedx gradle plugin and used that in a builtBy:

artifacts.add("cycloneDxReport", layout.buildDirectory.file("reports/cyclonedx-backend-$version.xml")) {
    builtBy(cycloneDxBom)
}

Compiler was happy, but at runtime I had that error message from above.

I’ve changed it to:

tasks {
    cyclonedxBom {
    ...
   }
}



artifacts.add("cycloneDxReport", layout.buildDirectory.file("reports/cyclonedx-backend-$version.xml")) {
    builtBy(tasks.named("cyclonedxBom"))
}

Now it is building again. Seems I can’t assign that task to a val and use it - obviously as it is failing - although I am not sure why not and why the compiler is happy about that usage.

Yes you can, if you would have, but you didn’t :slight_smile:
You assigned the result of calling tasks.cyconledxBom { ... } which is syntactic sugar for tasks.cyconledxBom.configure { ... } which is a void method, so in Kotlin results in Unit, not the task or task provider.

val cycloneDxBom = tasks.cyclonedxBom
cyclonedxBom {
    ...
}

would have worked.
Or also

val cycloneDxBom by tasks.existing(TypeOfTheTask::class) {
    ...
}

Or also

tasks.cyclonedxBom {
    ...
}
...
builtBy(tasks.cycloneDxBom)

1 Like

grummel - now I need some sugar too, that is such a bummer, a little bit of embarrassing :wink: - you’re right of cause argl - thanks for that feedback.

1 Like