Hello, I’m trying to create a utility class that’s Java compatible. I was able in the past to do the...
s
Hello, I’m trying to create a utility class that’s Java compatible. I was able in the past to do the following:
Intents.kt
Copy code
@file:JvmName("IntentsUtil")
@file:JvmMultifileClass

open class Intents {
    companion object
}
PlayStoreIntents.kt
Copy code
@file:JvmName("IntentsUtil")
@file:JvmMultifileClass

fun Intents.Companion.viewAppOnPlayStore(appUri: String): Intent =
    Intent(Intent.ACTION_VIEW).apply {
        data = Uri.parse(appUri)
        setPackage(Apps.PLAY_STORE.url)
    }
When invoked Java side was able to simply:
Copy code
IntentsUtil.viewAppOnPlayStore("foo");
Now however, it requires me to do:
Copy code
IntentsUtil.viewAppOnPlayStore(Intents.Companion, "foo");
Any idea to why that is? How can I avoid passing the Companion object?
n
why is it an extension method?
s
I have want to group different extension methods in different files. It would be too big of a mess to have them all in one file.
r
I would think this would work with
JvmMultifileClass
, but I'm guessing it doesn't play nice with companion objects. Can you make
Intents
an object? Probably worth filing a bug report
s
I just tried using an object, and it just changed what I need to pass to the method:
Copy code
IntentsUtil.viewAppOnPlayStore(Intents.INSTANCE, "foo");
Good call, I’ll log a bug. It looks like a regression.
n
"1.14.2"
did you mean 1.4.32?
s
Updated. Thanks!
a
I don't know why it worked before but I don't think this is supposed to work. You can see that
@JvmStatic
doesn't work on extensions. This makes sense because what if the extension is defined in a different module? I would say defining the utilities as top-level functions is a better choice.
s
What if the extension is defined in a different module? What’s the worry there not following. Also can you elaborate how you would define these extensions?
a
Modules are built independently. Assuming your
Intents
class is in module A, the compiled java class
Intents.class
is also in module A. Then you add an extension for
Intents.Companion
in module B, but it is impossible to insert the method into
Intent.class
which is compiled in module A.
s
I don't see the problem. Don't we add extensions of classes built in entirely different modules/projects all the time?
a
I’m saying that this is the reason you can’t make extensions static.
r
Yeah, I assume
JvmMultifileClass
won't work cross module. But if you're in the same module and using
JvmMultifileClass
, even across multiple files (which is what it's for), there's no reason
JvmStatic
wouldn't work. The whole point of
JvmMultifileClass
is to allow splitting a Java class across multiple files, so it's packing non-static methods into a single classfile already; static should be possible as well.
@Sami Eljabali it looks one solution is to use top-level functions in Kotlin with the
JvmMultifileClass
, see the example https://kotlinlang.org/docs/java-to-kotlin-interop.html#package-level-functions I'd still think objects should be supported eventually, but idk if they are at the moment.
🙏 1