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.
BaseFragment
– Aerophagiacompile 'com.jakewharton:butterknife:7.0.1'
– Fluorescentcom.android.tools.build:gradle:2.3.0-alpha1
and it seems like its working so far.. – Aerophagia-alpha1
is not stable – Fluorescentcom.android.tools.build:gradle:2.1.2
. – Fluorescent2.3.0-alpha2
– Fluorescent