When applying Kotlinx-Serialization and KMP-Native...
# multiplatform
k
When applying Kotlinx-Serialization and KMP-NativeCoroutines plugins in the same KMP module, getting this error during configuration:
Copy code
> Cannot add task 'commonizeNativeDistribution' as a task with that name already exists.
Any ideas?
r
Interesting, KMP-NativeCoroutines doesn't use the cinterop commonizer. Do you have any more details about your setup? Any other plugins / dependencies that might affect this?
k
Kotlin 1.7.20 Gradle 7.5 I got a custom build logic Gradle plugin though which I add Gradle plugins to the classpath:
Copy code
// build-logic/build.gradle.kts
dependencies {
  implementation(libs.agp)
  implementation(libs.kotlin.gradle.plugin)
  implementation(libs.kotlin.multiplatform.plugin)
  implementation(libs.kotlin.serialization.gradle.plugin)
  implementation(libs.kotlin.multiplatform.swiftpackage.plugin)
  implementation(libs.kotlin.coroutines.native.gradle.plugin)
  implementation(libs.sqldelight.gradle.plugin)
}
In this particular module that fails I only have the build logic plugin applied in addition to two external ones (some dependencies but it still fails if I remove those dependencies):
Copy code
plugins {
  id("build.logic.kotlin.multiplatform")
  alias(libs.plugins.kotlin.coroutines.native)
  alias(libs.plugins.kotlin.serialization)
}
I saw exact same problem when applying SqlDelight Gradle plugin in KMP module in the past and I solved it by adding the plugin to the classpath. Doing so doesn’t seem to fix it here for some reason.
Not sure how to read this error message either, haven’t found much on github nor youtrack:
Copy code
Cannot add task 'commonizeNativeDistribution' as a task with that name already exists.
Just applying the native coroutines plugin alone fails as well:
Copy code
plugins {
  id("build.logic.kotlin.multiplatform")
  alias(libs.plugins.kotlin.coroutines.native)
}
r
Alright I think the issue is in how the dependencies are added in custom build logic plugin. Normally you would add dependencies to your source sets, adding them at the top level would require the source set name: https://kotlinlang.org/docs/gradle-configure-project.html#set-dependencies-at-top-level However I am also wondering why you are adding those plugins as dependencies instead of applying them?
k
My understanding is that it adds the plugins to the classpath which allows the project to resolve plugins
Copy code
kotlin-coroutines-native-gradle-plugin = { module = "com.rickclephas.kmp.nativecoroutines:com.rickclephas.kmp.nativecoroutines.gradle.plugin", version.ref = "kotlin-coroutines-native" }
It won’t resolve the plugin without adding it to the classpath:
Copy code
UnknownPluginException: Plugin [id: 'com.rickclephas.kmp.nativecoroutines'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Included Builds (None of the included builds contain this plugin)
- Plugin Repositories (plugin dependency must include a version number for this source)
That is if I use plugin alias or id without version:
Copy code
alias(libs.plugins.kotlin.coroutines.native) // fails to resolve
id("com.rickclephas.kmp.nativecoroutines") // fails to resolve
id("com.rickclephas.kmp.nativecoroutines") version "0.13.2" // is able to resolve but don't want to apply plugin like this
r
Hmm haven’t used such an approach, so I am not sure how that should work. It looks like that Slack plugin is used for Android apps, so it might not work for multiplatform projects as is.
k
It has been working so far with Wire, SqlDelight, Serialization, etc
Even I don’t take this approach: remove the plugin from
dependencies
block in
build-logic/build.gradle.kts
, and apply plugin like so, it’s still fails with the same error 🤔
Copy code
id("com.rickclephas.kmp.nativecoroutines") version "0.13.2"
Which tells me it might be unrelated to adding
implementation(libs.kotlin.coroutines.native.gradle.plugin)
to the build logic plugin
r
Oke. Which plugins are these?:
Copy code
implementation(libs.kotlin.gradle.plugin)
implementation(libs.kotlin.multiplatform.plugin)
k
Copy code
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-multiplatform-plugin = { module = "org.jetbrains.kotlin.multiplatform:org.jetbrains.kotlin.multiplatform.gradle.plugin", version.ref = "kotlin" }
r
Well I am out of ideas 😅 maybe someone who's more familiar with the custom plugin approach can provide a clue.
k
Thanks for helping to look into this nonetheless
To close the loop, fixed this by properly adding plugins to the classpath instead of relying on custom plugin’s dependencies 👌 Root `build.gradle.kts`:
Copy code
plugins {
  alias(libs.plugins.kotlin.coroutines.native) apply false
  alias(libs.plugins.kotlin.serialization) apply false
  // ...
}

buildscript {
  repositories {
    google()
    mavenCentral()
    gradlePluginPortal()
  }

  dependencies {
    classpath(libs.kotlin.multiplatform.plugin)
    classpath(libs.kotlin.coroutines.native.gradle.plugin)
    classpath(libs.kotlin.serialization.gradle.plugin)
    // ...
  }
}
560 Views