在8.0之前butterknife在Module Library中使用有bug,仔细观察原来是由于引用R资源字段不是常量所导致在Bind的时候找不到id,8.0之后修复了这个bug,原文:
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;
...
}
①、Project的gradle文件配置
dependencies { classpath 'com.android.tools.build:gradle:2.3.1' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1' // 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(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.2.1' compile 'com.android.support:design:23.1.1' compile 'com.jakewharton:butterknife:8.5.1' apt 'com.jakewharton:butterknife-compiler:8.5.1' }③、主Module的gradle文件配置
apply plugin: 'com.neenbedankt.android-apt'
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') 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' compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8' testCompile 'junit:junit:4.12' compile 'com.jakewharton:butterknife:8.5.1' apt 'com.jakewharton:butterknife-compiler:8.5.1' compile project(':nimLib') }然后在Library中使用的时候用R2代替R,BindView(R2....)。注意在做分支语句请使用if-else代替switch-case不然会报错。。。。