-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Kotlin support
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.
For injection annotations, you have to use the lateinit
keyword:
@StringRes
protected lateinit var myString: String
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
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.
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.
- 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
If you are using data binding, add this block to your build.gradle:
kapt {
correctErrorTypes = true
}
19/11/2020 The 4.8.0 release is out !
- Get started!
- Cookbook, full of recipes
- Customize annotation processing
- List of all available annotations
- Release Notes
- Examples
- Read the FAQ
- Join the Mailing list
- Create an issue
- Tag on Stack Overflow
- Ask on Gitter