I'm trying to split some of my existing app code into an android library module. While copying my working code into the module directory I've encountered a problem with annotations where the line @EViewGroup(R.layout.dialog_action_item)
casts an compile-time error with the message
An annotation argument must be a compile-time constant
I can't see why this suddenly is a problem when the exact same code works in the app module. Both modules gradle files implement the same dependencies and the layout file is also a copy of the old layout file.
View file:
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..
}
Layout file:
<?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>
Gradle:
kapt 'org.androidannotations:androidannotations:4.4.0'
implementation 'org.androidannotations:androidannotations-api:4.4.0'
Build error stack:
`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)`
The gradle and layout file is of cause in the same module (locationmapservicelib) as the DialogActionItem class.
Any help is greatly appreciated!
Unfortunately the R
fields are not constants in Android library projects, so we cannot simply put them into annotations parameters.
There are two ways to resolve this:
resName
parameter and pass a string: @EViewGroup(resName = "dialog_action_item")
R2
class, and use them: @EViewGroup(R2.layout.dialog_action_item)
You have to add configuration to AndroidAnnotations for both of these. See the detailed documentation for library projects.