参考博客:
https://www.jianshu.com/p/eaafc13e71ba
https://www.cnblogs.com/chenyangsocool/p/9565730.html
https://www.jianshu.com/p/7c20d777578f
安装方法:
打开androidstudio,file->settings->plugins->marketplugins->android butterknife zelezny:install->重启android studio
配置方法:
1.添加在线依赖库,一行代码,在project下的build.gradle的repositories括号里添加两遍。
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
2.配置刚刚添加的
依赖库,就是加两行代码。在app下的build.gradle里加上
implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT'
使用方法:将鼠标放在activity文件中引用的layout文件名上,右击选中generate点击“generate butterknife injections”,点击“confirm”即可
----------------------------------------------------------------一条很有必要的分割线----------------------------------------------------------------------
昨晚以上步骤,我以为一切都搞定了。然而现实却是:在编译app时候报错了“The given artifact contains a string literal with a package reference 'android.support.v4.content' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.”
一番搜索后了解到,是butterknife的版本太旧导致的,于是修改了一下版本:
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
结果又报了新错误:“Error: Static interface methods are only supported starting with Android N (--min-api 24): void butterknife.Unbinder.lambda$static$0()”,在app的build.gradle中加个编译选项即可。
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}