ButterKnife 8.6.0 使用

ButterKnife 8.6.0 使用

一、ButterKnife在project中的使用

1、在项目的project中的build.gradle文件中的dependencies标签下添加:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'
    }
}

2、在modle的build.gradle文件中的dependencies标签中添加(导入库的依赖):

dependencies {
    compile 'com.jakewharton:butterknife:8.6.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}

3、装插件 android-butterknife-zelezny

二、ButterKnife在library中的使用

1、官方指南及遇到的问题
To use Butter Knife in a library, add the plugin to your buildscript:

buildscript {  
  repositories {  
    mavenCentral()  
   }  
  dependencies {  
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'  
  }  
}

and then apply it in your module:

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

dependencies {  
  compile 'com.jakewharton:butterknife:8.6.0'  
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'  
}

但是按照这个步骤操作后并没有效果,用@BindView的地方提示NullPointerException,用@onClick的标注的点击事件,点击后也没有反应
2、 最终解决方案
最后发现,只需修改一下上述步骤1和2就可以了。
步骤1加上依赖注入的plugin:

buildscript {  
    repositories {  
        jcenter()  
        mavenCentral()  
    }  
    dependencies {  
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // 添加的部分  
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'  
    }  

}

然后步骤2也修改一下:

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

dependencies {  
  compile 'com.jakewharton:butterknife:8.6.0'  
  apt 'com.jakewharton:butterknife-compiler:8.6.0' // 修改的地方 把annotationProcessor改为apt  
}

之后就正常了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值