airbnb / epoxy

Epoxy is an Android library for building complex screens in a RecyclerView
https://goo.gl/eIK82p
Apache License 2.0
8.51k stars 728 forks source link

"annotation type not applicable to this kind of declaration" with KSP #1272

Open davidbilik opened 2 years ago

davidbilik commented 2 years ago

👋 I am trying the new KSP with Epoxy 5.0.0-beta05 in our project and it looks like there is a difference in generating java models through KSP and through KAPT. We use this signature of our models

@EpoxyModelClass
abstract class SettingsItemModel(
    @EpoxyAttribute var text: Text,
    @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) var onClick: () -> Unit,
) : EpoxyModelWithViewBinding<ItemSettingBinding>(R.layout.item_setting) {

    override fun ItemSettingBinding.bind() {
       ...
    }
}

because it allows us to use the generated model like

class SettingsController : TypedEpoxyController<Data>() {

    override fun buildModels(data: Data?) {
        settingsItem(
            text = Text.Resource(R.string.settings_addresses),
            onClick = { ... }
        ) {
            id("address)
        }

which we prefer instead of having lateinit vars inside our models. However, with KAPT this works fine but KSP throws an error

...l/app/build/generated/ksp/devApiDebug/java/.../SettingsItemModel_.java:37: error: annotation type not applicable to this kind of declaration
  public SettingsItemModel_(@EpoxyAttribute(value = {}, hash = true, setter = true) Text text,
                            ^
...l/app/build/generated/ksp/devApiDebug/java/.../SettingsItemModel_.java:38: error: annotation type not applicable to this kind of declaration
      @EpoxyAttribute(value = EpoxyAttribute.Option.DoNotHash, hash = true, setter = true) Function0<Unit> onClick) {

and an error points to a constructor parameters annotations which shouldn't be there I guess

public class SettingsItemModel_ extends SettingsItemModel implements GeneratedModel<ViewBindingHolder>, SettingsItemModelBuilder {
  private OnModelBoundListener<SettingsItemModel_, ViewBindingHolder> onModelBoundListener_epoxyGeneratedModel;

  private OnModelUnboundListener<SettingsItemModel_, ViewBindingHolder> onModelUnboundListener_epoxyGeneratedModel;

  private OnModelVisibilityStateChangedListener<SettingsItemModel_, ViewBindingHolder> onModelVisibilityStateChangedListener_epoxyGeneratedModel;

  private OnModelVisibilityChangedListener<SettingsItemModel_, ViewBindingHolder> onModelVisibilityChangedListener_epoxyGeneratedModel;

  public SettingsItemModel_(@EpoxyAttribute(value = {}, hash = true, setter = true) Text text,
      @EpoxyAttribute(value = EpoxyAttribute.Option.DoNotHash, hash = true, setter = true) Function0<Unit> onClick) {
    super(text, onClick);
  }

I did not make any changes to the code when changed KAPT for KSP but I don't think I have an option to tweak this.

elihart commented 2 years ago

thanks for raising this use case. I imagine it should be fine to support this with some tweaks to the KSP configuration, but it might be a while before I can get to it.

tsuellow commented 4 months ago

any progress on this? on my 2 most important libraries epoxy and dagger I have had to go back to kapt in order to keep stuff working.