ButterKnife读取控件失败问题解决

ButterKnife的使用步骤如下:

1.添加ButterKnife Plugins

点击AndroidStudio —>Preferences—>Plugins—>选择 Android ButterKnife Zelezny后点击install后,重启即可,如果没有可点击下方中间的Browse Repositories去搜索

2.添加ButterKnife依赖

在app module 的build.gradle文件中dependencies{}下加入以下代码:

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
复制代码


一般出现了ButterKnife读取控件失败的原因是,同时配置了apt(比如使用Dagger时需要配置apt)

那么这个时候需要将以上第二句 annotationProcessor应变成 apt ,否则会找不到相应的控件。如下:

apt 'com.jakewharton:butterknife-compiler:8.8.1'复制代码

也就是说annotationProcessor和apt不能同时使用


android-apt与annotationProcessor有什么区别呢?

android-apt 是安卓处理注释的工具,全称为Annotation Processing Tool ,是个人开发的一个apt框架,由于google后面推出了annotationProcessor来替代android-apt,所以后续这个框架也停止更新了,但仍然有一些项目上会用到。

apt配置方法

1. project module 的 build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}复制代码


2. app module 的 build顶部增加如下代码:

apply plugin: 'com.neenbedankt.android-apt'复制代码


3. app module 的 build 中的 dependencies{} 增加如下代码:

compile 'com.jakewharton:butterknife:8.8.1'
apt 'com.jakewharton:butterknife-compiler:8.8.1'复制代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值