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

标签 gradle gradle-kotlin-dsl kotlin-js

失败:生成失败,发生异常。

  • 出了什么问题:
    无法确定任务':packageJson'的依赖关系。

  • 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'.


  • 尝试:
    使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行,以获取更多日志输出。与--scan一起运行以获取完整的见解。
  • https://help.gradle.org上获得更多帮助

  • 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")
    
    我没有在闲置 channel 中找到某个人指出的解决方案
    您跟踪具有解决方案的链接
    the sloution link

    关于gradle - 在kotlin js项目中包含kotlinx-datetime库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63415127/

    相关文章:

    android - 仪器测试 : define a test as mandatory for the following

    gradle - 如何从最终工件(jar)中排除依赖项,但需要在 gradle 中编译和运行单元测试时使用

    gradle - 使用Gradle动态设置分类器

    gradle - Gradle Kotlin DSL中的closureOf和delegateClosureOf有什么区别

    java - Gradle Kotlin 应用程序的启动脚本找不到主类

    android-studio - 如何将 JS 目标添加到 KMM 项目?

    java - 用于多项目设置的 Spotbugs 配置

    kotlin - 构建.gradle.kts :10:28: Unresolved reference: settings

    javascript - 如何从常规 Javascript 调用 Kotlin/JS 函数?

    docker - 在Docker中构建Kotlin JS项目时,任务 ':rootPackageJson'的执行失败