Hey I am working on a library in Kotlin In this l...
# android
t
Hey I am working on a library in Kotlin In this library, if I have some variable inside
companion object
, than those variable are treated as static in Java But when I try to access those variable in a java project, which is using library, It is like :
Copy code
MyClass.Companion.abcVariable
I searched and found I had to append @JvmStatic in front of variable so that java can access it directly like :
Copy code
MyClass.abcVariable
This works for
classes
but for
interfaces
it requires a min API level of 24 and my Min API level is 21 Is there any other way to directly access companion object variables for
interface
in API projects supporting min API level less than 24 like :
Copy code
MyClass.abcVariable
g
Mark your variable as @JvmField or as
const
(if it’s possible for your case), otherwise your property will be compiled to interface default method (java 8 required) and it doesn’t work on old Android
t
For Properties : -
const
is working for constants -@JvmField is not working for properties and is giving compile time error as
JvmField
can not be applied to a property inside companion object For functions in
companion object
None of these two are working I need something for properties as well as functions
g
is not working for properties
What do you mean? yes, of course JvmField converts property to field, but it works: Kotlin will generate raw JVM field instead of property (field + getter) and this is your only solution
oh, I got what you mean, you have an interface
t
Yes, I am working on `interface`and i needed something that works for both
companion object
member variables and functions
g
Probably this is not possible to do. I would create issue for that kotl.in/issue
t
thanks for that, can you provide me issue link to track it?
g
I didn’t create. I would do that on your place %)
t
sure my apologies for misunderstanding that Will do that rightaway