Impact to ButterKnife from "Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them as annotation attributes"
Asked Answered
O

2

35

I'm using annotated attributes all over my Android project to bind Views and events to fields and methods.

@BindView(R.id.textViewOrderId)
TextView textViewOrderId;

@OnClick(R.id.buttonDateFilter)
public void onButtonDateFilter(View view) {...}

Migrating out of ButterKnife has become a bit expensive now. I would like to clarify a few things about this new Gradle Plugin change,

  • How will non-final resource ids affect my current project?
  • Will I have to migrate out of ButterKnife for good if I update the Gradle Plugin?
  • How effective is the R2 class mentioned in this Reddit post?
Oblation answered 14/10, 2020 at 12:5 Comment(0)
S
31

This still has no answer. But I suppose there is no option but to replace ButterKnife with an alternative, most probably View Binding.

As it states in this article, View Binding Tutorial for Android: Getting Started:

Additionally, ButterKnife’s development is coming to an end. Jake Wharton, the creator of this library, is deprecating it in favour of View Binding.

From the ButterKnife GitHub page:

Attention: This tool is now deprecated. Please switch to view binding. Existing versions will continue to work, obviously, but only critical bug fixes for integration with AGP will be considered. Feature development and general bug fixes have stopped.

Schlessel answered 16/10, 2020 at 17:3 Comment(3)
What if we're using a view holder in recyclerView? I don't think the view biding still works under this circumstance. So again, another disaster to developers.Sayed
In Kotlin, there's no need to use view binding, because kotlin android extensions does the trick way better. However, still we need to set onClickListener in code, so that's no alternative to butter knife.Pauperism
@Kimi Chiu You most certainly can use viewbinding in a viewholder. You just pass a binding object into the ViewHolder constructor instead of a view, and then call super on the .getRoot() of whatever you passed in. to do this, in OnCreateViewHolder, you simply return new ViewHolder(ItemViewBinding.inflate(LayoutInflater.from(parent.getcontext(), parent, false)); Constructor looks like this: public ViewHolder(ItemViewBinding vb) { super(vb.getRoot()); }Nahamas
T
5

As suggested by Jake Wharton, you can apply the butterknife-plugin and use R2 instead of R to access resource ids.

Toowoomba answered 29/10, 2020 at 14:19 Comment(3)
can you put a link here for that information please? Where the R2 is being mentioned by Jake.Rociorock
@KidusTekeste meanwhile we're also using viewbinding and its quite nice to use. The R2 solution is not ideal in the long run I believe, since butterknife isn't continued any more.Toowoomba
@Kidus the link is in the question.Oblation

© 2022 - 2024 — McMap. All rights reserved.