【问题标题】:Annotations: @EViewGroup - annotation argument must be compile-time constant error in Android library module注释:@EViewGroup - 注释参数必须是 Android 库模块中的编译时常量错误
【发布时间】:2019-02-16 03:52:22
【问题描述】:

我正在尝试将我现有的一些应用程序代码拆分为一个 android 库模块。在将我的工作代码复制到模块目录中时,我遇到了一个注释问题,其中@EViewGroup(R.layout.dialog_action_item)casts 行在消息中显示了一个编译时错误

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

当完全相同的代码在应用程序模块中运行时,我不明白为什么这会突然成为问题。两个模块的 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>

分级:

kapt 'org.androidannotations:androidannotations:4.4.0'
implementation 'org.androidannotations:androidannotations-api:4.4.0'

构建错误堆栈:

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

gradle 和布局文件与 DialogActionItem 类位于同一模块 (locationmapservicelib) 中。

非常感谢任何帮助!

【问题讨论】:

  • 我最近看到了类似的东西,但在我的例子中,我引用了一个位于不正确的构建变体源集中的布局文件。如果您不是这种情况,您能否将完整的编译错误堆栈跟踪添加到您的问题中?谢谢:)
  • 我并没有真正为这个项目使用构建变体,所以所有布局文件都在同一个目录中(每个模块)。我在上面的描述中添加了简短的错误堆栈
  • 您使用的是发布/调试源集吗?如果没有,恐怕没有别的想法了。

标签: android annotations android-library android-annotations android-module


【解决方案1】:

很遗憾,R 字段在 Android 库项目中不是常量,因此我们不能简单地将它们放入注解参数中。

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

  • 使用resName参数并传递一个字符串:@EViewGroup(resName = "dialog_action_item")
  • 使用 ButterKnife Gradle Plugin 在R2 类中生成最终字段,并使用它们:@EViewGroup(R2.layout.dialog_action_item)

您必须为这两个添加配置到 AndroidAnnotations。请参阅detailed documentation for library projects

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 2020-10-04
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    相关资源
    最近更新 更多