原文地址:https://www.jianshu.com/p/b0999774fd15

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

build {

    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
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

收藏 打印