ButterKnife 8.0.1 not working
Asked Answered
S

11

59

I am using butterknife 8.0.1, but a nullpointerexception is appearing.

This line is on my build.grade file: compile 'com.jakewharton:butterknife:8.0.1'

this is my Main Class: (I wrote the includes properly)

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.MainScreenTextView) TextView mainText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        **mainText.setText("Butter knife is working fine");**
    }

and this is MainActivity.xml:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <TextView
        android:id="@+id/MainScreenTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is the Main Screen"
        android:textColor="#000000"
        android:background="#666666"
        android:padding="5dp"
        android:textSize="20dp"/>
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

Subirrigate answered 3/5, 2016 at 20:19 Comment(0)
R
150

Per the readme, you need to include the butterknife-compiler in order for the generated code to be produced automatically:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

Without this there is no generated code to be loaded and thus none of the fields get set.

You can verify ButterKnife is working by calling ButterKnife.setDebug(true) and looking in Logcat.

Remmer answered 3/5, 2016 at 20:35 Comment(12)
Worked well for me also. This should be the accepted answer.Adolphadolphe
I followed the same readme conditions but for mine its not working. its giving me this Error:Gradle: Execution failed for task ':app:compileWholesaleDebugJava'. > java.lang.IllegalArgumentException: couldn't make a guess for com.sme.Activity.fragment.AccountStatementFragmentInterstice
Use lowercase package names. Otherwise you'll need to wait for 8.1.0 where that bug is fixed.Remmer
Hi @JakeWharton When i add apply plugin: 'com.neenbedankt.android-apt' send me and errorLahomalahore
why does the author make it so complex?Roslyn
Thx, Jake. Add this info to your mainpage "download". Many people will be bless you for this.Roentgenotherapy
@KaiWang because to reduce Reflection in code so to increase execution speed by 30%Nucleotide
@Lahomalahore you need to add classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' to build.grade file of your project not your AppNucleotide
Still getting NPAStevens
Plugin with id 'com.android.application' not found.Pfeiffer
Just these two dependencies are required compile'com.jakewharton:butterknife:8.0.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.0.1'Dukie
gradle please should also include annotation processor dependency .I messed up just cause it gave me single dependency.Nucleotide
O
7

I used this library in Fragment and has NPE. My code was:

ButterKnife.bind(view);

But it was wrong. Library need to know two objects:
1) Target - with annotations @BindView
2) Source - with views

It will be right to write:

ButterKnife.bind(this, view);

When this - your fragment, and view - view of this fragment.

Otherwhere answered 6/10, 2016 at 19:18 Comment(1)
What if it is an activity?Horvitz
D
6

for me the problem was that I was using

annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'

instead of

 apt 'com.jakewharton:butterknife-compiler:8.7.0
Domett answered 28/7, 2017 at 8:18 Comment(1)
what's the difference?Marisamariscal
N
5
App Level(build.gradle)

apply plugin: 'android-apt'
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}


Project Level(build.gradle)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {

        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
Neslund answered 3/10, 2016 at 5:17 Comment(0)
J
3

Config Butterknife on build.gradle file like this,

compile("com.jakewharton:butterknife:8.5.1")
annotationProcessor "com.jakewharton:butterknife-compiler:8.5.1"

It works for me.

Javelin answered 19/3, 2018 at 9:16 Comment(0)
R
3

If you use kotlin:

make sure to use this dependency in module app:

dependencies {
   implementation 'com.jakewharton:butterknife:10.0.0'
   kapt 'com.jakewharton:butterknife-compiler:10.0.0'
}
Recreant answered 11/5, 2019 at 15:55 Comment(1)
This was my issue. To add to this, even if you have zero kotlin code in your project but have used and then deleted Kotlin snippets, you will still run into an error unless the kapt line above is included.Dogger
S
2

I had this issue too, just because I've added butterknife from Android Studio's Dependency management and not by copy-pasting gradle lines from Butterknife website. So I had to add compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' instead of just compile 'com.jakewharton:butterknife:8.5.1'

Sero answered 4/4, 2017 at 5:5 Comment(0)
B
1

So for an update to a previous answer. If you are using annotationProcessor like this

annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'

change it up to this:

kapt 'com.jakewharton:butterknife-compiler:10.2.1'

it's kapt instead of apt as of 2023.

Branca answered 11/1, 2023 at 21:22 Comment(0)
V
0

From JakeWharton

Yes that plugin is no longer needed. You're already using annotationProcessor for the 'butterknife-compiler' artifact which is built-in to the Android Gradle plugin.

Then the solution is delete apt classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

Vice answered 27/2, 2017 at 8:13 Comment(0)
C
0

Add annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' In your gradle file it worked for me

Cheng answered 25/5, 2017 at 10:16 Comment(0)
C
0

I have just faced this problem, after updating my project to Gradle version 3.0.1. I was able to fix it by just including in Gradle app file the line:

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

The final result was:

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })

        ...

        compile 'com.jakewharton:butterknife:8.8.1'
        annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    }

I hope this helps somebody, although this question being old.

Chigoe answered 14/12, 2017 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.