butterknife: ButterKnife generates ViewBinding class with wrong resource ID

I am using a 3rd-party library in our project, and compilation works fine, but the app crashes when it starts up. I looked at the logcat and generate source code, it seems the generated source code has the wrong resource ID.

For example, the source file is

class HomeActivity ... {
    @BindView(R2.id.mode_transition_layout) AppModeTransitionLayout modeTransitionLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

The generated HomeActivity_ViewBinding class has

  @UiThread
  public HomeActivity_ViewBinding(T target, View source) {
    this.target = target;

    target.modeTransitionLayout = Utils.findRequiredViewAsType(source, R.id.infant_description_text, "field 'modeTransitionLayout'", AppModeTransitionLayout.class);
  }

R.id.infant_description_text is a different resource ID, and I’d assume here should use R.id.mode_transition_layout.

At the runtime I get an exception says java.lang.IllegalStateException: Required view 'infant_description_text' with ID 2131822441 for field 'modeTransitionLayout' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.

We are using ButterKnife 8.4.0.

Any help is highly appreciated!

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 29 (5 by maintainers)

Commits related to this issue

Most upvoted comments

i am seeing this issue with 8.8.1 too. especially after adding a new xml layout via copy and paste, and then renaming some ids (not refactoring)

The only secure way is to delete the app/build folder and rebuild the project

Same problem in my project. I have tested with various versions of ButterKnife. Version 8.5.1 works fine. Version 8.6.0 and all above stable produces this error. Cleaning project not resolves this problem.

we’re still seeing this problem, sounds like a legit butterknife bug. @engian i’d recommend reopening

@JakeWharton I fixed my issue, for your reference, here is the build config I changed:

Before (generated code with wrong resource ID):

  • library ‘core’ depends on 3rd-party library
  • library ‘lib’ depends on ‘core’ and 3rd-party library
  • module ‘app’ depends on ‘lib’

I removed the dependency from ‘core’ to 3rd-party library and butterknife works properly.

Our of curiosity, is there a way to invoke butterknife from command line for debugging?

I’m also seeing this issue again, but only when using the Android Gradle Plugin 3.0.0 beta

Cool, I have tried 8.5.2-SNAPSHOT and it works now. Please help to release it once it passes all the test. Thanks so much for you guys’ support!

I am not dealing with this issue anymore. Fortunately or not, I’ve replaced it by native dataBinding.

I have experienced the same issue at the first time and start looking to eliminate butterknife annotation one by one. After I remove all of @BindColor and @BindArray on library project, the butterknife able to generate the correct ViewBinding file. Maybe this could be a temporary solution.

@engian does it happen on clean builds? Sounds similar to #776.