butterknife: Missing resource ID

I’m running:

  • Android Studio 2.3.3
  • Kotlin 1.1.2-3
  • Butterknife 8.6.0

My project has an app module an a library module, in the app module I’ve included:

dependencies {
    ...
    compile "com.jakewharton:butterknife:$butterknife_version"
    kapt "com.jakewharton:butterknife-compiler:$butterknife_version"
}

In the library module I have:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.jakewharton:butterknife-gradle-plugin:$butterknife_version"
    }
}

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
    ...
}
dependencies {
    ...
    compile "com.jakewharton:butterknife:$butterknife_version"
    kapt "com.jakewharton:butterknife-compiler:$butterknife_version"
}

My library code includes a custom view called CameraControlView with the following:

@OnClick(R2.id.switch_camera)
fun switchCamera() {
}

The generated CameraControlView_ViewBinding.java has the following offending line:

view = Utils.findRequiredView(source, 2131493059, "method 'switchCamera'");

R2.id.switch_camera is 0x7f0c00c3 (2131493059) R.id.switch_camera is 0x7f0c00c3 in library R.id.switch_camera is 0x7f100186 in app

Am I configuring butterknife wrong for the library or is this a bug?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 5
  • Comments: 39 (10 by maintainers)

Most upvoted comments

the problem is still here, not fixed, fuck

@newtalentxp 大哥!你是键盘侠吗?不要公开丢中国人脸。

OnClick annotation is not working for me in library module. Is that the same problem as described here or should I create a separate issue?

ButterKnife: 9.0.0-SNAPSHOT Kotlin: 1.2.40 AGP: 3.1.2

For the following class

class MyFragment : BaseFragment() {

    @BindView(R2.id.textview_upsell_confirmation_title) lateinit var headlineTextView: TextView
    @BindView(R2.id.textview_upsell_confirmation_text) lateinit var confirmationTextView: TextView

    @OnClick(R2.id.button_premium_upsell_confirmation)
    internal fun upsellConfirmationClicked() {
    }
}

ButterKnife generates

  @UiThread
  public MyFragment_ViewBinding(
      final MyFragment target, View source) {
    this.target = target;

    View view;
    target.headlineTextView = Utils.findRequiredViewAsType(source, R.id.textview_upsell_confirmation_title, "field 'headlineTextView'", TextView.class);
    target.confirmationTextView = Utils.findRequiredViewAsType(source, R.id.textview_upsell_confirmation_text, "field 'confirmationTextView'", TextView.class);
    view = Utils.findRequiredView(source, 2131493117, "method 'upsellConfirmationClicked$core_app_debug'");
    view7f0c00fd = view;
    view.setOnClickListener(new DebouncingOnClickListener() {
      @Override
      public void doClick(View p0) {
      }
    });
  }

At runtime I’m receiving Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID

I noticed, that for @BindView generated code contains reference to R.id. But for @OnClick generated code contains hardcoded constant.

UPDATE: I also noticed that sometimes it happens like that, but sometimes binding for OnCLick is generated with reference to R.id.

@newtalentxp wowow easy man

I have just tested Kotlin 1.3.20 (stable) with Butterknife 9.0.0 on library modules, and everything seems to be working fine! 👍

It’s fixed in 1.3.20

On Fri, Nov 16, 2018, 10:30 AM Daniel Frett <notifications@github.com wrote:

It appears this issue was re-introduced with Kotlin 1.3.10.

R2 usage in a Kotlin class works correct with the following dependency versions: ButterKnife: 9.0.0-rc1 Kotlin: 1.2.71

Upgrading Kotlin to 1.3.10 causes the R2 resource to be replaced with a raw int and not the correct R id in the generated ViewBinding class

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/JakeWharton/butterknife/issues/974#issuecomment-439429498, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEWSydeSaMxnIzqhgsTJhZELw8OOwks5uvtoagaJpZM4N2Lt3 .

the problem is still here, not fixed, fuck

Do you mean that R2 should contain something like switch_camera = com.example.R.id.switch_camera?

The generated R2 contains:

    @IdRes
    public static final int switch_camera = 0x7f0c00c3;

Library R contains:

        public static int switch_camera=0x7f0c00c3;

App R contains:

        public static final int switch_camera=0x7f100186;