1

I added Butter knife library to Gradle like this:

dependencies {
   compile 'com.jakewharton:butterknife:8.0.1'
   ...
}

Created a Button with id btnPress. In my Activity, when i tried to add method with @onClick(R.id.btnPress), on running the application, the method does not execute.

Activity:

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btnPress)
    Button btnPress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        ButterKnife.bind(MainActivity.this);
    }

    //This method is not being called when Button is pressed.
    @OnClick(R.id.btnPress)
    void onPress() {
        ...
    }
}
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
  • are you calling `setContentView` before calling `ButterKnife.Bind` ? and make sure about your layout and views id. – Mohammad Rahchamani Jun 13 '16 at 04:47
  • @MohammadRahchamani Yes, i am setting View before binding. I have checked layout ids, they are same. – Malwinder Singh Jun 13 '16 at 04:48
  • Did you add `apt 'com.jakewharton:butterknife-compiler:8.0.1'` in your module-level `build.gradle` and `classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'` in your project-level `build.gradle`? – Dennis Lu Jun 13 '16 at 04:48
  • @Malwinder your issue is just with listeners? I mean is your `btnPress` correctly initialized or it's just `null` ? – Mohammad Rahchamani Jun 13 '16 at 04:50
  • @L.Meng I am receiving error: Gradle DSL method not found: 'apt()' – Malwinder Singh Jun 13 '16 at 04:56
  • @Malwinder Look at this [gradle-dsl-method-not-found-apt](http://stackoverflow.com/questions/36876158/gradle-dsl-method-not-found-apt?answertab=votes#tab-top) – Dennis Lu Jun 13 '16 at 05:01

1 Answers1

1

Here's how i resolved this issue:

First, in your top-level build.gradle file, include:

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

in the buildscript dependencies, such as:

  buildscript {
    dependencies {
      classpath 'com.android.tools.build:gradle:2.0.0'
      classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
  }

Then, in your module's build.gradle file, include apply plugin: 'com.neenbedankt.android-apt' towards the top.

Now include ButterKnife library and compiler in module level build.gradle:

dependencies {
    compile 'com.jakewharton:butterknife:8.0.1'
    apt 'com.jakewharton:butterknife-compiler:8.0.1'
    ...
}
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103