Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidAnnotations - ViewById cannot be used on a private element

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?

like image 531
kristyna Avatar asked Mar 13 '26 00:03

kristyna


1 Answers

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.

like image 195
BakaWaii Avatar answered Mar 15 '26 13:03

BakaWaii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!