butterknife注意事项

一、需要配置java8

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

二、Android studio3.0之后,annotationProcessor代替apt,无需再引用apt插件(3.0之前,项目根build.gradle中添加classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8',module中的build.gradle添加apply plugin: 'android-apt')

三、如果项目使用androidx则使用butterknife版本为9.0.0以上,如果使用support包则使用版本不超过9.0.0

四、Butterknife从8.4.0版本开始支持在非主module中使用,使用步骤如下

1、项目根build.gradle中添加(只在主module中无需添加此插件)

classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0'

2、module中build.gradle中添加

apply plugin: 'com.jakewharton.butterknife'

dependencies {
   implementation 'com.jakewharton:butterknife:9.0.0'
   annotationProcessor "com.jakewharton:butterknife-compiler:9.0.0"
}

3、在非主module中使用R2.id.*绑定控件,通过R.id.*匹配控件,不支持switch

@BindView(R2.id.ll_main_container)
LinearLayout mLlMainContainer;


@OnClick({R2.id.rel_back, R2.id.rel_net})
public void onClick(View v) {
   //通过R.id.*进行匹配,否则点击无效
   if(v.getId() == R.id.rel_back){
   
   }else if(v.getId() == R.id.rel_net){

   }
}

猜你喜欢

转载自blog.csdn.net/yufumatou/article/details/109583839