when I use the `kotlin`helper method in my gradle ...
# gradle
c
when I use the `kotlin`helper method in my gradle file i can not add more options to the dependency. this is valid syntax:
Copy code
testImplementation("org.jetbrains.kotlin:kotlin-test") {
        isTransitive = false
    }
but this is not:
Copy code
testImplementation(kotlin("test")) {
    isTransitive = false
}
I think it is related to the kotlin helper returning Any instead of String:
Copy code
fun DependencyHandler.kotlin(module: String, version: String? = null): Any =
    "org.jetbrains.kotlin:kotlin-$module${version?.let { ":$version" } ?: ""}"
apart from that even if I set isTransitive to false the transitive dependencies are still included.
e
Copy code
testImplementation(kotlin("test")) {
    (this as ModuleDependency).isTransitive = false
}
but why?
c
does not work for me:
also verified that it is because of the
Any
, this also does not work:
Copy code
val dependencyNotation = "org.jetbrains.kotlin:kotlin-test" as Any
testImplementation(dependencyNotation) {
    isTransitive = false
}
e
is this kotlin-multiplatform specific? because that should work in ordinary gradle/jvm projects
c
no. it happens when I create a new kotlin project in idea (that has the test dependency per default) and then try to add isTransitive = false.
s
I get the same behaviour. There just isn't an overload that takes an
Any
and a configuration block. The
kotlin(...)
helper function actually does return a string, so this should work:
Copy code
testImplementation("${kotlin("test")}") {
    isTransitive = true
}
Alternatively you could use
apply
, since adding a dependency also returns it:
Copy code
implementation(kotlin("test"))?.apply {
    (this as ExternalModuleDependency).isTransitive = true
}
I think the right solution would be for the
kotlin(...)
function to actually return a string, or even an actual instance of
ExternalModuleDependency
. Maybe worth a YouTrack ticket?
Another interesting effect is that even when you get the syntax working it does not omit transitive dependencies, at least not from the runtime classpath.
e
oh I see, the place where we do something similar we use
implementation(...).apply { ... }
I'm still puzzled why you want to do this with
kotlin-test
though. it doesn't have any dependencies other than
kotlin-stdlib
which you need anyway
also this is 100% Gradle, it's not part of Kotlin or KGP
c
I'm still puzzled why you want to do this with
kotlin-test
though. it doesn't have any dependencies other than
kotlin-stdlib
which you need anyway
it has a runtime dependency on junit 5 that I would like to get rid of.
e
that should only be if you use
kotlin-test-junit5
, although KGP automatically tries to upgrade it if you don't specify
set
kotlin.test.infer.jvm.variant=false
to disable that, but note that the assertions and annotations in that package won't work anymore (unless you specify a
-junit
,
-junit5
, or
-testng
dependency yourself)
c
What is kgp? Why does it add dependencies that cannot be turned off with normal gradle means?
Oh the kotlin gradle plugin. Imo it really should not add dependencies and if it does it should respect the istransitive setting.
e
KGP also auto-adds the stdlib dependency, and the auto-adding of the variant of the test dependency is similar to how Gradle itself auto-adds the test framework dependency
it doesn't obey
isTransitive
that you configure there because it's a adding a dependency to the configuration
c
does gradle really add runtime dependencies on jupiter? the gradle testing examples declare the junit dependency manually: https://docs.gradle.org/current/userguide/java_testing.html
e
it is planned to change (possibly in Gradle 9) but for now it's still active for JUnit and JUnit 5
c
what the kotlin plugin does is it sees that junit platform is used and then it adds a dependency on junit jupiter. which are two separate things. there are many other testing libraries that use junit platform, for example my test framework failgood.