butterknife icon indicating copy to clipboard operation
butterknife copied to clipboard

Generated _ViewBinding id and BindView implmentation desync

Open ersin-ertan opened this issue 7 years ago • 14 comments

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.

ersin-ertan avatar Mar 12 '17 15:03 ersin-ertan

I also encountered the same problem

AndroidJohnsonWang avatar Mar 15 '17 08:03 AndroidJohnsonWang

Same with me

yendangn avatar Apr 04 '17 06:04 yendangn

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.

ersin-ertan avatar Apr 04 '17 07:04 ersin-ertan

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;
...

polysiya avatar May 26 '17 03:05 polysiya

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 :)

polysiya avatar May 26 '17 03:05 polysiya

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)

kyrylchukY avatar Jul 04 '17 20:07 kyrylchukY

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.

TheKeeperOfPie avatar Jan 30 '18 16:01 TheKeeperOfPie

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.

TheKeeperOfPie avatar Mar 07 '18 16:03 TheKeeperOfPie

I also have this happening once in a while. For me it always happens on @OnClick methods. I'm using version 8.8.1

ChrisTitos avatar Jul 27 '18 15:07 ChrisTitos

It happens when you add new id to any your layout.xml file. Only clean and build helps right now. It's weird :(

csivladislavbakan avatar Oct 26 '18 15:10 csivladislavbakan

This still happens in version 9.0.0-rc2

ChrisTitos avatar Dec 19 '18 13:12 ChrisTitos

This still happens in version 10.0.0

MarkerwApk avatar Jan 08 '19 12:01 MarkerwApk

Looks like gradle issue

csivladislavbakan avatar Jan 08 '19 12:01 csivladislavbakan

Problem is only, when "Instant Run" is enabled

MarkerwApk avatar Jan 08 '19 12:01 MarkerwApk