3

AndroidAnnotations version: 4.3.1

Android compile SDK version: 26

Kotlin version: 1.1.3-2

I am trying to build app using Kotlin and AndroidAnnotaions. The build ends with

Error:Execution failed for task ':app:kaptDebugKotlin'. > Internal compiler error. See log for more details

in androidannotations.log a is a ton of erros like

00:10:43.908 [RMI TCP Connection(91)-127.0.0.1] ERROR o.a.i.p.ModelValidator:77 - org.androidannotations.annotations.ViewById cannot be used on a private element

thats the usage of @ViewById annotation

@ViewById
var description: TextView? = null

The same also happens with Pref annotated vars.

Is anybody else facing the same issue or it's just me?

kristyna
  • 1,360
  • 2
  • 24
  • 41

1 Answers1

7

Try to use lateinit:

@ViewById
lateinit var description: TextView

The reason of getting this error may because of the behavior of the backing field. It is invisible by default and the field identifier can only be used in the accessors of the property. That's why you got @ViewById cannot be used on a private element.

The reason of why lateinit works is because it change the accessibility of the field. According to Kotlin doc:

Late-Initialized properties are also exposed as fields. The visibility of the field will be the same as the visibility of lateinit property setter.

So, @JvmField is another solution for this problem.

@ViewById
@JvmField var helloTextView: TextView? = null

It also changes the visibility of the field as what the doc states:

If you need to expose a Kotlin property as a field in Java, you need to annotate it with the @JvmField annotation. The field will have the same visibility as the underlying property. You can annotate a property with @JvmField if it has a backing field, is not private, does not have open, override or const modifiers, and is not a delegated property.

You may also refer to this example and Kotlin docs about Android frameworks using annotation processing.

BakaWaii
  • 6,732
  • 4
  • 29
  • 41
  • Thank you, it's woking. Works for: @ViewById, @Bean, @Prefs, @StringRes, @RootContext... all non primitive objects. However I still think that my approach should work too: `Without lateinit you'd have to declare a nullable type and add additional nullability checks.` – kristyna Aug 16 '17 at 15:45
  • Yea, I think so at the beginning too. After some research, I found out a reasonable reason for that. I've updated the answer about the reason. – BakaWaii Aug 16 '17 at 17:08
  • `JVMField` is asking for final field. – Gokhan Arik Sep 30 '19 at 02:45