Skip to main content

Again: Supporting Android Typedefs in Kotlin

Before reading on, it is important to note that Android lint checks do not run on Kotlin code. However, it is possible to annotate Kotlin code such that lint can correctly check Java code against the annotated interfaces.

There is an issue tracking lint support for Kotlin here.

Typedef (Enumerated Annotations)

Previously, I wrote about how to get a library written in Kotlin to interoperate with IntDef and StringDef. The method I outlined got the lint check working in a partial state. However, here is what you have to do to get the annotation to work properly.

In Kotlin, declare the constants that are the values in the typedef.


package com.example.datepicker

class MyKotlinClass {
    companion object {
        const val YEAR = 1L
        const val MONTH = 2L
        const val WEEK = 3L
        const val DAY = 4L

The actual typedef is must be declared in Java.

package com.example.datepicker


@IntDef({MyKotlinClass.YEAR, MyKotlinClass.MONTH, MyKotlinClass.WEEK, MyKotlinClass.DAY})
public @interface DatePickedKind {}

You can now annotate your Kotlin and Java code freely with @DatePickedKind, and Lint will properly check Java code against the annotation.