gradlekotlinkotlin-multiplatformgradle-multi-project-build

How to add dependencies to tests of another project in a multi-platform multi-project Kotlin build


I have a multi-platform Kotlin project, which contains multiple modules (subprojects). The Building Multiplatform Projects with Gradle documentation of Kotlin clearly shows how to set up project dependencies between modules:

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                // All of the compilations that include source set 'commonMain'
                // will get this dependency resolved to a compatible target, if any:
                api project(':foo-lib')
            }
        }
    }
}

The problem I am facing now is that although this documents how to add a dependency from one module (all of its target platforms) to another module (its corresponding target platforms), it does not clarify how to add a dependency to the test sources of another module.

I would expect this to be a fairly common use case. For example, one of the reasons I want to do this is that my test sources of module A contain stub classes. Module B depends on module A, including the class definitions for which test stubs are provided in the test sources of module A. Thus, it is not unexpected that tests of module B might want to reuse those test stubs defined in the test sources of module A.

A common recommendation in multi-project builds is to move such test sources to yet another module, but this seems unnecessary to me. Solutions which in my opinion are cleaner exist for normal multi-project builds, but I can't figure out how to configure the equivalent of this in a multi-platform multi-project Kotlin build which also relies on the Kotlin Gradle DSL demonstrated in the code above.


Solution

  • To establish a dependency of this sort between single-platform projects, you would normally need to create a Gradle Configuration in the producer project, add the test compilation outputs or a test JAR into that configuration, and, in the consumer project, depend on that configuration (i.e. add a project(...) dependency with an explicit configuration or, more preferrable, add attributes to ensure that Gradle chooses the test-outputs configuration in variant-aware dependency resolution).

    However, all of this requires that tests are compiled to a form that could be reused by the consumer project's common source sets. With Kotlin Multiplatform projects, this is not yet the case. While the production common source sets, which participate in published compilations, are compiled to Kotlin metadata (*.kotlin_metadata files), test sources and other kinds of non-published code are not yet transformed to that format.

    Instead, tests are currently only compiled to the final platform-specific binaries (i.e. *.class files, *.js, Native binaries), which cannot be used for common sources analysis.

    Therefore, dependencies of this kind are not supported yet. This could change in the future. Please follow this issue in the Kotlin issue tracker: KT-35073.