README.md 2.9 KB
Newer Older
J
Jake Wharton 已提交
1 2 3
Butter Knife
============

J
Jake Wharton 已提交
4 5
![Logo](website/static/logo.png)

6 7
Field and method binding for Android views which uses annotation processing to generate boilerplate
code for you.
8

9
 * Eliminate `findViewById` calls by using `@BindView` on fields.
10
 * Group multiple views in a list or array. Operate on all of them at once with actions,
11 12
   setters, or properties.
 * Eliminate anonymous inner-classes for listeners by annotating methods with `@OnClick` and others.
13
 * Eliminate resource lookups by using resource annotations on fields.
J
Jake Wharton 已提交
14 15 16

```java
class ExampleActivity extends Activity {
17 18
  @BindView(R.id.user) EditText username;
  @BindView(R.id.pass) EditText password;
19

20
  @BindString(R.string.login_error) String loginErrorMessage;
J
Jake Wharton 已提交
21 22 23 24

  @OnClick(R.id.submit) void submit() {
    // TODO call server...
  }
J
Jake Wharton 已提交
25 26 27 28

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
29 30
    ButterKnife.bind(this);
    // TODO Use fields...
J
Jake Wharton 已提交
31 32 33 34
  }
}
```

J
Jake Wharton 已提交
35
For documentation and additional information see [the website][3].
J
Jake Wharton 已提交
36

J
Jake Wharton 已提交
37
__Remember: A butter knife is like [a dagger][1] only infinitely less sharp.__
J
Jake Wharton 已提交
38 39 40 41 42 43



Download
--------

R
Ronald 已提交
44
```groovy
45
dependencies {
J
Jake Wharton 已提交
46 47
  implementation 'com.jakewharton:butterknife:10.1.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
48
}
J
Jake Wharton 已提交
49
```
J
Jake Wharton 已提交
50

J
Jake Wharton 已提交
51 52
If you are using Kotlin, replace `annotationProcessor` with `kapt`.

53 54
Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap].

J
Jake Wharton 已提交
55

56 57 58 59 60 61 62 63

Library projects
--------------------

To use Butter Knife in a library, add the plugin to your `buildscript`:

```groovy
buildscript {
64 65
  repositories {
    mavenCentral()
S
Stéphane Nicolas 已提交
66
    google()
67
   }
68
  dependencies {
J
Jake Wharton 已提交
69
    classpath 'com.jakewharton:butterknife-gradle-plugin:10.1.0'
70 71 72 73 74 75 76 77 78 79 80
  }
}
```

and then apply it in your module:

```groovy
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
```

81
Now make sure you use `R2` instead of `R` inside all Butter Knife annotations.
82 83 84 85 86 87 88 89 90 91 92

```java
class ExampleActivity extends Activity {
  @BindView(R2.id.user) EditText username;
  @BindView(R2.id.pass) EditText password;
...
}
```



J
Jake Wharton 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
License
-------

    Copyright 2013 Jake Wharton

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.



 [1]: http://square.github.com/dagger/
113
 [2]: https://search.maven.org/remote_content?g=com.jakewharton&a=butterknife&v=LATEST
J
Jake Wharton 已提交
114
 [3]: http://jakewharton.github.com/butterknife/
115
 [snap]: https://oss.sonatype.org/content/repositories/snapshots/