Search code examples
kotlingradlejvm

How to Create a Kotlin Project Using kotlinx-datetime and Share It Across Different JVMs


I'm working on a project where I want to use the kotlinx-datetime library (specifically, org.jetbrains.kotlinx:kotlinx-datetime:0.4.1). However, I'm facing a challenge when trying to share this project with another one that uses a different JVM. Both projects need to include the same version of kotlinx-datetime, but they have different JVM requirements.

I'm encountering the following error when trying to share my project:

Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
   > 2 files found with path 'META-INF/versions/9/previous-compilation-data.bin' from inputs:
      - C:\...\kotlinx-datetime-jvm\0.4.1\...\kotlinx-datetime-jvm-0.4.1.jar
      - C:\...\org.jetbrains.kotlinx\atomicfu-jvm\0.22.0\...\atomicfu-jvm-0.22.0.jar

Here's what I've tried and what I was expecting:

I tried adding api("org.jetbrains.kotlinx:kotlinx-datetime:0.4.1") to my subproject and defining it there. Then, I implemented this in my Android project using implementation("...:api_client:1.1-SNAPSHOT").

Now, I have two main questions:

  1. How can I create a Kotlin project that uses implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.1") and set it up correctly?

  2. How can I then share this project with another one that also uses implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.1") but runs on a different JVM? What are the best practices for handling this scenario?

I'd appreciate any guidance or tips on how to structure my projects and dependencies to achieve this, especially considering the error message I've encountered. Thank you!


Solution

  • As this error is just about meta information, you may just exclude the relevant files:

    android {
        packaging {
            resources.excludes.add("META-INF/versions/**")
        }
    }
    

    But, in this case, just upgrade your kotlinx-datetime to 0.5.0. Usually, meta data conflicts happen because those parts shouldn't be part of the artifact in the first place. Once one of the libraries is fixed, an upgrade helps.