Kotlin support - WonderCsabo/androidannotations GitHub Wiki
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:
- Add
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
to yourbuildscript
dependencies block. - Apply the
kotlin-allopen
plugin - 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
}