butterknife
butterknife copied to clipboard
Generated _ViewBinding id and BindView implmentation desync
When I navigate to a certain screen at runtime, there is a crash because of an incorrect binding.
java.lang.IllegalStateException: Required view 'et_inspection_section_header_total' with ID 2131624440 for field 'address1' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
My library project contains both XML layouts and @BindView(R2...)
implementations, and uses:
classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
and both butterknife libraries.
My main project references the XML layouts of the library, and has the erroneous @BindView
code generations using:
com.jakewharton:butterknife:8.5.1
com.jakewharton:butterknife-compiler:8.5.1
The desync is that the implementation of @BindView(R.id.txt_cust_view_address1)
in class Abc
is generating the code in class Abc_ViewBinding
:
target.address1 = Utils.findRequiredViewAsType(source, R.id.et_inspection_section_header_total, "field 'address1'", TextView.class);
where et_inspection_section_header_total
is the wrong id, not a field of class Abc
and is from an unrelated class which only exists in the library as a layout, not used/referenced within any part of the project in either module(however existing layouts/ids have been known to collide as well).
Note:
I found that the generated _ViewBinding
class will correctly bind to views that were in the top level of the layout, and the incorrect generations were from the
<include layout="@layout/partial_cust_view_detail"
Both layouts reside in the library project.
I also encountered the same problem
Same with me
Let's thumbs up the issue if you have had the same problem. This way the comments can contain only additional relevant information to the issue or this repo.
Consider deleting your comment and switching to the thumbs up.
I have similar problem and I found some id values of R(main project) and R2(library project) are same.
// SettingsActivity.java (in main project)
...
@BindView(R.id.nickname)
TextView mNicknameView;
...
// SettingsActivity_ViewBinding.java
...
target.mNicknameView = Utils.findRequiredViewAsType(source, R.id.notification_icon, "field 'mNicknameView'", TextView.class);
...
// R.java (in main project)
...
public static final int nickname=0x7f1000e5;
...
// R2.java (in library project)
...
@IdRes
public static final int notification_icon = 0x7f1000e5;
...
Oh, I used 8.5.1 before and just found it fixed on 8.6.0 (https://github.com/JakeWharton/butterknife/blob/master/CHANGELOG.md#version-860-2017-05-16)
It works well on 8.6.0. Thanks for resolving the issue :)
The same for me, even after update to 8.6.0
I've performed a clean build(even Invalidate Cache/Restart), but the generated class still has a wrong ID, that clashes with ID from the other module:
target.productDetailName = Utils.findRequiredViewAsType(source, example.core.R.id.subtotal_label, "field 'productDetailName'", TextView.class);
(this is for a class from the 'product' module, but it binds a view from the 'core' module)
Just wanted to note that I'm also seeing this on the latest 9.0.0-SNAPSHOT.
If you just add another binding to any class, it fixes itself. I have no idea what that means or if it'll help with debugging, but it's related to how many bindings there are or processing time or something.
So there's a workaround if this happens. Need to clean and then delete the build cache ($HOME/.gradle/caches
for me), causing a full rebuild. Then builds should read the right ID.
It's probably a build cache issue rather than BK, but I haven't tried disabling it entirely yet.
I also have this happening once in a while. For me it always happens on @OnClick
methods. I'm using version 8.8.1
It happens when you add new id to any your layout.xml file. Only clean and build helps right now. It's weird :(
This still happens in version 9.0.0-rc2
This still happens in version 10.0.0
Looks like gradle issue
Problem is only, when "Instant Run" is enabled