AndroidStudio中Module Library中使用butterknife踩坑
阿新 • • 發佈:2019-02-03
在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②、Library的gradle檔案配置// in the individual module build.gradle files }
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不然會報錯。。。。