简体   繁体   中英

EpoxyModels are not working in feature modules/library projects in Android

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

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM