Android 组件化开发ButterKnife配置和相关问题

ButterKnife 组件化相关配置

一:官方给butterKnife的模块化使用方式和我的组件化项目结构
image.png

image.png
二:组件化中ButterKnife使用
1.在工程project的build.gradle下添加butterKnife插件依赖

classpath "com.android.tools.build:gradle:3.5.4"
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.0'//butterKnife组件化第一步

注:这有一个Gradle和ButterKnife版本编译冲突问题
2.在common_base库公共依赖库build.gradle中添加

//ButterKnife第三步功能基础组件中依赖
api rootProject.ext.dependencies["butterknife"]//等价于:api 'com.jakewharton:butterknife:10.2.0'
//annotationProcessor rootProject.ext.dependencies["butterknifeCompiler"]//这个每个用到butterKnife都需要添加故基础组件可以不添加

使用api是为了其他依赖这个库都能使用,不用重复添加,具体可以看看api,implementation ,compile区别
3.App壳模块中添加,是application模块

apply plugin: 'com.android.application'
implementation project(path: ':common_base')
//黄油刀ButterKnife
//这个东西一定要放在这个里面,否则会报错的(空指针)!
annotationProcessor rootProject.ext.dependencies["butterknifeCompiler"]//等价于:annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'

4.在其他lib库模块需要添加

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'//相应的库module模块添加这个
implementation project(path: ':common_base')
//黄油刀ButterKnife
annotationProcessor rootProject.ext.dependencies["butterknifeCompiler"]//等价于:annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'

三:ButterKnife使用相关问题
1.Gradle和ButterKnife版本冲突相关问题,具体不知,一般是降低Gradle版本
2.butterknife报空指针问题 需要添加annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
3.在其它Module中使用Butterknfie怎么使用(主要是解决R资源的问题)

@BindView(R2.id.login_tv)//通过R2
TextView login_tv;

ButterKnife.bind(this);

原因:当作为库的一部分构建时,R.java生成的类中的值不会被声明为“final”.用于@BindView()的注释需要这些R.值是最终的,以后不会更改.
故:ButterKnife就使用了一个插件,拷贝R中所有的值到R2中,并且把R2中的值都设置成了final类型的,这样就能蒙混过关,反正子模块中的值可能还是要重新设置的。