Centralized dependencies vs dependencies per sourceSet?

With Kotlin Multiplattform template, I found that dependencies have been set per source set, like the following:

sourceSets {
    main {
        dependencies {
            implementation(aaa)
        }
    }

    test{
        dependencies {
            implementation(bbb)
        }
    }
}

Is that actually the same as declaring dependencies centrally in the gradle file, like

dependencies {
    implementation(aaa)
    testImplementation(bbb)
}

or how do configurations work in dependencies per sourceSet?
Would I have to use testImplementation configuration in the test sourceSet as well for both to be equal?

In Groovy DSL your two versions are probably the same.

In Kotlin DSL the problem is, that type-safe accessors like implementation(...) and testImplementation(...) are only generated for configurations that are added by plugins applied in plugins { ... } without further configuration.
But the KMP plugin only creates these configurations according to additional configuration.
But within the DSL the KMP plugin provides, they can have the implementation(...) method statically provided in the nested dependencies { ... } block, so it would also work in Kotlin DSL properly.
Much like when using the built-in JVM test suites plugin.

1 Like