Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Kotlin support

Csaba Kozák edited this page Dec 6, 2018 · 2 revisions

AndroidAnnotations supports processing Kotlin code. You should be able to use the same functionality as with Java code. To see how to setup AndroidAnnotations with Kotlin, check out the example project.

Injecting into properties

For injection annotations, you have to use the lateinit keyword:

@StringRes
protected lateinit var myString: String

Injecting into primitive properties

For primitive properties, you cannot use lateinit, you have to use @JvmField instead. Also you have to mark the property as final (@JvmField requirement):

@Extra
@JvmField
protected final var myIntExtra: Int = 0

Final members

Kotlin classes and functions are final by default. Many AndroidAnnotations annotations need non-final classes (all enhanced component annotations, like @EActivity), and non-final methods (all method overriding annotations, like @Backgound). To make these work with AndroidAnnotations, you have to mark them as open. Alternatively you can setup kotlin-allopen plugin as described in the next section.

kotlin-allopen plugin (requires Kotlin 1.0.6+)

In AndroidAnnotations 4.4.0 we added a utility annotation that can be used to tell the kotlin compiler to not declare classes or methods as final if they are annotated with our annotations.

How to:

  1. Add classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" to your buildscript dependencies block.
  2. Apply the kotlin-allopen plugin
  3. Configure the plugin to take care of our annotations.
allOpen {
    annotation("org.androidannotations.api.KotlinOpen")
}

For more details about the kotlin-allopen plugin you can check the Kotlin Compiler Plugins Documentation

Data binding

If you are using data binding, add this block to your build.gradle:

kapt {
    correctErrorTypes = true
}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally