ButterKnife8.8.1出现 NullpointException(空指针)的解决办法

最近有人向我反馈说,使用了ButterKnife8.8.1后出现空指针的问题,但也有些人说没问题。我看了这两个人的配置,基本都是按照JakeWharton大神在github上面说明进行配置的;但为什么一个是正常的,而一个却出现空指针?

buildscript {

    repositories {

        mavenCentral()

    }

    dependencies {

        classpath ‘com.jakewharton:butterknife-gradle-plugin:8.8.1’

        …

    }

}

apply plugin: ‘com.jakewharton.butterknife’

apply plugin: ‘com.android.library’

apply plugin: ‘com.jakewharton.butterknife’

dependencies {

    …   

    compile ‘com.jakewharton:butterknife:8.8.1’

    annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

}

经过仔细对比了一下

发现出错的一方是因为在library module里面进行如下引用

  compile ‘com.jakewharton:butterknife:8.8.1’

  annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

而未出错的一方是因为在app module里面进行如下引用

  compile ‘com.jakewharton:butterknife:8.8.1’

  annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

有些项目可能由几个依赖的library module和app module组成,所以由于依赖关系,

compile ‘com.jakewharton:butterknife:8.8.1’

butterknife的导入必须放在library module。

那么如何解决呢?解决方法很简单,在 app module 内引用 annotationProcessor

 annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

 而在 library  module内引用 butterknife 即可解决。

compile ‘com.jakewharton:butterknife:8.8.1’

    原文作者:自由懒散的码农
    原文地址: https://www.jianshu.com/p/b0999774fd15
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞