butterknife Annotation processors must be explicitly declared now...
butterknife相信大家都很熟悉。
很多工程都用到了Butter Knife这个框架,能节省很多代码量。像findViewById
这种代码就不用再出现了,而且这个框架也提供了很多其他有用的注解。
Butter Knife 是 Android 视图字段和方法绑定,使用注解处理来生成样板代码。
主要特性:
- 在字段使用 @FindView消除findViewById调用
- 使用 @FindViews在一个列表中分组多个视图
- 通过注解方法为监听者消除匿名 inner-classes
下载最新的jar,或者是通过 Gradle引用butterknife:
<dependency>
<groupId>com.jakewharton</groupId>
<artifactId>butterknife</artifactId>
<version>6.1.0</version>
</dependency>
Gradle:
compile 'com.jakewharton:butterknife:6.1.0'
不同的版本,有不同的使用方法,更新很快.另外注意,这个库的版本更新挺快的,我第一次用到的时候是7.1.0,而现在的最新版本已经是8.5.1了,也就是说大家可能需要去ButterKnife的Github查看最近的版本。
对于版本问题,最近看了一些开源项目,发现有的依赖版本用’+’代替,在这里建议写死,在我们自己以后的项目当中
dependencies{
compile’com.google.code.gson2.+’
}
这样的写法可以保证库每次都是最新的,但也带来了不少的问题:
每次build时会向网络进行检查,国内访问仓库速度很慢
库更新后可能会更改内部逻辑而带来bug,动态版本无法通过git的diff来规避此问题
每个开发者都可能会得到不同的最新版本,带来潜在隐患 推荐写成固定的库版本:
dependencies{
compile’com.google.code.gson2.2.1’
}
某一天,我将compile ‘com.android.support23.0.1改为compile ‘com.android.support
23.0.2,然后就出错了,原因是根本没有23.0.2这个版本。
那我们怎么知道到底有哪些版本呢?看这里Support Library Revision History
那要是改成24.0.0会怎么样?22.0.0又会怎么样?前提:compileSdkVersion 23
24.0.0 报错,但是能run,app在模拟器上可以运行
22.0.0 报错,但是能run,app在模拟器上可以运行
通常,是新建一个项目,放在本地作为一个依赖,依赖我们修改其他项目中的版本,尽量减少下载和编译的时间.翻墙真的不方便.
使用
Butter Knife,专门为Android View设计的绑定注解,专业解决各种findViewById。
对一个成员变量使用@BindView注解,并传入一个View ID, ButterKnife 就能够帮你找到对应的View,并自动的进行转换(将View转换为特定的子类):
class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields…
}
}
与缓慢的反射相比,Butter Knife使用再编译时生成的代码来执行View的查找,因此不必担心注解的性能问题。调用bind来生成这些代码,你可以查看或调试这些代码。
例如上面的例子,生成的代码大致如下所示:
public void bind(ExampleActivity activity) {
activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578);
activity.footer = (android.widget.TextView) activity.findViewById(2130968579);
activity.title = (android.widget.TextView) activity.findViewById(2130968577);
}
资源绑定
绑定资源到类成员上可以使用@BindBool、@BindColor、@BindDimen、@BindDrawable、@BindInt、@BindString。使用时对应的注解需要传入对应的id资源,例如@BindString你需要传入R.string.id\_string的字符串的资源id。
class ExampleActivity extends Activity {
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red; // int or ColorStateList field
@BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field
// …
}
在非Activity中使用绑定
Butter Knife提供了bind的几个重载,只要传入跟布局,便可以在任何对象中使用注解绑定。
例如在Fragment中:
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(R.id.button2) Button button2;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) \{
View view = inflater.inflate(R.layout.fancy\_fragment, container, false);
ButterKnife.bind(this, view);
// TODO Use fields...
return view;
\}
}
还有一种比较常见的场景,就是在ListView的Adapter中,我们常常会使用ViewHolder:
public class MyAdapter extends BaseAdapter {
@Override public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
view = inflater.inflate(R.layout.whatever, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
}
holder.name.setText(“John Doe”);
// etc…
return view;
}
static class ViewHolder {
@BindView(R.id.title)
TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
你能在提供给的例子中找到上述实现。
ButterKnife.bind的调用可以被放在任何你想调用findViewById的地方。
提供的其他绑定API:
使用Activity作为跟布局在任意对象中进行绑定。如果你使用了类似MVC的编程模式,你可以对controller使用它的Activity用ButterKnife.bind(this, activity)进行绑定。
使用ButterKnife.bind(this)绑定一个布局的子布局。如果你在布局中使用了<merge>标签并且在自定义的控件构造时inflate这个布局,你可以在inflate之后立即调用它。或者,你可以在onFinishInflate()回调中使用它。
View 列表
你可以一次性将多个views绑定到一个List或数组中:
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List
apply函数,该函数一次性在列表中的所有View上执行一个动作:
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);
Action和Setter接口能够让你指定一些简单的动作:
static final ButterKnife.Action
@Override public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter
@Override public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
};
Android中的Property属性也可以使用apply方法进行设置:
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
监听器绑定
使用本框架,监听器能够自动的绑定到特定的执行方法上:
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server…
}
而监听器方法的参数都时可选的:
@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server…
}
指定一个特定的类型,Butter Knife也能识别:
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText(“Hello!”);
}
可以指定多个View ID到一个方法上,这样,这个方法就成为了这些View的共同事件处理。
@OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void pickDoor(DoorView door) {
if (door.hasPrizeBehind()) {
Toast.makeText(this, “You win!”, LENGTH_SHORT).show();
} else {
Toast.makeText(this, “Try again”, LENGTH_SHORT).show();
}
}
自定义View时,绑定事件监听不需要指定ID
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
重置绑定:
Fragment的生命周期与Activity不同。在Fragment中,如果你在onCreateView中使用绑定,那么你需要在onDestroyView中设置所有view为null。为此,ButterKnife返回一个Unbinder实例以便于你进行这项处理。在合适的生命周期回调中调用unbind函数就可完成重置。
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(R.id.button2) Button button2;
private Unbinder unbinder;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
unbinder = ButterKnife.bind(this, view);
// TODO Use fields…
return view;
}
@Override public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
可选绑定:
在默认情况下, @bind和监听器的绑定都是必须的,如果目标view没有找到的话,Butter Knife将会抛出个异常。
如果你并不想使用这样的默认行为而是想创建一个可选的绑定,那么你只需要在变量上使用@Nullable注解或在函数上使用@Option注解。
注意:任何名为@Nullable的注解都可以使用在变量上。但还时强烈建议使用Android注解库中的@Nullable。使用这个库对你的代码有很多好处,关于该库的详情,可以点击此处:Android Tools Project
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// TODO …
}
对于包含多个方法的监听
当一个监听器包含多个回调函数时,使用函数的注解能够对其中任何一个函数进行绑定。每一个注解都会绑定到一个默认的回调。你也可以使用callback参数来指定一个其他函数作为回调。
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO …
}
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO …
}
Butter Knife提供了一个findViewById的简化代码:findById,用这个方法可以在View、Activity和Dialog中找到想要View,而且,该方法使用的泛型来对返回值进行转换,也就是说,你可以省去findViewById前面的强制转换了。
View view = LayoutInflater.from(context).inflate(R.layout.thing, null);
TextView firstName = ButterKnife.findById(view, R.id.first\_name);
TextView lastName = ButterKnife.findById(view, R.id.last\_name);
ImageView photo = ButterKnife.findById(view, R.id.photo);
如果你只是使用这个方法,可以使用静态引入ButterKnife.findById方法。
下载
dependencies \{
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
\}
文章标题遇到的错误Annotation processors must be explicitly declared now…
Error:Execution failed for task ‘javaPreCompileDebug’.
Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration.
- butterknife-7.0.1.jar (com.jakewharton:butterknife:7.0.1)
Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior. Note that this option is deprecated and will be removed in the future.
See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.
方法一:
apply plugin:’com.android.application’ ….
在defaultConfig {}方法后面加上下面这个方法
//添加了javaCompileOptions
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath = true
}
}
不过根据上面提示 此方式此后可能会失效.
方法二:(推荐)
依赖修改为如下:
provided 'com.jakewharton:butterknife:7.0.1'
annotationProcessor 'com.jakewharton:butterknife:7.0.1'
我是用的第一种方法,第二种在开发中修改还是会出现这个问题,如果大家有更好的办法,欢迎留言私信指正.
书到用时方恨少,纸上得来终觉浅。希望对您有所帮助。
参考:
https://stackoverflow.com/questions/44084251/android-studio-3-0-canary-1-annotation-processor-error
还没有评论,来说两句吧...