ButterKnife配置及使用教程及報錯解決

參考博客:

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
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章