解决插件ButterKnife在Library中使用的问题

解决插件ButterKnife在Library中使用的问题

注:

如果这里引入 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.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.1234

2、在 Library 的 build.gradle 中 引入 butterknife

  • 在library的build.gradle中引入插件
apply plugin: 'com.jakewharton.butterknife'1
解决插件ButterKnife在Library中使用的问题

  • 在 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 中使用时,和单组件开发形式一样使用

解决插件ButterKnife在Library中使用的问题

2、在library 中使用

  • 要用 R2 代替 R findviewid
解决插件ButterKnife在Library中使用的问题


  • 在 click方法 中同样使用 R2,但是找 id 的时候使用 R。
解决插件ButterKnife在Library中使用的问题


  • 不能使用 switch- case 找 id 的,用 if-else 代替
解决插件ButterKnife在Library中使用的问题



分享到:


相關文章: