ButterKnife项目地址:https://github.com/JakeWharton/butterknife

AndroidX按照正常的方式集成:

android {  ...  // Butterknife requires Java 8.  compileOptions {    sourceCompatibility JavaVersion.VERSION_1_8    targetCompatibility JavaVersion.VERSION_1_8  }}dependencies {  implementation 'com.jakewharton:butterknife:10.1.0'  annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'}
buildscript {  repositories {    mavenCentral()    google()   }  dependencies {    classpath 'com.jakewharton:butterknife-gradle-plugin:10.1.0'  }}

集成完成以后,自动生成的BindView中的View 是Null,多方查找未果,结果在一个老的项目中找到了答案,于此记录一下;

Android 多个Module使用ButterKnife出现的NullPointerException_第1张图片

这个是我的项目结构,我把常用的一些工具类和常用的第三方放在了common模块中(ButterKnife也在这个模块中);

但是我在主模块app中使用ButterKnife生成的控件一直是Null

Android 多个Module使用ButterKnife出现的NullPointerException_第2张图片

etPhone、etPwd均是Null;

解决方法是在app模块的gradle中增加注解annotationProcessor(注解处理器)

annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

也就是说,我在common模块中引入了ButterKnife,如果在app模块中使用的话,还要在app模块加入annotationProcessor,不然app模块没有办法根据注解生成Java或R文件;

如果加了这个还是null或报错,可以加上  apply plugin: 'com.jakewharton.butterknife' 试试;

Android 多个Module使用ButterKnife出现的NullPointerException_第3张图片

总结,使用ButterKnife的模块必须有注解处理器 annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

类似使用到注解处理器的框架还有很多 ButterKnife、Glide 、Arouter、Dagger2等等。。。

 

更多相关文章

  1. Android 神兵利器Dagger2使用详解(四)Scope注解的使用及源码分析
  2. Ubuntu编译Android整个系统以及编译指定模块到模拟器
  3. android自动更新新版模块(简单,实用)
  4. Android Camera 模块分析
  5. android模块&相关技术
  6. Android 打造编译时注解解析框架 这只是一个开始
  7. Android 注解,注入框架
  8. Android注解:自定义注解之源码注解

随机推荐

  1. 文章:开发者眼中的Android手机平台
  2. Android中的WebView进行直接加载网页
  3. Android中的任务和进程详解
  4. Android – 專案的建立與結構
  5. android 自定义menu及 Error parsing XML
  6. [重要]使用Vitamio打造自己的Android万能
  7. 关于Android的反编译apk文件
  8. Android 之 超详细 Broadcast
  9. 看这里!顶级必备Android开发者工具
  10. Eclipse在线升级Android ADT和SDK(4.x)方法