当前ButterKnife的最新版本是10.2.1 官网链接
测试环境:
Android studio 版本:3.5.0 Gradle 版本:5.4.1
ButterKnife 10.2.0使用步骤:
app build.gradle
第一步:导入远程库
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
第二步:build.gradle文件添加butterknife的引用
apply plugin: 'com.jakewharton.butterknife'
第三步:添加compileOptions(因为 10.版本以上用的lambda表达式 需要用到1.8版本的jdk)
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
project build.gradle
第四步:添加 mavenCentral() 和 classpath ,下面代码是完整的配置文件
buildscript {
repositories {
jcenter()
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
说完了使用,接下来说一下坑在哪里:
是我一开始在APP module中没有添加butterknife的引用,通过 第三方 gradle配置 api'com.jakewharton:butterknife:10.2.0' 的方式来绑定控件,但是运行中总是抛出空指针异常,说明控件没有绑定成功,然后在APP module中又添加了 butterknife的引用,为防止出现异常 这里我把引用全部改为了implemention的方式 这也是升级版本之后出现的问题,我在此处做一下记录 加深记忆! |