butterknife icon indicating copy to clipboard operation
butterknife copied to clipboard

Missing resource ID for OnClick annotation

Open cooperkong opened this issue 5 years ago • 8 comments

#974 perhaps related to that, it is able to generate correct _ViewBinding code for views but somehow not for OnClick functions.

View view;
    target.toolbar = Utils.findRequiredViewAsType(source, R.id.toolbar, "field 'toolbar'", Toolbar.class);
    view = Utils.findRequiredView(source, com.mypackage.R.id.information_about, "field 'info' and method 'onClickAbout'");
    target.info = view;
    view7f0b006e = view;
    view.setOnClickListener(new DebouncingOnClickListener() {
      @Override
      public void doClick(View p0) {
        target.onClickAbout();
      }
    });
    view = Utils.findRequiredView(source, 2131427440, "method 'onClickTermsAndCondition'");
    view7f0b0070 = view;
    view.setOnClickListener(new DebouncingOnClickListener() {
      @Override
      public void doClick(View p0) {
        target.onClickTermsAndCondition();
      }
    });

It looks like it's still use an int reference rather than actual R reference for OnClick I'm using latest BK and kotlin 1.3.30

Thanks.

cooperkong avatar Apr 15 '19 01:04 cooperkong

That's the default behavior. What's the problem? Please provide a sample which demonstrates the behavior.

JakeWharton avatar Apr 15 '19 02:04 JakeWharton

Hi, here is the repo to reproduce this issue https://github.com/cooperkong/butterknifeR2issue It crashes with ResourceNotFound if launch Activity2 which is in a seperate module. however if I uncomment @BindView in Activity2 it works.

class Activity2: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.lib2_layout)
        ButterKnife.bind(this)
    }
//    @BindView(R2.id.textview2)
//    lateinit var textView: TextView

    @OnClick(R2.id.textview2)
    fun donothing() {

    }
}

Thanks.

cooperkong avatar Apr 15 '19 03:04 cooperkong

i have the same issue

markchristopherng avatar Apr 16 '19 04:04 markchristopherng

@JakeWharton I have uploaded the sample, can you please have a look see if it can be reproduced?

cooperkong avatar Apr 16 '19 23:04 cooperkong

When I find time I will

On Tue, Apr 16, 2019, 7:26 PM WenChao Kong [email protected] wrote:

@JakeWharton https://github.com/JakeWharton I have uploaded the sample, can you please have a look see if it can be reproduced?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/JakeWharton/butterknife/issues/1498#issuecomment-483879937, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEERylgi6f9KUVvKg7AZBrpqPgPMM1ks5vhlw0gaJpZM4cu9WE .

JakeWharton avatar Apr 16 '19 23:04 JakeWharton

I solved this problem these days and submitted a pull request. Next, I will analyze the reasons and solutions. Please take a look @JakeWharton

Reason: When using kotlin, for the same resource, if you do not use other annotations (eg: @bindview) before the event annotation (eg: @onclick), after generating view_binding.java, the Utils.findRequiredView() uses a raw integer not R reference.

When the merged resource is packaged, the static value of the R file has changed. So it leads "Missing resource ID".

Analysis: The annotation processing of kotlin generates a stub java file and processes the annotation according to the java file.But at this time ,Java file already using raw integer

My solution is to solve in the process() method, if it is the first time a raw integer, go through the R.java, find the corresponding static value in R.java, and use R reference replace it. If the second time is a raw integer, it will not be processed because the same resource will use the same Id(butterknife.compiler.Id.java).

JantHsueh avatar Jul 27 '19 16:07 JantHsueh

@JantHsueh was your PR merged? Where can we see it? Or in which version the solution is available?

difereto-globant avatar Oct 17 '19 22:10 difereto-globant

I have the same issue... Is there a solution besides add @BindView for the all @OnClick resource.

shicaiD avatar Jun 22 '20 07:06 shicaiD