Android Studio: Integrating Butterknife?
Asked Answered
V

7

14

I am trying to implement Butterknife into my android studio project.

However when I do so I get an error on @InjectView "cannot resolve symbol InjectView".

Have I not implemented Butterknife sucsessfully?

Activity code:

package com.example.rodf.testapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @InjectView(R.id.tvHelloWorld) TextView tv1;

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



    }
}

layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvHelloWorld"
        android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



</RelativeLayout>

gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.rodf.testapp"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    //adding the butter knife library
    compile 'com.jakewharton:butterknife:6.0.0'
}
Varela answered 25/1, 2015 at 16:49 Comment(1)
Have you synced your gradle file?Trooper
S
12

I think your code is good,

  1. Try to sync your gradle by click enter image description here

  2. Try to go File -- invalidate Caches and restart your Android studio.

Also, don't forget put ButterKnife.inject(this); in onCreate()

Stenography answered 25/1, 2015 at 16:55 Comment(0)
M
20

Note that in the latest versions of the ButterKnife library, the @InjectView() annotation is no longer used.

In stead @Bind(R.id.tvHelloWorld) and ButterKnife.bind(this); are used.

Reference: http://jakewharton.github.io/butterknife/

Moncton answered 16/8, 2015 at 20:23 Comment(0)
S
12

I think your code is good,

  1. Try to sync your gradle by click enter image description here

  2. Try to go File -- invalidate Caches and restart your Android studio.

Also, don't forget put ButterKnife.inject(this); in onCreate()

Stenography answered 25/1, 2015 at 16:55 Comment(0)
T
11

So ButterKnife just got updated with version 8.5.1

To use that,

Add below line inside project-level build.gradle:

classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'

Add below lines inside app-level build.gradle:

// Field and method binding for Android views
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

Apply butterknife plugin as below:

apply plugin: 'com.jakewharton.butterknife'

A little change in syntax to previous ButterKnife version is that, now you have to use R2 instead of R while using ButterKnfe annotations.

To be specific:

Instead of writing

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

We will be writing

@BindView(R2.id.textView)
TextView mTextView;

and then simply build the project.

Turfman answered 21/2, 2017 at 13:59 Comment(2)
This is a huge change. It's unfortunate that it's not documented or reflected at jakewharton.github.io/butterknife, and the documentation at github.com/JakeWharton/butterknife only partially reflects the change -- the early code samples on the page use R, and the later ones use R2. It's also unfortunate that you can upgrade to 8.5.1, not change any of your R references to R2, and it still compiles and runs -- what kind of mysterious bugs have I got now? This is a mess.Rato
Life saver, I think. I was trying to debug for a half an hour, and turns out, the version code is the only cause of this. FacepalmYeseniayeshiva
D
9

Set up manual configuration for ButterKnife from this link

File -> Other Settings -> Default Settings

Compiler -> Annotation Processors -> Check Enable annotation processing

Dietz answered 25/1, 2015 at 17:7 Comment(2)
Before year this answer was perfect, but now Android studio does not have those settingsAileenailene
Yes it does, but the location changed: File -> Other Settings -> Default Settings -> Build, Execution, Deployment -> Compiler* -> Annotations Processors* -> Enable annotation processing Note that the ones with * are under a sub menu that you manually have to show. Also you can simply search on the Search field for: "Enable annotation" and it will show up.Glassworks
C
2

Move the TextView tv1 declaration inside your class. Also call the ButterKnife.inject(this); method.

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends ActionBarActivity {

    @InjectView(R.id.tvHelloWorld) TextView tv1;

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

}
Copestone answered 25/1, 2015 at 18:59 Comment(0)
K
1

I tried all the answer's but nothing is working for me because in the library they changed something. Insteded of @InjectView try @Bind. It will work fine

Knutson answered 21/1, 2016 at 15:2 Comment(0)
H
1

In the latest versions of the ButterKnife library(8.5.1), the @InjectView() annotation is not used.

You can use @BindView(Component) instead of @InjectView(Component) and instead of using Butterknife.inject(this) use ButterKnife.bind(this)

Hiphuggers answered 17/3, 2017 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.