ButterKnife:https://github.com/JakeWharton/butterknife
android-butterknife-zelezny:https://github.com/avast/android-butterknife-zelezny
butterknife在library中使用问题处理:http://blog.csdn.net/ytzys/article/details/53243438
一、ButterKnife在项目中使用
1.在项目的project 的build.gredle 文件中的dependencies标签下添加。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'
}
}
2.在module的build.gredle 文件中的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'
}
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;
}
但是按照这个步骤操作后并没有效果,用@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
}
这样处理之后,就正常了!