注:
如果這裡引入 ButterKnife 8.5.1 以上的版本,如:
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'1
會出現編譯異常:
Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.Possible causes for this unexpected error include:
- Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)Re-download dependencies and sync project (requires network)
- The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.Stop Gradle build processes (requires restart)
- Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
2、在 Library 的 build.gradle 中 引入 butterknife
- 在library的build.gradle中引入插件
apply plugin: 'com.jakewharton.butterknife'1
- 在 dependencies 中添加依賴,此處可以使用最新版 butterknife
implementation 'com.jakewharton:butterknife:8.8.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'12
若想 library 中的 butterknife 被主 module 使用,可將 implementation 改為 api:
api 'com.jakewharton:butterknife:8.8.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'12
到此,接入工作完畢。
4. 使用和注意事項
1、在 主module 中使用時,和單組件開發形式一樣使用
2、在library 中使用
- 要用 R2 代替 R findviewid
- 在 click方法 中同樣使用 R2,但是找 id 的時候使用 R。
- 不能使用 switch- case 找 id 的,用 if-else 代替
閱讀更多 程序員小新人學習 的文章