AndroidAnnotations 版本: 4.3.1
Android编译SDK版本:26
Kotlin 版本:1.1.3-2
我正在尝试使用 Kotlin 和 AndroidAnnotaions 构建应用程序。构建结束于
Error:Execution failed for task ':app:kaptDebugKotlin'. > Internal compiler error. See log for more details
在 androidannotations.log
中有一大堆错误,例如
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
这就是@ViewById
注解的用法
@ViewById
var description: TextView? = null
Pref 注释的变量也会发生同样的情况。
有其他人面临同样的问题还是只有我?
最佳答案
尝试使用lateinit
:
@ViewById
lateinit var description: TextView
出现此错误的原因可能是支持字段的行为。默认情况下它是不可见的,field
标识符只能在属性的访问器中使用。这就是您得到 @ViewById cannot be used on a private element
的原因。
lateinit
起作用的原因是因为它改变了字段的可访问性。根据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.
所以,@JvmField
是这个问题的另一种解决方案。
@ViewById
@JvmField var helloTextView: TextView? = null
它还改变了字段的可见性,如文档所述:
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.
您也可以引用这个example和 Kotlin docs关于使用注释处理的 Android 框架。
关于AndroidAnnotations - ViewById 不能用于私有(private)元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45701157/