Execute Android Project via command line (gradle) does not work

  Kiến thức lập trình

I have a project to run e2e tests. All the test code is in the androidTest folder.
I use cucumber to create and execute custom test steps.
I can start the tests in AndroidStudio using the run button.
Running the tests in a console using the gradle command does not work.

Here you can see the run config parameters I use in Android Studio:

Main class: io.cucumber.core.cli.Main
Glue: com.testapp
Feature or folder path: .../app/src/androidTest/resources/login.feature
program arguments:  --plugin org.jetbrains.plugins.cucumber.java.run.CucumberJvm5SMFormatter
Working directory: $MODULE_WORKING_DIR$
Use classpath of module: TestApp.app.androidTest
Shorten command line: none

I found an example for a gradle task to execute a cucumber project via console, but this example is written for a build.gradle file and not for build.gradle.kts file:

sourceSets.main {

configurations {
    cucumberRuntime {
        extendsFrom testImplementation

task runTest() {
    dependsOn assemble
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
            args = ['--plugin', 'pretty', '--glue', 'com.testapp.cucumber', 'src/test/resources']

I tried to convert it to an build.gradle.kts file:

android {

    sourceSets {
        getByName("main") {

val cucumberRuntime by configurations.creating {

tasks.register<JavaExec>("runTest") {
    dependsOn("assemble", "testClasses")
    mainClass = "io.cucumber.core.cli.Main"
    classpath = cucumberRuntime + sourceSets["main"].output + sourceSets["androidTest"].output
    args = listOf(
        "--plugin", "pretty",
        "--plugin", "html:target/cucumber-report.html",
        "--glue", "com.testapp",

This does not work:

Build failed

Could not create task ‘:app:runCucumber’.
SourceSet with name ‘main’ not found.

I use following versions:
java: 17
Gradle: 8.4
Gradle Plugin: 8.3.2
cucumberVersion: 7.17.0

Has someone an Idea?