https://kotlinlang.org logo
#android
Title
# android
t

Tarun

04/10/2018, 9:29 AM
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

gildor

04/10/2018, 9:35 AM
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

Tarun

04/10/2018, 9:46 AM
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

gildor

04/10/2018, 9:51 AM
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

Tarun

04/10/2018, 9:57 AM
Yes, I am working on `interface`and i needed something that works for both
companion object
member variables and functions
g

gildor

04/10/2018, 9:59 AM
Probably this is not possible to do. I would create issue for that kotl.in/issue
t

Tarun

04/10/2018, 10:02 AM
thanks for that, can you provide me issue link to track it?
g

gildor

04/10/2018, 10:03 AM
I didn’t create. I would do that on your place %)
t

Tarun

04/10/2018, 10:10 AM
sure my apologies for misunderstanding that Will do that rightaway