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

1

9

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.

Shiv answered 21/2, 2017 at 8:22 Comment(3)
I found that the ids value in R2 for any res (color, dimen, ...) is the same as in R. only the difference in R2 is the annotation like "@DrawableRes" and "@IdRes", and used this annotation to be sure you return correct and the expected type. for example "@ColorRes" return value is expected to be a color resource reference, and "@DrawableRes" return value is expected to be a drawable resource reference.Perrone
@Perrone So you're saying that if I use R2 I will get compile time errors in case I try to bind an id to a wrong type?Shiv
I excepted that, annotations help us to improve code inspection developer.android.com/studio/write/annotations.htmlPerrone
C
16

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

Carbonado answered 7/3, 2017 at 15:18 Comment(2)
Okay I got it now. Thanks!Shiv
For a bit further explanation, I have an answer here #48577606Marlomarlon

© 2022 - 2024 — McMap. All rights reserved.