androidkotlinandroid-annotationskotlin-android-extensions

How to use AndroidAnnotation @SharedPref with Kotlin


I'm trying to use AndroidAnnotations @SharefPref within kotlin, but Iget following error

org.androidannotations.annotations.sharedpreferences.Pref can only be used on an element that extends org.androidannotations.api.sharedpreferences.SharedPreferencesHelper

What am I doing wrong?

//Interface
@SharedPref(SharedPref.Scope.APPLICATION_DEFAULT)
open interface MyPreferences {
    @DefaultInt(-1)
    fun someIntValue():Int
}

//Fragment
@Pref
lateinit open var sharedPref:CongressPreferences_

//usage within fragment
val get: Int = sharedPref.selectedEventId().get()

Solution

  • This is due to a bug in the Kotlin annotation processor.
    To fix this, you must add correctErrorTypes = true to your kapt block.

    kapt {
      correctErrorTypes = true
    }
    

    Also make sure you are using the latest Kotlin version (as of this moment: 1.1.3).