In Gradle KotlinScript DSL, how to import generated class accessors like `implementation`?

72 views Asked by At

Assuming that a Gradle kt file under <root>/buildSrc requires a class accessor:

/**
 * Adds a dependency to the 'implementation' configuration.
 *
 * @param dependencyNotation notation for the dependency to be added.
 * @return The dependency.
 *
 * @see [DependencyHandler.add]
 */
fun DependencyHandler.`implementation`(dependencyNotation: Any): Dependency? =
    add("implementation", dependencyNotation)

Unfortunately, the entire file that defines this accessor is generated, so if I copy the function directly into my kt file it will be imported like this:

import gradle.kotlin.dsl.accessors._46b0ca6400a65b9754358ead47fee5d2.implementation

The _46b0ca6400a65b9754358ead47fee5d2 will change every time the project is recompiled, so it is useless.

How could I import it reliably when the project is compiled anytime, anywhere?

(BTW the following solution was generated by an LLM, but it is equally useless and is clearly a result of hallucination):

import org.gradle.api.JavaVersion
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.get
0

There are 0 answers