butterknife

butterknife 介绍

  • 使用 @BindView 代替 findViewById 的使用
  • 使用注解代替数组的逐个声明
  • 使用 @OnClick 代替 匿名内部类这样的监听器
  • 使用资源注释 代替 资源查找

使用举例

    class ExampleActivity extends Activity {
      @BindView(R.id.user) EditText username;
      @BindView(R.id.pass) EditText password;

      @BindString(R.string.login_error) String loginErrorMessage;

      @OnClick(R.id.submit) void submit() {
        // TODO call server...
      }

      @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_activity);
        ButterKnife.bind(this);
        // TODO Use fields...
      }
    }

github 地址

https://github.com/JakeWharton/butterknife
http://jakewharton.github.io/butterknife/

使用说明

添加依赖

    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

如果使用的 Kotlin , 使用 kapt 代替 annotationProcessor

在 library 中使用,需要另外操作

  • 第一步、项目的 build.gradle 中添加依赖

    buildscript {
      repositories {
        mavenCentral()
       }
      dependencies {
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
      }
    }
    
  • 第二步、在 Module 中添加插件依赖

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

备注:插件的作用就是生成视图绑定所需要的唯一 ID(也就是 R2 文件)。因为 R 文件只能在 app Module 中使用,为了能够在 library 中也正常使用,作者设计这个插件。此插件由 R 文件 生成 R2 文件,达到所有 library 均可以使用的目的。

发现错误解决办法

  • 由于 Andorid Studio 3.0 与 ButterKnife 的不兼容导致如下错误:

Error:Unable to find method ‘com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;’.Possible causes for this unexpected error include:

Gradle’s dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)Re-download dependencies and sync project (requires network)

The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.Stop Gradle build processes (requires restart)

Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

  • 这个问题对应的 Issue 地址

https://github.com/JakeWharton/butterknife/issues/963

  • 解决方法

将所有 ButterKnife 版本降到 8.4.0

// 1. 项目文件中修改
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'


// 2. Module 中修改
    implementation 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

项目中多个包使用

  • 在核心包中添加两项内容:
    //1. 添加 Android Studio 插件
    apply plugin: ‘com.jakewharton.butterknife’

    // 2. 添加 butterknife 注解依赖
    implementation 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    
  • 在其他包中使用时,也要添加两项:

    //1. 添加 Android Studio 插件
    apply plugin: 'com.jakewharton.butterknife'
    
    // 2. 添加 butterknife 注解依赖
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    
  • 记住使用之前先编译一下,以生成 R2 文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值