Stylianos Gakis
06/04/2025, 4:45 PMprivate fun Project.configureCommonDependencies(libs: LibrariesForLibs) {
dependencies {
val koinBom = libs.koin.bom
add("implementation", platform(koinBom))
add("implementation", project(":logging-public")) ...
}
}
}
Which lives in a convention plugin which we apply to all of our projects.
Now, we are switching some of these projects to be Kotlin Multiplatform projects. The problem arises that we can no longer just do add("implementation", ...
as it does not exist.
Using the kotlin DSL it instead then looks something like
kotlin {
sourceSets {
commonMain.dependencies {
implementation(...)
}
}
}
Is there some way for me to still rating this piece of code in this common place in my convention plugin, and conditionally do one or the other thing, depending on if the project in which it is being applied to is multiplatform or not?
And if yes, how would the syntax have to look for that? I am having a bit of a hard time figuring this out myself to be honestVampire
06/04/2025, 5:24 PMcommonMainImplementation
.
So you could e. g. give the configuration name to your function and supply the according one where you want it added.Stylianos Gakis
06/04/2025, 6:41 PMephemient
06/04/2025, 9:01 PMfun KotlinDependencyHandler.configureCommonDependencies() {
implementation(...)
}
which could be used like
kotlin {
sourceSets {
commonMain {
dependencies {
configureCommonDependencies()
but unfortunately it's unrelated to the built-in DependencyHandler
type :-/Andrew Grosner
06/05/2025, 1:06 AMAndrew Grosner
06/05/2025, 1:08 AMephemient
06/05/2025, 6:40 AMpluginManager.withPlugin
Vampire
06/05/2025, 7:50 AMI still wonder if there's a reasonable way for me to be able to infer which one of the two I should apply.For example
fun KotlinDependencyHandler.configureCommonDependencies(libs: LibrariesForLibs) {
configureCommonDependencies(libs, "commonMainImplementation")
}
fun DependencyHandlerScope.configureCommonDependencies(libs: LibrariesForLibs) {
configureCommonDependencies(libs, "implementation")
}
private fun Project.configureCommonDependencies(libs: LibrariesForLibs, configurationName: String) {
dependencies {
add(configurationName, project(":logging-public"))
}
}
and let the consumer call it at an appropriate place.
Or if you want it more implicit like the others suggested, react to the plugins that get applied.
But like @ephemient said, not what @Andrew Grosner said.
Requiring plugin application order for proper function is bad practice. πStylianos Gakis
06/05/2025, 9:16 AMprivate fun Project.configureCommonDependencies(libs: LibrariesForLibs) {
pluginManager.withPlugin(libs.plugins.kotlinMultiplatform.get().pluginId) {
project.extensions.configure<KotlinMultiplatformExtension> {
sourceSets.configureEach {
dependencies {
configureCommonDependencies(project, libs)
}
}
}
}
pluginManager.withPlugin(libs.plugins.kotlinJvm.get().pluginId) {
dependencies {
configureCommonDependencies(project, libs)
}
}
pluginManager.withPlugin(libs.plugins.kotlin.get().pluginId) {
dependencies {
configureCommonDependencies(project, libs)
}
}
}
@Suppress("UnusedReceiverParameter")
private fun KotlinDependencyHandler.configureCommonDependencies(project: Project, libs: LibrariesForLibs) {
project.configureCommonDependencies(libs, "commonMainImplementation")
}
@Suppress("UnusedReceiverParameter")
private fun DependencyHandlerScope.configureCommonDependencies(project: Project, libs: LibrariesForLibs) {
project.configureCommonDependencies(libs, "implementation")
}
private fun Project.configureCommonDependencies(libs: LibrariesForLibs, configurationName: String) {
dependencies {
val koinBom = libs.koin.bom
add(configurationName, platform(koinBom))
add(configurationName, project(":logging-public")) ...
}
}
And itβ¦ looks to be working as far as I can tell πVampire
06/05/2025, 9:46 AMconfigurationName
Stylianos Gakis
06/05/2025, 9:51 AMconfigurationName
as you suggested of course π