Configuring ButterKnife in your project

suggest change

Configure your project-level build.gradle to include the android-apt plugin:

buildscript {
   repositories {
      mavenCentral()
   }

   dependencies {
      classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
   }
}

Then, apply the android-apt plugin in your module-level build.gradle and add the ButterKnife dependencies:

apply plugin: 'android-apt'

android {
    ...
}

dependencies {
      compile 'com.jakewharton:butterknife:8.5.1'
      annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
}

Note: If you are using the new Jack compiler with version 2.2.0 or newer you do not need the android-apt plugin and can instead replace apt with annotationProcessor when declaring the compiler dependency.

In order to use ButterKnife annotations you shouldn’t forget about binding them in onCreate() of your Activities or onCreateView() of your Fragments:

class ExampleActivity extends Activity {

@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Binding annotations
    ButterKnife.bind(this);
    // ...
}

}

// Or
class ExampleFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(getContentView(), container, false);
    // Binding annotations
    ButterKnife.bind(this, view);
    // ...
    return view;
}

}

Snapshots of the development version are available in Sonatype’s snapshots repository.


Below are the additional steps you’d have to take to use ButterKnife in a library project

To use ButterKnife in a library project, add the plugin to your project-level build.gradle:

buildscript {
    dependencies {
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
    }
}

…and then apply to your module by adding these lines on the top of your library-level build.gradle:

apply plugin: 'com.android.library'
// ...
apply plugin: 'com.jakewharton.butterknife'

Now make sure you use R2 instead of R inside all ButterKnife annotations.

class ExampleActivity extends Activity {

    // Bind xml resource to their View 
    @BindView(R2.id.user) EditText username;
    @BindView(R2.id.pass) EditText password;

    // Binding resources from drawable,strings,dimens,colors
    @BindString(R.string.choose) String choose;
    @BindDrawable(R.drawable.send) Drawable send;
    @BindColor(R.color.cyan) int cyan;
    @BindDimen(R.dimen.margin) Float generalMargin;

    // Listeners
    @OnClick(R.id.submit)
    public void submit(View view) {
    // TODO submit data to server...
    }

    // bind with butterknife in onCreate
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        // TODO continue
  }

}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


ButterKnife:
* Configuring ButterKnife in your project

Table Of Contents
2 Gradle
5 Intent
17 Service
19 WebView
31 SQLite
33 ButterKnife
35 Glide
37 Dialog
38 ACRA
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
95 OkHttp
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
261 Fastjson
263 Jackson
267 Smartcard