在 kotlin js 项目中包含 kotlinx-datetime 库的问题

issue in including kotlinx-data-time libray in kotlinjs project

失败:构建失败,出现异常。

Could not resolve all dependencies for configuration ':npm'. Could not resolve org.jetbrains.kotlinx:kotlinx-datetime:0.1.0. Required by: project : > Cannot choose between the following variants of org.jetbrains.kotlinx:kotlinx-datetime:0.1.0: - jsIr-runtime - jsLegacy-runtime All of them match the consumer attributes: - Variant 'jsIr-runtime' capability org.jetbrains.kotlinx:kotlinx-datetime:0.1.0: - Unmatched attributes: - Found org.gradle.status 'release' but wasn't required. - Found org.jetbrains.kotlin.js.compiler 'ir' but wasn't required. - Compatible attributes: - Required org.gradle.usage 'kotlin-runtime' and found compatible value 'kotlin-runtime'. - Required org.jetbrains.kotlin.platform.type 'js' and found compatible value 'js'. - Variant 'jsLegacy-runtime' capability org.jetbrains.kotlinx:kotlinx-datetime:0.1.0: - Unmatched attributes: - Found org.gradle.status 'release' but wasn't required. - Found org.jetbrains.kotlin.js.compiler 'legacy' but wasn't required. - Compatible attributes: - Required org.gradle.usage 'kotlin-runtime' and found compatible value 'kotlin-runtime'. - Required org.jetbrains.kotlin.platform.type 'js' and found compatible value 'js'.

7 秒内构建失败

将构建扫描发布到 scans.gradle.com 需要接受在 https://gradle.com/terms-of-service 中定义的 Gradle 服务条款。你接受这些条款吗? [是,不是]

使用 Kotlin 1.3.xx 构建项目时,如果您的依赖项之一(或任何传递性依赖项)是使用 Kotlin 1.4+ 构建的,您可能会遇到 Gradle 错误:

出现此解析问题是因为 Kotlin 1.4 为 Kotlin/JS 引入了两个不同的编译器后端之间的选择 – Kotlin 1.3.xx.

中没有的选择。

解决方法:

为了使用这些库,在项目的根目录中创建一个名为 workaround_to_use_1_4_libs_in_1_3.gradle.kts 的文件,并添加以下代码:

    val pluginAction: Plugin<*>.() -> Unit = {
    val pluginVersion = try {
        this.javaClass.getMethod("getKotlinPluginVersion").invoke(this) as String
    } catch(e: Exception) { null }
    if (pluginVersion != null && pluginVersion.startsWith("1.3")) {
        val jsCompilerAttr = Attribute.of("org.jetbrains.kotlin.js.compiler", String::class.java)
        project.dependencies.attributesSchema.attribute(jsCompilerAttr) {
            this.disambiguationRules.add(KotlinJsCompilerDisambiguationRule::class.java)
        }
    }
}
project.plugins.withId("org.jetbrains.kotlin.multiplatform", pluginAction)
project.plugins.withId("org.jetbrains.kotlin.js", pluginAction)
// project.plugins.withId("kotlin2js", pluginAction) // maybe even `kotlin2js`
private class KotlinJsCompilerDisambiguationRule : AttributeDisambiguationRule<String> {
    override fun execute(details: MultipleCandidatesDetails<String>) {
        details.closestMatch("legacy")
    }
}

要应用解决方法,请将以下代码段添加到您的 build.gradle 文件中:

apply(from = "workaround_to_use_1_4_libs_in_1_3.gradle.kts")

我没有找到 slack 频道中有人指出的解决方案 您跟踪 link 有解决方案

the sloution link