dependsOn function call is not available in gradle 8.1.0

Hello,

I am writing my first gradle plugin and currently working on dependsOn() function call availability issue in gradle 8.0.

The full code is:

class CustomPlugin: Plugin<Project> {
    override fun apply(project: Project) {
        project.tasks.register<HelloTask>("hello"){
            dependsOn("build")
        }
    }
}

I use this post and gradle-recipes for reference. Not everything is up to date, but I do not have a better sources to speed up a learning curve for this tech. Migration from 7 to 8 brought many unclear dependencies failures and here is a one of them.

Could you please clarify dependsOn usage in this context? Is it still relevant here?

Thanks!

Update:

Code sample to reproduce the issue:. Take a look on HelloTask and MyPlugin. I used an existing gradle-recipe repo to avoid dealing with dependencies issues and writing my first plugin in existing sample.

Best guess is, that HelloTask is not a Task subclass

No, it is a Task subclass:

abstract class HelloTask: DefaultTask() {

    @TaskAction
    fun taskAction() {
        println("Hello \"${project.parent?.name}\" from task!")
    }
}

Then you probably need to provide an MCVE, as it works just fine here like that.

Please checkout the first post. I updated original post with reproducible sample.

The problem - besides that you do not use the function you showed in your post which just looks nicer - you use the wrong plugin in your buildSrc build script. You should not use the Kotlin/JVM plugin, especially not with a version, as it might then differ from the built-in Kotlin runtime.

Instead of the Kotlin/JVM plugin you should apply the kotlin-dsl plugin. One effect is, that you use the appropriate Kotlin version. The other effect is, that the sam-with-receiver Kotlin compiler plugin is applied.

Without it, the task to be configured within register(...) { ... } is available as it, so you need to do it.dependsOn(...). With it, it is available as this and you can just do dependsOn(...).

Thank you for valuable feedback. Could you please to be more detailed? I am not sure I got you right regarding right plugin.

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.7.0"
}

repositories {
    google()
    jcenter()
}

tasks.withType<KotlinCompile>().configureEach {
    kotlinOptions.apiVersion = "1.3"
}

dependencies {
    implementation("com.android.tools.build:gradle-api:8.1.0-rc01")
    implementation(kotlin("stdlib"))
    gradleApi()
}

Above is the existing build.gradle.kts from buildSrc. Here is what I found when search for kotlin-dsl plugin. After applying it build.gradle.kts will look like one below, but syncing gives me an error:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
//    kotlin("jvm") version "1.7.0"
    id("org.gradle.kotlin.kotlin-dsl") version "4.0.16"
}

repositories {
    google()
    jcenter()
}

tasks.withType<KotlinCompile>().configureEach {
    kotlinOptions.apiVersion = "1.3"
}

dependencies {
    implementation("com.android.tools.build:gradle-api:8.1.0-rc01")
    implementation(kotlin("stdlib"))
    gradleApi()
}
Unable to load class 'org.gradle.kotlin.dsl.assignment.internal.KotlinDslAssignment'.

This is an unexpected error. Please file a bug containing the idea.log file.

Again, do not use any version or it might not match the one required for that Gradle version.
Just use

plugins {
    `kotlin-dsl`
}

as I mentioned.

The issue is what you have shared is not covered by publication I used for reference, neither in an official documentation or gradle publications regarding breaking changes in the v8.0 release. The only mention about this format of gradle plugin I have found here, but it suggest to use java-gradle-plugin - there is no any reference to kotlin-dsl.

Thank you for your help.

kotlin-dsl is documented as the plugin to use for custom build logic in Kotlin.
And it is not new to Gradle 8, it is there since the Kotlin DSL exists which is quite some years.
There was no breaking change in Gradle 8 regarding that.

Also the publication you referred to did not mention applying the Kotlin/JVM plugin from a cursory look.
It probably just went without a build script, or at least I do not see any mentioned, in which case the special buildSrc build does automatically have the kotlin-dsl plugin applied as far as I remember. I’m not sure though, as I prefer to have it exlicit and actually prefer an included build over buildSrc anyway.

And that gradle-recipes repo you linked to is just a bad reference as it uses the Kotlin/JVM plugin there which is highly discouraged and bad practice. But it also then adapts to that and uses it instead of this.

Could you please share sources besides official gradle documentaion which are good for reference from your perspective?

Currently my interests to gradle is based on Android development and Google’s code source and publications are important. If they are a bit (or a lot?) misleading, it would be good to know a better alternative.

I don’t do any Android stuff, so cannot recommend anything Android related, I’m sorry.

Ok, thank you for your help with this issue.

1 Like