2

EpoxyModels are not compiling when using inside feature modules of Android App.

@EpoxyModelClass(layout = R.layout.layout_foo) //an annotation argument must be a compile-time constant here
abstract class  FooModel : EpoxyModelWithHolder<FooModel.FooHolder>() 
{
            ...
            ...
        
    class FooHolder : BaseEpoxyHolder() 
     {
       val textViewTitle: TextView by bind((R.id.textViewTitle))
       //bind is the method borrowed from [here](https://github.com/airbnb/epoxy/blob/963ef0fd850bd379da7b0be6a2ada25d01ae0ee7/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/helpers/KotlinEpoxyHolder.kt#L20)
     }
}

The above code complains about "an annotation argument must be a compile-time constant" for layout = R.layout.layout_foo line.

Again based on the documentation looks like, butterknife needs to be used for library projects(feature modules are kind of library project to some extent) that would generate R2 classes based on this

Below is the modified code with Butterknife which also I think it's overkill. Not sure, why I was not able just to do findviewbyId.

ModelClass(layout = R2.layout.layout_foo)
abstract class  FooModel : EpoxyModelWithHolder<FooModel.FooHolder>() {

    @EpoxyAttribute
    lateinit var fooDto: Foo

    override fun bind(holder: FooHolder) {
        holder.textViewTitle.text = fooDto.title
    }

    class FooHolder : BaseEpoxyHolder() {
        @BindView(R2.id.textViewTitle) lateinit var textViewTitle: TextView
    }
}

Below is the error coming from viewholder with butterknife

kotlin.UninitializedPropertyAccessException: lateinit property textViewTitle has not been initialized

TylerH
  • 20,799
  • 66
  • 75
  • 101
Bulu
  • 1,351
  • 3
  • 16
  • 37

1 Answers1

13

As said in:

https://github.com/airbnb/epoxy/issues/819#issuecomment-576728923

There is a problem with using that annotation on library project.

For the moment you can use:

@EpoxyModelClass
abstract class MyModel : EpoxyModelWithHolder<MyHolder>() {

    override fun getDefaultLayout(): Int {
        return R.layout.my_layout
    }
}
Luca Pellizzari
  • 324
  • 4
  • 12
  • Do you think I still need R2 for Viewholder class too ?class FooHolder : BaseEpoxyHolder() { @BindView(R2.id.textViewTitle) lateinit var textViewTitle: TextView } – Bulu Jan 21 '20 at 22:17
  • No, if the layout is in the module it doesn't have to pass from the annotation processor, so no need for R2. I'm not using it, if you config is different, you might evaluate. – Luca Pellizzari Jan 21 '20 at 22:25