Butterknife问题解决

来源:互联网 发布:什么是网络社区营销 编辑:程序博客网 时间:2024/06/16 14:36

1. 官方指南及遇到的问题

butterknife当前版本是8.4.0,已经提供了对library project的支持,github主页的使用步骤总结一下就是:

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.4.0'  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.and then apply it in your module:

apply plugin: 'com.android.library'apply plugin: 'com.jakewharton.butterknife'dependencies {  compile 'com.jakewharton:butterknife:8.4.0'  annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.Now make sure you use R2 instead of R inside all Butter Knife annotations.

class ExampleActivity extends Activity {  @BindView(R2.id.user) EditText username;  @BindView(R2.id.pass) EditText password;}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

但是按照这个步骤操作后并没有效果,用@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.4.0'    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

然后步骤2也修改一下:

apply plugin: 'com.android.library'apply plugin: 'com.jakewharton.butterknife'apply plugin: 'android-apt'dependencies {  compile 'com.jakewharton:butterknife:8.4.0'  apt 'com.jakewharton:butterknife-compiler:8.4.0' // 修改的地方 把annotationProcessor改为apt}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这样处理之后,就正常了!

而且我们可以看到,在路径下,已经生成了对应的中间文件: 
这里写图片描述

0 0
原创粉丝点击