1

I have an android library(aar) that was developed with Kotlin(version: 1.7.10) I want to use my library inside another project with Kotlin version 1.4.32 or any Kotlin version except 1.7.10, but I get this error during build:

Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.4.2.

Full Error:

/Users/user/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.7.10/bac80c520d0a9e3f3673bc2658c6ed02ef45a76a/kotlin-stdlib-common-1.7.10.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.4.2.

So my question is, what should I do to be able to use my library inside projects with any different Kotlin version? Or How can I build a Kotlin library that works for every Kotlin version.

Should I have any Gradle configuration to do that?

Additional info

Device: Mac book m1 pro, and Asus with intel chipset. 
OS: OSX, and Windows
IDE: Android Studio Chipmunk 2021.2.1 patch 2
AGP: 7.3.3

Cleaning the project or Invalidate cache and restarting the project does not help.

Also changing the version of Kotlin used in the library from 1.7.10 to 1.6.21 doesn't help. It will produce the same error with 1.7.10 or other versions.

5
  • 1
    I think the only solution is to update your project to 1.7.10, or higher. There's a reason there are newer versions. Newer versions are not always backwards compatibly with older versions
    – Ivo
    Commented Aug 12, 2022 at 7:30
  • Actually I am the library maintainer and can't tell everyone that updates their projects to use my library, I want to make my library compatible with any projects regardless of its Kotlin version.
    – Ali
    Commented Aug 12, 2022 at 7:47
  • 4
    In Java, you wouldn't be able to use Java 11 to execute code compiled with Java 12+. No way around that, except to target Java 11 when compiling the code. I would expect Kotlin has similar restrictions.
    – Slaw
    Commented Aug 12, 2022 at 7:59
  • @Slaw - You are right, although documentation does appear to suggest that if newer binaries are used on an older compiler it can work but only if the newer binary has used a compatible api surface with the older compiler, obviously that is not guaranteed - kotlinlang.org/docs/…
    – Mark
    Commented Aug 12, 2022 at 9:10
  • @Mark You can do something at least similar in Java with e.g., --release 11.
    – Slaw
    Commented Aug 12, 2022 at 9:19

1 Answer 1

1

Your library needs to be built with lower or equal version than your project, so there are 2 options:

  1. Update your project to version 1.7.10 or higher
  2. Build your library with kotlin version 1.4.32 or lower

This error happens, because there are constantly added, changed or removed functionalities in the kotlin language so your library may use something that earlier versions of kotlin may not understand. That's why your project has to be built with equal or higher version than your library.


You can also set language and api version in compiler settings: in intelliJ go to Settings > Build, Execution, Deployment > Compiler > Kotlin Compiler. Here you can set the target version, you want your library to work with.

2
  • So Why some libraries with Kotlin version 1.6 can build and run in projects with Kotlin version 1.4? how does it work?
    – Ali
    Commented Aug 12, 2022 at 13:57
  • 1
    ah, ok, you can set language and api version in settings: Build, Execution, Deployment > Compiler > Kotlin Compiler. Go here and set 1.4 for your library - it should force your library to be built with compatibility with version 1.4
    – dey
    Commented Aug 12, 2022 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.