注释:@EViewGroup-注释参数必须是Android库模块中的编译时常量错误

注释:@EViewGroup-注释参数必须是Android库模块中的编译时常量错误,android,annotations,android-library,android-annotations,android-module,Android,Annotations,Android Library,Android Annotations,Android Module,我正试图将我现有的一些应用程序代码拆分成一个android库模块。在将我的工作代码复制到模块目录时,我遇到了一个注释问题,其中行@EViewGroup(R.layout.dialog\u action\u item)会在消息中引发编译时错误 注释参数必须是编译时常量 我不明白为什么当完全相同的代码在应用程序模块中工作时,这会突然成为一个问题。两个模块的gradle文件都实现了相同的依赖关系,布局文件也是旧布局文件的副本 查看文件: import android.content.Context i

我正试图将我现有的一些应用程序代码拆分成一个android库模块。在将我的工作代码复制到模块目录时,我遇到了一个注释问题,其中行
@EViewGroup(R.layout.dialog\u action\u item)
会在消息中引发编译时错误

注释参数必须是编译时常量

我不明白为什么当完全相同的代码在应用程序模块中工作时,这会突然成为一个问题。两个模块的gradle文件都实现了相同的依赖关系,布局文件也是旧布局文件的副本

查看文件:

import android.content.Context
import android.graphics.Typeface
import android.os.Build
import android.support.annotation.AttrRes
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.widget.FrameLayout
import com.lam.locationmapservicelib.R
import kotlinx.android.synthetic.main.dialog_action_item.view.*
import com.lam.locationmapservicelib.utils.ImageLoader
import com.lam.locationmapservicelib.utils.ViewManager
import com.lam.locationmapservicelib.views.dialog.DialogActionItemModel
import org.androidannotations.annotations.EViewGroup

@EViewGroup(R.layout.dialog_action_item)
open class DialogActionItem : FrameLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, @AttrRes defStyleAttr: Int) : super(context, attrs, defStyleAttr)

some methods..

}
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dialog_button_size">

    <TextView
        android:id="@+id/buttonText"
        style="@style/Body1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginEnd="@dimen/padding_16"
        android:layout_marginStart="@dimen/padding_16"
        android:ellipsize="end"
        android:lines="1"
        android:textAlignment="center" />
</FrameLayout>
生成错误堆栈:

`e: ...\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\fragments\map\MapFragment.java:5: error: incompatible types: <null> cannot be converted to int
@org.androidannotations.annotations.EFragment(value = null)
                                                      ^
e: ...\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\views\dialog\Dialog.java:5: error: incompatible types: <null> cannot be converted to int
@org.androidannotations.annotations.EViewGroup(value = null)
                                                       ^
e: ...\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\views\dialog\views\DialogActionItem.java:5: error: incompatible types: <null> cannot be converted to int
@org.androidannotations.annotations.EViewGroup(value = null)`
`e:…\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\fragments\map\MapFragments.java:5:错误:不兼容的类型:无法转换为int
@org.androidannotations.annotations.EFragment(值=null)
^
e:…\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\views\dialog\dialog.java:5:错误:不兼容的类型:无法转换为int
@org.androidannotations.annotations.EViewGroup(值=null)
^
e:…\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\views\dialog\views\DialogActionItem.java:5:错误:不兼容的类型:无法转换为int
@org.androidannotations.annotations.EViewGroup(值=null)`
gradle和布局文件与DialogActionItem类位于同一模块(locationmapservicelib)中


非常感谢您的帮助

不幸的是,
R
字段在Android库项目中不是常量,因此我们不能简单地将它们放入注释参数中

有两种方法可以解决此问题:

  • 使用
    resName
    参数并传递一个字符串:
    @EViewGroup(resName=“dialog\u action\u item”)
  • 使用ButterKnife Gradle插件在
    R2
    类中生成最终字段,并使用它们:
    @EViewGroup(R2.layout.dialog\u action\u item)

对于这两个,您必须向AndroidAnnotations添加配置。请参阅。

我最近看到了类似的内容,但在我的例子中,我引用了一个布局文件,该文件位于错误的构建变量源集中。如果您的问题不是这样,请将完整的编译错误堆栈跟踪添加到您的问题中?谢谢:)我没有为这个项目使用构建变量,所以所有布局文件都在同一个目录中(每个模块)。我在上面的描述中添加了短错误堆栈。您是否使用发布/调试源集?如果没有,恐怕我就不会想到别的了。
`e: ...\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\fragments\map\MapFragment.java:5: error: incompatible types: <null> cannot be converted to int
@org.androidannotations.annotations.EFragment(value = null)
                                                      ^
e: ...\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\views\dialog\Dialog.java:5: error: incompatible types: <null> cannot be converted to int
@org.androidannotations.annotations.EViewGroup(value = null)
                                                       ^
e: ...\LocationMapService\locationmapservicelib\build\tmp\kapt3\stubs\debug\com\lam\locationmapservicelib\views\dialog\views\DialogActionItem.java:5: error: incompatible types: <null> cannot be converted to int
@org.androidannotations.annotations.EViewGroup(value = null)`