Does `@file:JvmName` work in `commonMain` ? Or, is...
# multiplatform
e
Does
@file:JvmName
work in
commonMain
? Or, is it expected to work? Currently the file name is not changed.
1
m
It should work. What makes you think it doesn't?
You'll have to look at the
.class
files
e
Looking at the outputted
.class
files, I see it doesn't rename them as I would expect.
JvmName
doesn't work at all
m
Interesting 🤔
e
If I use it in
jvmMain
, it works
I mean it might just be it's not designed to work in common, but at that point I would not expose it
m
It's working for us here
Copy code
$ ls libraries/apollo-api/build/classes/kotlin/jvm/main/com/apollographql/apollo3/api/Adapters.class                   
libraries/apollo-api/build/classes/kotlin/jvm/main/com/apollographql/apollo3/api/Adapters.class
e
@mbonnin 1.9.0?
m
Yes
e
Damn, what am I doing wrong then. Wait, I'll post a couple screenshot
m
I'll try a simpler reproducer
e
image.png,image.png
Same in the sense that it works?
m
Yes, sorry.
@file:JvmName
seems to be working there:
Copy code
mbonnin:~/git/test-jvmname$ ./gradlew build

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to <https://docs.gradle.org/8.2/userguide/command_line_interface.html#sec:command_line_warnings> in the Gradle documentation.

BUILD SUCCESSFUL in 394ms
4 actionable tasks: 4 up-to-date
mbonnin:~/git/test-jvmname$ ls build/classes/kotlin/jvm/main/IAmRenamed.class 
build/classes/kotlin/jvm/main/IAmRenamed.class
e
What the heck
m
but you shouldn't trust me, try the reproducer
(I most of the times don't trust myself blob upside down)
e
Trying now
m
Ah well, adding a native target yields this 🤔
Copy code
> Task :compileKotlinMacosArm64 FAILED
e: file:///Users/mbonnin/git/test-jvmname/src/commonMain/kotlin/Main.kt:1:7 Unresolved reference: JvmName
Ah but that's because I need to import the symbol then
e
I do have a native target, mingwX64
and also js
m
Yep, looks like the symbol is automatically added for JVM but not other targets. Adding the import makes it work 👍 https://github.com/martinbonnin/test-jvmname/commit/1f0ed66a5f12abd61acfa80a58b514ad21900091
Could it be a stale
.class
file?
e
I'm trying to setup your reproducer in the same way my own project is setup. Let's see
@mbonnin make the fun
expect
and actualize it in
jvmMain
, while keeping the
JvmName
annotation in common, and you'll reproduce
m
Sounds like you may need
@file:JvmMultifileClass
?
e
Never used it, didn't even know it existed lol
But it doesn't change the result, still not renamed
m
If you have 2 files (one in commonMain, one in jvmMain) you potentially have 2 JVM classes
So you need 2
@file:JvmName
annotations
e
If you have 2 files (one in commonMain, one in jvmMain)
Yes you're right. The fact they have the same name is playing tricks in my brain
nod 1
m
And if you use the same name you'll end up with
Copy code
e: file:///Users/mbonnin/git/test-jvmname/src/commonMain/kotlin/Main.kt:1:1 Duplicate JVM class name 'IAmRenamed' generated from: IAmRenamed, IAmRenamed
e: file:///Users/mbonnin/git/test-jvmname/src/jvmMain/kotlin/Main.jvm.kt:1:1 Duplicate JVM class name 'IAmRenamed' generated from: IAmRenamed, IAmRenamed
This is where
@file:JvmMultifileClass
comes in
e
But wait, I don't get any error tho
m
Latest versions of IJ use
.jvm.kt
suffixes, etc.. for the target specific files, I tend to use that too
But wait, I don't get any error tho
Might be that one of those is empty? If there's no definition, I'm suspecting the Kotlin compiler doesn't emit a class at all
And no class => no conflict 🙂
👀 1
e
I'll make a pull on your repro so you see what I did
👍 1
m
You need to add
@JvmName
here
e
That's what I'm doing in the real project. But then I can just get rid of it in common
m
The
Main.kt
file in commonMain and the one in jvmMain are completely different files
e
Makes sense, basically I should just stop thinking in terms of file names
m
Exactly. The
actual
file name can be completely unrelated to the
expected
definition
At the end of the day, 2 different files are fed to the compiler and therefore if you want to rename both you should use 2
@JvmName
e
you should use 2
@JvmName
Which probably isn't my case as I want it renamed only at the JVM level. But good to know that multifileclass annotation exists. I need to scan that file for the other annotations too
JvmPackageName
seemed interesting 👀, but it's internal.
One gripe I've got with KMP is that in JS package names are translated to namespaces, which makes it harder to use from TypeScript. As soon as I've seen
JvmPackageName
I thought I could have a single name as package, and then rename it for the JVM. That would have been cool
m
Which probably isn't my case as I want it renamed only at the JVM level.
commonMain sources also create JVM classes
e
I can't see
ZCharsetKt
tho. I've removed the annotations in
commonMain
, so I suppose that would be the name, right?
m
Depends your setup. If your commonMain classes only contain expected declarations then there's a good chance no .class file is written at all
e
@mbonnin than yes, that's exactly the case. OK I think I got it now, thanks!
🎉 1
And sorry for having you make a repro
m
No worries at all, this is what repros are for !
gratitude thank you 1
I have a small kscript that creates repro templates automatically so it's a lot of fun to create them 🤓