Solvedbutterknife 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!

30 Answers

✔️Accepted Answer

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

Other Answers:

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.

@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?

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

Related Issues:

80
butterknife 依赖最新版的9.0.0-rc2运行报错
@moneeWong Add this to your build.gradle Error: Static interface methods are only supported starting...
64
butterknife Android X support
I've recently attempted to upgrade a project which uses Butterknife v8.8.1 to make use of the new An...
36
butterknife All bind view are null
For mixed Java/Kotlin projects add the following lines to your module level build.gradle ...
13
butterknife android studio 3.2 canary with androidx not compiling (Today is google IO 2018)
error: package android.support.annotation does not exist The imports inside the auto generated _View...
6
butterknife ButterKnife generates ViewBinding class with wrong resource ID
i am seeing this issue with 8.8.1 too especially after adding a new xml layout via copy and paste ...