3

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!

SteamedCow
  • 197
  • 1
  • 13
  • I saw something similar recently, but in my case I was referencing a layout file that was in the incorrect build variant source set. If that's not the case for you, can you please add the full compilation error stack trace to your question? Thanks :) – stkent Jul 23 '18 at 20:53
  • I'm not really using build variants for this project so all layout files are in the same directory (per module). I've added the short error stack in the description above – SteamedCow Jul 23 '18 at 21:21
  • Are you using release/debug source sets? If not, nothing else comes to mind I'm afraid. – stkent Jul 24 '18 at 10:17

1 Answers1

3

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:

  • use the resName parameter and pass a string: @EViewGroup(resName = "dialog_action_item")
  • use the ButterKnife Gradle Plugin to generate final fields in 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.

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105