Search code examples
gradlegradle-kotlin-dslkotlin-js

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


FAILURE: Build failed with an exception.

  • What went wrong: Could not determine the dependencies of task ':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'.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 7s

Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no]


Solution

  • When building a project using Kotlin 1.3.xx, you may encounter a Gradle error if one of your dependencies (or any transitive dependency) was built using Kotlin 1.4+:

    This resolution problem appears because Kotlin 1.4 introduces the choice between two different compiler backends for Kotlin/JS – a choice that was not available in Kotlin 1.3.xx.

    Workaround:

    In order to use the libraries, create a file called workaround_to_use_1_4_libs_in_1_3.gradle.kts in the root directory of your project, and add the following code:

        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")
        }
    }
    

    To apply the workaround, add the following snippet to your build.gradle file:

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

    i didnt find the solution I got pointed to it by some one in slack channel you track link that has the solution

    the sloution link