在app的build.gradle引用butterknife:
implementation 'com.jakewharton:butterknife:9.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0'
buildscript {
repositories {
...
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0'
}
}
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
...
dependencies {
...
implementation 'com.jakewharton:butterknife:9.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0'
}
@BindView(R.id.tv_test1)
TextView tvTest1;
@BindViews({R.id.tv_test2, R.id.tv_test2})
public List<TextView> textList;
@BindString(R.string.base_bind_string)
String str;
@BindBitmap(R.drawable.ic_launcher)
public Bitmap mBitmap ;
@BindColor(R.color.base_bind_color)
int color;
@OnClick(R2.id.btn_test1)
public void showToast(){
...
}
@OnLongClick(R2.id.btn_test1)
public boolean showToastl(){
...
return true;
}
public class BaseActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
ButterKnife.bind(this);
}
}
在activity使用注意事项: 1.activity中调用ButterKnife.bind(this)必须在setContentView之后,且不需要做解绑操作 2.使用ButterKnife修饰的元素(例如@BindView修饰)访问控制权限不能是static或者private
public class BaseFragment extends Fragment {
private Unbinder unbinder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View contentView = onCreateViewEx(inflater, container, savedInstanceState);
unbinder = ButterKnife.bind(this, contentView);
return contentView;
}
/**
* onDestroyView中进行解绑操作
*/
@Override
public void onDestroyView() {
super.onDestroyView();
if(unbinder != null) {
unbinder.unbind();
unbinder = null;
}
}
}
在Fragment使用注意事项: 1.在fragment中使用ButterKnife.bind(this,view)绑定,this不能替换成getActivity() 2.在fragment中使用ButterKnife需要进行解绑操作,一般在onDestroyView调用unbind()方法
public static class ViewHolder extends RecyclerView.ViewHolder{
@BindView(R2.id.tv_item)
public TextView mTextView;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
错误: @BindViews annotation contains duplicate ID 2131165346. (com.cold.butterknifedemo.BaseActivity.textList)
错误: @OnLongClick methods must have a 'boolean' return type. (com.cold.butterknifedemo.BaseActivity.showToastl)
生成的ViewBinding不会调用父类的ViewBinding,解决方法是在子类动态调用父类ViewBiding的构造函数。 JakeWharton/butterknife#808
Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
Failed to transform file 'classes.jar' to match attributes {artifactType=android-dex, dexing-is-debuggable=true, dexing-min-sdk=15} using transform DexingTransform Error while dexing. butterknife 9.0.0 使用了 java 8 在app的builid.gradle文件添加:
android {
...
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
...
}
WARNING: API 'variantOutput.getProcessResources()' is obsolete and has been replaced with 'variantOutput.getProcessResourcesProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variantOutput.getProcessResources(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace. Affected Modules: module1 这是gradle-4.10.1-all的一个bug,使用gradle-4.6-all即可 解决方案: gradle插件使用3.2.1,在根目录build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
...
}
```
gradle-wrapper.properties修改:
```Groovy
...
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
在我的项目中集成了databinding,导致butterknife使用不当报错会只想databinding,给排查异常带来了很大的困扰。 例如,变量使用private, static修饰,变量类型和获取的实际类型不对应等。
Copyright 2019 wjianchen13
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.