Home » Android » Android: Why do we need to use R2 instead of R with butterknife?

Android: Why do we need to use R2 instead of R with butterknife?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’ve been using butterknife for a few months and I’ve just noticed in its documentation that it says:

Now make sure you use R2 instead of R inside all Butter Knife
annotations.

Why is that? I’ve been using R and everything works perfect.

How to&Answers:

Using R2 is only necessary for building Android Library projects.

https://github.com/JakeWharton/butterknife#library-projects

It has to do with fact that the values in the R.java generated class aren’t declared as “final” when built as part of a library. The annotations used for @BindView() need these R. values to be final and not subject to change later on.

I’m sure someone could explain all of this better — but the bottom line is – you’re just fine using “R.” values in @BindView in an Android application