Android Studio 2.x 和Android Studio3.x 版本有些依赖或写法还是有点不同的,在此记下。
也许你会遇到这个错误提示:
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-5.1.1.jar (com.jakewharton:butterknife:5.1.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.
这就是studio版本的问题导致的butterknife:5.1.1错误,这里已经解决。
Android Studio 2.x :
1.在Android Studio中右击Fele--Project Structure--app----Denpendencies---右上角的+号--选择Library dependency--搜索你需要的
2.贴出依赖:
implementation 'com.jakewharton:butterknife:5.1.1'
3.实例代码写法:
import butterknife.ButterKnife;
import butterknife.InjectView;
import me.liujia95.biliplayer.R;
import me.liujia95.biliplayer.activity.SearchActivity;
import me.liujia95.biliplayer.utils.UIUtils;
public class FaxianTopViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@InjectView(R.id.faxian_top_search)
TextView mSearch;
@InjectView(R.id.faxian_top_scan)
ImageView mScan;
public FaxianTopViewHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
initListener();
}
private void initListener() {
mSearch.setOnClickListener(this);
mScan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == mSearch) {
Intent intent = new Intent(UIUtils.getContext(), SearchActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UIUtils.getContext().startActivity(intent);
} else if (v == mScan) {
}
}
}
Android Studio 3.x :
1.先去大项目里面的build.gradle(最外层的)添加
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
2.在app的build.gradle里添加:
implementation 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
3.实例代码写法:
import butterknife.BindView;
import butterknife.ButterKnife;
import me.liujia95.biliplayer.R;
import me.liujia95.biliplayer.activity.SearchActivity;
import me.liujia95.biliplayer.utils.UIUtils;
public class FaxianTopViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.faxian_top_search)
TextView mSearch;
@BindView(R.id.faxian_top_scan)
ImageView mScan;
public FaxianTopViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
initListener();
}
private void initListener() {
mSearch.setOnClickListener(this);
mScan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == mSearch) {
Intent intent = new Intent(UIUtils.getContext(), SearchActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UIUtils.getContext().startActivity(intent);
} else if (v == mScan) {
}
}
}