I want to build a library using Kotlin 1.6 but mak...
# library-development
m
I want to build a library using Kotlin 1.6 but make sure that it’s still usable by consumers who are using Kotlin 1.3. (I know it’s deprecated, but I have some of those consumers still.) I’ve found some info in the Kotlin documentation, Stack Overflow, etc., but I’m still not certain, so I hope some of the great minds here can confirm or deny my suspicions. If I set
apiVersion = "1.3"
and force my tests to use a
1.3.n
version of
kotlin-stdlib-jdk
, that should both set and validate that my library will work for consumers using Kotlin 1.3, right? (The relevant excerpts from my
build.gradle
file are in the thread so I don’t spam the channel with a wall of build script.)
Relevant bits of my
build.gradle
file:
Copy code
plugins {
  id "org.jetbrains.kotlin.jvm" version "1.6.0"
}

ext.kotlin_version = "[1.3,)"

dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  testImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72"
}

compileKotlin{
  kotlinOptions.apiVersion = "1.3"
}
compileTestKotlin {
  kotlinOptions.apiVersion = "1.3"
}
b
I'm not certain about this, but should compileTestKotlin also have:
Copy code
kotlinOptions.languageVersion = "1.3"
m
I tried that and it failed because the compiler complained about 1.3 not being supported. I moved on pretty quickly, but I could try it again and see if I can learn anything else from it. Does the overall approach seem correct?
b
You're right, it's saying 1.4 is the oldest supported language version. You have to compile the tests with the 1.3 compiler for them to know that Kotlin 1.3 can use your library. Just using the 1.3 stdlib wouldn't be enough
And in case you haven't seen it already, there aren't even guarantees if you wanted to use 1.4, so you'd probably be in for a lot of headaches: https://kotlinlang.org/docs/kotlin-evolution.html#evolving-the-binary-format
Preferably (but we can't guarantee it), the binary format is mostly forwards compatible with the next feature release, but not later ones (in the cases when new features are not used, e.g. 1.3 can understand most binaries from 1.4, but not 1.5).
Writing your library in 1.3 would mean 1.6 projects could use it though:
All binaries are backwards compatible, i.e. a newer compiler can read older binaries (e.g. 1.3 understands 1.0 through 1.2),
m
My understanding is that both Kotlin 1.5 and 1.6 libraries cannot be used in projects compiled with Kotlin 1.3
It's only "1" version forward
So as a library auathor, it's best to wait a bit before adopting newer versions of the language
j
According to the 1.6 release blog post:
Supporting previous API versions for a longer period Starting with Kotlin 1.6.0, you can now develop using three previous API versions instead of two (along with the current stable one). Currently, this includes API versions 1.3, 1.4, 1.5, and 1.6.
https://blog.jetbrains.com/kotlin/2021/11/kotlin-1-6-0-is-released/
m
Oooo didn't see that, thanks 👀
It's about API versions though. Compiling a project with a 1.6 lib might require reading the 1.6 metadata from the 1.6 class files, which the 1.3 compiler will most likely not be able to do
Unless apiVersion=1.3 also writes 1.3 compatible metadata 🤔 ?
apiVersion
doesn't look like it sets the metadata to a compatible format.
languageVersion
looks like it does though
m
Oh, thanks so much for the comprehensive writeup!
j
Nice writeup indeed! Certainly worth it to put into #feed
👍 1
b
Looks like you can use language version 1.3 with the 1.6.10 compiler now :) https://youtrack.jetbrains.com/issue/KT-49868