4

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()
longi
  • 11,104
  • 10
  • 55
  • 89
  • 1
    Isn't the error message self-explanatory? `MyPreferences` does not extend `SharedPreferencesHelper`. – azizbekian Jul 07 '17 at 13:54
  • @azizbekian No it's not. Working with `Kotlin` and `Annotations` sometimes throws errors, which are not related to the real issue. This happens if the annotated code gets generated. (see solution below) – longi Jul 07 '17 at 14:47

2 Answers2

5

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).

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105
  • I found the fixed bug report at android annotations, but it didn't told about adding `correctErrorTypes` ( https://github.com/androidannotations/androidannotations/issues/334 ) Thanks! – longi Jul 07 '17 at 14:41
  • You found the wrong issue, it was reported in 2012. :) – WonderCsabo Jul 07 '17 at 15:01
0

I just wanna extend on @WonderCsabo 's answer. His answer almost saved me, but not fully.

After adding this to my app label build gradle.

kapt { correctErrorTypes = true }

I wasn't able to run my app.

Then I closed my android studio and then run Android studio again as administrator. Voila! it works like charm.

Thank you @WonderCsabo

Md. Arif
  • 448
  • 1
  • 5
  • 13