在Android日常开发中,使用butterknife框架可以帮助我们快速的进行界面开发,但是之前butterknife不能在library中使用,原因是Android library中的R文件字段不是常量,在butterknife8.0.0以后修复了该bug,在butterknife的github主页有介绍,地址是butterknife,
但是按照上面说的,
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'
}
}
and then apply it in your module:
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
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;
...
}
我在library中引用butterknife,
工程的gradle中配置
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
library中引用
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' testCompile 'junit:junit:4.12' compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' }
这里我使用了apt插件。
然后在主module中引用library,
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile project(':mylibrary') compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:design:25.3.1'
}
发现在主module中使用butterknife,用@BindView
的地方提示NullPointerException
研究后发现要在每个使用butterknife的module中都要依赖一下butterknife包。
完整的gradle配置如下:
在工程的gradle配置如下:
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }在library中的gradle配置如下:
apply plugin: 'com.android.library' apply plugin: 'com.jakewharton.butterknife' apply plugin: 'com.neenbedankt.android-apt'
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' testCompile 'junit:junit:4.12' compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' }
在主module的gradle配置如下:
apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt'
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile project(':mylibrary') compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:design:25.3.1' compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' testCompile 'junit:junit:4.12' }
至此,可以在application和library中使用butterknife的注解了。