ButterKnife 8.4版本使用依赖与8.2版本不同,已更新下面的内容,阅读时请注意注释


最近在更新ButterKnife时发现出了点问题,到官网看了一下,原来ButterKnife使用apt.配置也要进行相应的更改

0x01 Project build.gradle

首先在项目的build.gradle添加apt引用

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'

        // butterkinfe8.2版本使用了apt,但是8.4版本改为另一个butterknife gradle plugin,路径如下
        //classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
        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
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

0x02 module build.gradle配置

在要使用ButterKinfe的module的build.gradle中添加apt插件引用

apply plugin: 'com.android.application'
//butterknife 8.2版本使用 apt
apply plugin: 'com.neenbedankt.android-apt'
//butterknif 8.4 使用了gradle plugin
//apply plugin: 'com.jakewharton.butterknife'
...

然后添加ButterKinfe的引用

dependencies {
    compile 'com.jakewharton:butterknife:8.2.1'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
    //butterknife 8.4版本依赖有变动
    //compile 'com.jakewharton:butterknife:8.4.0'
    //annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

然后就可以使用啦~