kotlingradlekmm

Kotlin multiplatform library exporting dependencies for JVM


I’m creating a multiplatform for jvm and iOS on kotlin using Gradle. For the jvm, even if I define my dependencies as implementation, they are included in the library generated .pom and with runtime scope.

By using implementation, I was expecting that these dependencies are not passed to the library consumer.

But, when I use this library on my other jvm project, Gradle is importing the library-specific version. Not the one that I set in my application dependencies.

in this case, I'm doing a downgrade. The library is using the dependency version 1.4.1, and on the application I want to use version 1.4.0.1.


Solution

  • By using implementation, I was expecting that these dependencies are not passed to the library consumer.

    If you expect the consumers to provide those transitive dependencies themselves, you should use compileOnly instead of implementation.

    The difference between api and implementation is that transitive dependencies declared with api will be visible and usable by the application by just depending on your library (seen at compile time AND runtime). With implementation, the transitive dependency will still be here at runtime but will not be visible on the compile classpath of the consuming application, so you cannot use the transitive dependency's declarations in the application's code.

    Have a look at the table here: https://docs.gradle.org/current/userguide/java_library_plugin.html

    If you stick with implementation, you can still force a version for the transitive dependency in Gradle by using strictly or by excluding it and redeclaring it yourself. See the doc: https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html