Encountered "Module was compiled with an incompatible version of Kotlin"? You are not alone!
\n\nImagine: you are writing code in Kotlin, everything works like clockwork, but suddenly — bam! — the studio (or console) displays a long red message with a threatening text about "incompatible version of Kotlin" and some versions 1.5.1 and 1.1.16. A familiar picture? If you have ever worked with Kotlin in Android Studio, IntelliJ IDEA, or simply built a project via Gradle, this error has surely made you scratch your head.
\n\nThe good news: it is fixed quickly and without surgical intervention. The bad news: if you don't figure it out, it will keep coming back again and again. Today we will break this error down to the bone, understand why it occurs, and forget about it forever. Let's go!
\n\nWhat does this message actually mean?
\n\nLet's translate from "computer" to human. Kotlin, like any modern language, is constantly evolving. Each new version of the compiler adds features, fixes bugs, and... changes the format of internal metadata (binary files that describe your classes and functions).
\n\nWhen you include a third-party library (or a module of your project), the compiler checks: "Was this library compiled with the same version of Kotlin?" And if the metadata version of the library (e.g., 1.5.1) does not match the version expected by your current compiler (e.g., 1.1.16), it rightfully complains. It's like trying to insert an SD card from an old camera into a new laptop — it physically doesn't fit.
\n\nIn our case, the error says: "The library (module) was built with metadata version 1.5.1, and I (the compiler) expect version 1.1.16". Usually this means that the Kotlin plugin version in your project is outdated or, conversely, the library is too new.
\n\nWhy does this happen? 3 main reasons
\n\n1. Version mismatch in Gradle (most common)
\n\nHave you ever updated one dependency, forgetting to update the Kotlin plugin itself? Or vice versa — updated the plugin, but left the old libraries? A typical situation for Android projects with many modules.
\n\n// build.gradle (Project-level)\nplugins {\n id 'org.jetbrains.kotlin.android' version '1.5.1' apply false // Plugin is new\n}\n\n// build.gradle (Module-level)\ndependencies {\n implementation 'com.some.library:old-lib:1.0.0' // This lib was built with Kotlin 1.1.16\n}\n\nThe compiler sees: plugin — 1.5.1, library — old. And it screams: "Incompatibility!"
\n\n2. Conflict in multi-module projects
\n\nIf you have a project with several modules (e.g., app, core, feature), and each has its own Kotlin version — expect trouble. One module is built with Kotlin 1.5.1, another with 1.1.16. When you try to link them, that same error pops up.
3. "Manual" import of JAR/AAR files
\n\nDownloaded a library from the internet and put it in the libs folder? Great, but if this library was built with a different version of Kotlin than your project — hello, error.
What does it look like in code? An example for understanding
\n\nLet's simulate a situation. You have a project on Kotlin 1.1.16 (yes, old, but it works for the example). You include a library that uses the newest features of Kotlin 1.5.1.
\n\n// Your code (Kotlin 1.1.16)\nfun main() {\n val result = SomeLibrary().doSomething() // Error!\n println(result)\n}\n\nAnd inside the library (version 1.5.1), say, inline fun interface or sealed interface is used, which simply do not exist in the old version. The compiler doesn't understand what kind of beast this is and throws a metadata incompatibility error.
Step-by-step solution: from simple to complex
\n\nStep 1. Check the Kotlin plugin version
\n\nOpen the root build.gradle (or build.gradle.kts) of your project. Find the line with the Kotlin version:
//