Kotlin support

Csaba Kozák edited this page Nov 2, 2017 · 1 revision

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

14/10/2018 The 4.5.1 release is out !

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.