IntDef and StringDef not being checked at compile time


While developing for Android we come across a lot of different annotation classes like IntDef and StringDef that effectively replace enums.

On Java that is no problem, as those annotations are checked in the compile time and we get suggestions and warnings based on that. Think of when you are setting the visibility of a view(in java) and the first suggestions are always INVISIBLE/GONE/VISIBLE, that is the power of IntDef. Programming for Kotlin? Then you always have to look up what is expected as input.

Now we can create the defs in Kotlin itself, which is great, but suggestions and warnings are still not there. Do we have some updates on that? am I missing something? Or what are your opinions on the subject?


This checking doesn’t come from the compiler, but from Android lint. Work to make android lint language independent is being done, but if I’m not mistaken you’ll need a newer version of Android Studio for it.


That makes more sense, thanks for answering. I guess I will try to write my own Lint rules if anything is still missing after the update :slight_smile:


@raeglan I am trying to use @StringDef instead of Enum in my code.

@StringDef(TYPE_A , TYPE_B, TYPE_C)
annotation class CodeResponse

and TYPE_A, TYPE_B and TYPE_C are defined in the companion object of outer class (not of class CodeResponse)

Is this the correct way to do it? also how to access these (TYPE_A, TYPE_B and TYPE_C) in a when expression


Hi @ggadkari, that is a bit of topic in this thread but I will give my opinion anyway.

If you have an outer class wrapping this CodeResponse and containing the companion object with your Long values called CodeResponses then just use CodeResponses.TYPE_A from inside a Kotlin file or CodeResponses.Companion.TYPE_A from a java class.