Search code examples
androidbutterknife

How i can add ButterKnife Locally (No remote dependencies)


I have downloaded 'butterknife-8.8.1.aar' , 'butterknife-annotations-8.8.1.jar' and 'butterknife-compiler-8.8.1.jar' and added to the project. When i launch the app, i get NullPointer Exception for the view.

public class MainActivity extends AppCompatActivity {

@BindView(R.id.tv_welcome)
TextView textView;

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

    ButterKnife.bind(this);
    textView.setText("Butter knife worked!");
}
}

XML

<TextView
    android:id="@+id/tv_welcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Gradle file

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// ButterKnife
implementation files('libs/butterknife-annotations-8.8.1.jar')
implementation files('libs/butterknife-compiler-8.8.1.jar')
implementation project(':butterknife-8.8.1')}

Solution

  • For ButterKnife to work we need to add three modules:

    • butterknife-annotations
    • butterknife-reflect
    • butterknife-runtime

       implementation project(':butterknife-annotations')
       implementation project(':butterknife-reflect')
       implementation project(':butterknife-runtime')