16

After re-running the app in Android Studio Butterknife.bind(this) does not find the views anymore. The only reliable solution I found so far is by Cleaning/Rebuilding Project and run it again. Then it finds the views again until the next re-run. This is incredibly annoying so far and takes a minimum of two minutes for a rebuild.

I have the following build.gradle

android {

compileSdkVersion 24
buildToolsVersion "25.0.0"
defaultConfig {
    applicationId "xx"
    minSdkVersion 21
    targetSdkVersion 24
    versionCode x
    versionName "xxx"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    jackOptions {
        enabled true
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    }
}
packagingOptions {
    exclude 'META-INF/NOTICE' 
    exclude 'META-INF/LICENSE' 
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    compile 'com.jakewharton:butterknife:8.4.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support:support-annotations:24.2.1'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:support-v13:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2'

}

I am also using build tools com.android.tools.build:gradle:2.2.2

With Butterknife.setDebug(true) I get the following:

D/ButterKnife: Looking up binding for xx.LoginFragment
D/ButterKnife: Not found. Trying superclass xx.BaseFragment
D/ButterKnife: Not found. Trying superclass android.app.Fragment
D/ButterKnife: MISS: Reached framework class. Abandoning search.

The BaseFragment does the binding and LoginFragment extends it. It looks like this

BaseFragment import android.app.Fragment;

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(getLayoutResourceId(), container, false);
        ButterKnife.setDebug(true);
        unbinder = ButterKnife.bind(this, v);
        initViews(v);
        return v;
    }

LoginFragment

@BindView(R.id.inputEmail)
    protected EditText inputEmail;

@Override
    protected void initViews(View v) {
        EditTextFocusListener focusListener = new EditTextFocusListener();
        inputEmail.setOnFocusChangeListener(focusListener);
    }

And the stacktrace

  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setOnFocusChangeListener(android.view.View$OnFocusChangeListener)' on a null object reference
            at xx.LoginFragment.initViews(LoginFragment.java:51)
            at xx.BaseFragment.onCreateView(BaseFragment.java:53)

As I said before the only solution which is reliable at the moment is to do a full clean/rebuild of the whole project. This exact structure worked fine before using the jackCompiler and I can not disable it anymore. Major part of the code depends on it.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • did you call unbinder.unbind(); into onDestroyView()? – Muhammad Waleed Nov 16 '16 at 13:47
  • @Javacoder Yes I do this for the `BaseFragment` – Murat Karagöz Nov 16 '16 at 13:48
  • @MuratK. what about `compile 'com.jakewharton:butterknife:7.0.1'` – IntelliJ Amiya Nov 26 '16 at 06:42
  • 1
    @IntelliJAmiya Did not try that, but I canged to the cannary channel and to `com.android.tools.build:gradle:2.3.0-alpha1` and it seems like its working so far.. – Murat Karagöz Nov 28 '16 at 08:40
  • @MuratK. glad to hear .Move ahead . FYI ,`-alpha1` is not stable – IntelliJ Amiya Nov 28 '16 at 08:43
  • 1
    @IntelliJAmiya Thanks for the heads up. I had hoped that someone would have an idea to solve this with the current version. – Murat Karagöz Nov 28 '16 at 08:48
  • 1
    @MuratK. you can post this as answer . #Temporary_solutions – IntelliJ Amiya Nov 28 '16 at 08:50
  • 1
    @IntelliJAmiya Yeah sure. – Murat Karagöz Nov 28 '16 at 09:04
  • @MuratK. what problem for old `com.android.tools.build:gradle:2.1.2` . – IntelliJ Amiya Nov 28 '16 at 09:07
  • 1
    @IntelliJAmiya That version is too low for the jackCompiler. I need at least v2.2.+. But this issue is getting quite popular on the [butterknife github issues](https://github.com/JakeWharton/butterknife/issues) – Murat Karagöz Nov 28 '16 at 09:13
  • @MuratK. indeed .I know .Then try using `2.3.0-alpha2` – IntelliJ Amiya Nov 28 '16 at 09:15
  • 1
    Using butterknife:7.0.1 solves nothing. I was using it when this issue started occurring in my app. I migrated my code to 8.4.0 and the issue was still there. I noticed that this issue was introduced when I switched from 'apt' to 'kapt' for dagger compiler, but butterknife still used 'apt' (or 'annotationProcessor'). After I switched from apt to kapt also for butterknife lib, all views are being injected as usual. No other action was needed (jackCompiler or AS update)PS: I am not posting this as an answer since OP doesn't use dagger and kotlin, but some folks might find my observation useful. – vanomart Dec 02 '16 at 07:05

4 Answers4

6

This is a temporary solution until someone has a better answer or it's released on the stable channel.

I changed to the canary channel and upgraded Android Studio to 2.3 Canary and com.android.tools.build:gradle:2.3.0-alpha1. Everything else stays the same as in the opening post.

EDIT: I ultimately decided to disable the jackCompiler and roll back to Java 1.7. It's stable now.

jackOptions {
        enabled false
    } 
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
4

The github page says to use annotationProcessor but the jakewharton.github.io page says to use apt.

Based on this SO post, it sounds like it's an issue with the instructions vs the version of the Android Gradle plugin that is used (although I'm using 2.2.2 and still saw the issue).

Update for October 2016: You probably don't need apt and the android-apt plugin anymore. Version 2.2 of the Android Gradle plugin has an annotationProcessor configuration that you should be using instead.

But you should try this

Try to change

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

with

apt 'com.jakewharton:butterknife-compiler:8.4.0'
Community
  • 1
  • 1
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • Thanks for the answer. I already tried to use the `apt` plugin and if I remember correctly you still need to apply the `android-apt` to make it work. But this does not go well with the jackCompiler as it complains. Seems more like an deeper issue between jack and butterknife. – Murat Karagöz Nov 29 '16 at 12:53
2

See here. Please, include this :

At Gradle:

compile 'com.jakewharton:butterknife:8.4.0'

annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' too.

This may help you.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
0

I think you missing in project gradle something like this:

classpath 'com.android.tools.build:gradle:2.2.2'
msamardzic
  • 1,060
  • 9
  • 12