Butterknife: ButterKnife generates ViewBinding class with wrong resource ID

Created on 18 Oct 2016  ·  29Comments  ·  Source: JakeWharton/butterknife

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!

Most helpful comment

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

All 29 comments

java.lang.IllegalStateException: Required view 'bottomBar' with ID 2131558568 for field 'mBottomBar' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
at butterknife.internal.Utils.findRequiredView(Utils.java:138)
at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:150)
at com.wanguqu.app.ui.activity.MainActivity_ViewBinding.(MainActivity_ViewBinding.java:21)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)

                  We are using ButterKnife 8.4.0.     androidstudio2.2      jdk1.8        jack Builder

                             Any help is highly appreciated!

dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
applicationId rootProject.ext.android.applicationId
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName

     jackOptions {
        enabled true
    }

    compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

}
}

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

@JakeWharton yeah, happens to clean build as well.

@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

I have a failing test that reproduces this issue. Will send a PR as soon as I have a fix!

@felipecsl me too,i‘m still seeing this problem,this bug not fix

I have library projects and meet this issue, too.
Interestingly some of my library projects are ok, some are not. (By the way, I have already tried 8.4.1-SNAPSHOT and it is still not fixed for my situation.)
Comparing two of my libraries (one is ok and another one is not), I noticed some weird things that I would like to share with you! @JakeWharton

Suppose I have four library projects: Base library, News module, Lottery module, Movie module. News module depends on Base library. Lottery module and Movie module are with the same dependencies, they all depends on Base library and News module. Their gradle structures are as the following:

  • News module
compile('com.android.example:base-library:x.x.x@aar') {
    transitive = true
}
  • Lottery and Movie module
compile('com.android.example:base-library:x.x.x@aar') {
    transitive = true
    exclude group: 'com.android.example'
}
compile('com.android.example:news-module:x.x.x@aar') {
    transitive = true
    exclude group: 'com.android.example'
}

Now my lottery module will not have trouble but my movie module will meet this issue. Please check the following screenshots of the ViewBinding file generated by ButterKnife of the two modules and notice the difference.

wx20161226-032205 2x
wx20161226-032428 2x

I hope you can find something useful for fixing this issue. And I can help to provide more details about these four projects if you need so.

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.

I have only used two kinds of annotation in my application and library projects: @BindView and @OnClick
So maybe it will not help me... My temporary solution is to remove butterknife dependencies in some libraries within which butterknife generates wrong codes and to wait for future fix.
Thank you anyway! @7293hq

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!

@zhfk930129 hello, How to get 8.5.2-SNAPSHOT version?

@wow121 Hi! Below is the key structure of my build.gradle file in library module.

buildscript {
    repositories {
        ...
        jcenter()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies {
        ...
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.2-SNAPSHOT'
    }
}
...
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
...
repositories {
    ...
    jcenter()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
    ...
    compile 'com.jakewharton:butterknife:8.5.2-SNAPSHOT'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.2-SNAPSHOT'
}

You should also add the maven url into your application module. That's it. Cheers!

Halo

Where is the solution for this issue, i face the same problem, and dont know what to do to solve this problem

Thankyou

Hi are you using butterknife 8.6.0 now and still get the issue?

I had this issue with version 8.6.0 of Butter Knife but a gradle clean build has resolved the issue for me.

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 Please provide some update on this. My code is at production level. Please help. I have also tried with 8.5.1. It is throwing an exception in my child fragment toolbar imageview.

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

False alarm... Upgrading to 9.0.0-SNAPSHOT fixed it. All good 👍

Using 9.0.0 SNAPSHOT and still seeing this issue with trivial input fields.
Any clues?

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

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

I met the same problem in one of my project which use the 9.0.0-SNAPSHOT after upgrading the gradle plugin to 3.1.2 from 2.3.3, but the other one didn't met the issue. It takes me 4 hours to find out the bug, but nothing found out.,😫

I upgraded gradle to 4.4, gradle plugin to 3.1.2 several days ago, and now butterknife version is butterknife-gradle-plugin:9.0.0-snapshot, com.jakewharton:butterknife:8.8.1 and com.jakewharton:butterknife-compiler:8.8.1, and this problem occurs again. When I compare the wrong IDs in R.java files, I find these IDs do have the same integer values. As I don't know how butterknife works, I only use the 'findViewById' instead. Hope this problem will be solved later.
Thanks. By the way, this problem didn't occur when gradle version is 3.3, gradle plugin is 2.3.3 and butterknife version is 8.7.0 in my project.

I ran into this one because the resource was in the module, and the Activity was in the app module, and still referred to R2.

I moved the resource to the same place as the Activity, and removed R2 references.

Hope it helps.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{world.cl.cashback/world.cl.androidcl.MyAppActivity}: java.lang.IllegalStateException: Required view 'fill_horizontal' with ID 2131165260 for field 'containerView' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalStateException: Required view 'fill_horizontal' with ID 2131165260 for field 'containerView' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
        at butterknife.internal.Utils.findRequiredView(Utils.java:88)
        at world.cl.androidcl.MyAppActivity_ViewBinding.<init>(MyAppActivity_ViewBinding.java:23)
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at butterknife.ButterKnife.bind(ButterKnife.java:171)
        at butterknife.ButterKnife.bind(ButterKnife.java:100)
        at world.cl.androidcl.MyAppActivity.onCreate(MyAppActivity.kt:34)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Hello @JakeWharton !
I am running into this issue pretty often lately. I am using 9.0.0-rc2. Somehow it was not fixed after the first clean attempt. The second one has solved the problem for now.

I don't know if this is caused by the same issue I had, but I'll put my solution here in the hope it helps.

For me, the error was slightly different, but the result looked like this. My code was trying to call enable on a dimens resource.

This was caused because the R.java file and the R2.java file had two widgets that had the same numeric id. I'm guessing in your case, fill_horizontal (probably a dimens or an integer) has the same numeric ID as whatever containerView is trying to call.

For me, I had an app module and a coreui module.

I had changed the references in the code (ExampleActivity.java)in the coreui module to point to R2 (R2.id.exitButton and so on).

Then, because of the way my inheritance was set up, I later realised I needed to move the ExampleActivity.java back to the app module.

I had not changed the R2 references, nor had I moved the layouts that they referred to. They were still in the coreui module.

For me, it was a simple matter of changing R2.id.* in ExampleActivity.java back to R.id.*, clean my headers so no more mention of R2, and move the affected layouts back to app. (Not sure if moving the layouts really mattered, but in my case it made sense).

Again, might not be your problem, but worth a look in case it is.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ZakTaccardi picture ZakTaccardi  ·  3Comments

mturki picture mturki  ·  3Comments

momochenxx picture momochenxx  ·  3Comments

galibimtiaz picture galibimtiaz  ·  3Comments

engr-umar-qureshi picture engr-umar-qureshi  ·  3Comments