最近开新项目,想让项目分类更加清晰点,于是就想着多分几个module,说下butterknife在库文件中如何正确使用
git上面最新版本已经是8.8.1但是我不推荐最新版,多次测试和查询发现8.4.0在库文件使用没什么问题,之后的版本反正我测试不通过,各种报错,接下来说下8.4.0怎么使用。同时gradle针对3.0以上版本,之前的版本改下引用方式也是一样,因为complie已经被放弃了。
先说下我的大概目录,分为app主module,base module库文件,A module 库文件,可能是我的某个业务module,现在需要做的是base引用butterknife,然后A引用base,app引用A,当然app也可以引用base。
1.首先在Project的gradle添加代码:
mavenCentral()
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
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
}
}
这里我为了统一修改库文件的版本,统一了一些库文件的版本号,后面贴的代码我回直接引用这个版本号
ext {
compileSdkVersion = 27
buildToolsVersion = '27.0.0'
minSdkVersion = 17
targetSdkVersion = compileSdkVersion
// recycleViewLibVersion = '27.1.1'
appcompatLibVerson='27.1.1'
designLibVersion='27.1.1'
versionCode = 1
versionName = '1.0'
//8.4之后版本有点坑
butterknife ='8.4.0'
}
2.base里面如何引用,因为其他module都会直接或者间接引用base这个库文件,所以核心是base
添加代码 : apply plugin: 'com.jakewharton.butterknife'
api "com.jakewharton:butterknife:$butterknife"
annotationProcessor "com.jakewharton:butterknife-compiler:$butterknife"
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "com.android.support:appcompat-v7:$appcompatLibVerson"
implementation "com.android.support:design:$designLibVersion"
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
api "com.jakewharton:butterknife:$butterknife"
annotationProcessor "com.jakewharton:butterknife-compiler:$butterknife"
}
注意使用api其他包引用了base才能访问,implementation 表示只能本module访问,然后apt也被干掉了用annotationProcessor 这个替换了。老版的gradle换回去好了。
3.app主module中如何使用butterknife
代码: apply plugin: 'com.jakewharton.butterknife' annotationProcessor "com.jakewharton:butterknife-compiler:$butterknife"
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.jakewharton.butterknife'
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "com.android.support:appcompat-v7:$appcompatLibVerson"
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':base')
annotationProcessor "com.jakewharton:butterknife-compiler:$butterknife"
}
然后在主module中使用,ButterKnife.bind(this)这个我就不贴了,因为在baseActivity里写了。
@BindView(R.id.tv_test)
TextView tvTest;
@OnClick(R.id.tv_test)
void click(){
ToastUtils.showShort(context,"哈哈哈");
startActivity(PurchaseActivity.class);
}
我测试了下,不加annotationProcessor "com.jakewharton:butterknife-compiler:$butterknife"这个项目不会报错,但是点击事件什么的绑定不会生效。
4.库文件A如何使用
感觉这个最特别,gradle配置和app一样,还是贴下代码
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':base')
annotationProcessor "com.jakewharton:butterknife-compiler:$butterknife"
}
代码中使用要特别注意下使用R2,不要在意文字。
@BindView(R2.id.tv_purchase)
TextView tvPurchase;
@OnClick(R2.id.tv_purchase)
void click(){
ToastUtils.showShort(context,"fuck库文件");
}
这样的化都ok了,完美处理