Hi everyone! I’m newbie in KMP. Started project fr...
# multiplatform
v
Hi everyone! I’m newbie in KMP. Started project from scratch. Documentation says that KMP projects are getting dependency on
kotlin-stdlib
automatically. However, for some reason, I can’t access
StringBuilder
in my
commonsMain
source set. At the same time I’ve added a couple of KMP libraries as dependencies to the
commonsMain
set explicitly and all entities from them are accessible. I feel like I’m making some stupid mistake. But I can’t find which one. Any idea what might’ve gone wrong?
m
Do you get a compiler error or do you get an error just in the IDE?
v
I don’t know. However, I imported a couple of other KMP projects and there StringBuilder is accessible just fine. My IDE is IDEA, it complains on “unresolved reference”.
For some reason the issue is gone if I add
linux64
target. But I only need JVM. Besides, the other libraries, which I use as a reference, do not have such problem.
m
Does gradle build successfully or does it show a compiler error with an unresolved reference?
k
Mine issue below this thread is probably related, because I cannot see
JsExport
neither
StringBuilder
in
commonMain
sources while it works in the jsMain and jvmMain sources
@Michael Krussel it shows a compiler error:
Copy code
e: file:///home/kubapet/Projects/dossier-profile-gradle/dossierprofile-publicapi-restapi/src/commonMain/kotlin/AwesomeClass.kt:1:2 Unresolved reference: JsExport
and as Vladislav says the other ilbraries added as commonMain dependencies are accessible from there it is only related to the stdlib
Oh ye, there is some regression in 1.9.10 -> 1.9.20; when I add this dependency explicitly, then it works
Copy code
commonMain.dependencies {
      implementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.9.10") {
        version {
          strictly("1.9.10")
        }
      }
but
Copy code
commonMain.dependencies {
      implementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.9.20") {
        version {
          strictly("1.9.20")
        }
      }
does not... maybe related to the migration of
kotlin-stdlib-common
to just
kotlin-stdlib
? @Vladislav Ermolin can you verify that it is the case even on your end?
v
Nope, doesn’t help me. Only addition of
linux64
target in addition to
jvm
solves the issue.
@Michael Krussel
./gradlew build
manages to compile the code, while IDE shows
Unresolved reference
.
k
which version are you using though?
v
2024.1
Just started out writing
jvm
target code and found out that any attempt to access
java
package inside of the
jvmMain
also gives unresolved reference. > Unresolved reference: java However, in this case project does not compile as well.
Solved by switching Java SDK to another version in Project Structure dialog. However, it made
<http://java.net|java.net>
classes accessible also in
commonMain
target.
k
I meant which version of kotlin-stdlib 🙂
v
It’s 1.9.23. But switching back to 1.9.10 didn’t make the
StringBuilder
available in
commonMain
https://kotlinlang.slack.com/archives/C3PQML5NU/p1714238818888659?thread_ts=1714145071.665529&amp;cid=C3PQML5NU
Only changing the JDK helped, but it introduced the opposite problem - I can now access
java
classes in
commonMain
, which I guess must not be allowed.
k
yeh, that sounds wrong
v
For the record: • I’m using official library project template • I’m using completely fresh installation of the latest IDEA from Toolbox • I’m using JDK from the Android Studio installation, which is JBR 17. But I tried other JDKs and result was the same. • My architecture is arm64
m
@Vladislav Ermolin If your only target is
jvm
then you can access Java classes from
commonMain
. You can access anything that is in all the targets you are targetting. I don't know why the IDE is showing you an error when it actually compiles. I've encountered a lot of different IDE bugs with KMP over the years. Sometimes doing the invalidate cache stuff and restarting Intellij helps sometimes it doesn't. Best solution is normally to try and find a work around like changing the Java SDK version and filing a bug with JetBrains.
👍 1
v
You can access anything that is in all the targets you are targetting.
Indeed, makes sense.
BTW, I’ve found a difference between 2 JVMs I used. For some reason that “other JBR 17”, which was causing an issue, shows “empty” classpath in the project structure dialog. Like if the installation were corrupted or smth like that.
Thanks everyone for the help! I think my issue is solved.